This project implements a bot for Facebook Messenger that uses text embeddings and a classification model (XGBoost) to automate responses and actions based on conversations. Originally designed for a specific industry, the bot can be easily adapted to other industries with minimal modifications.
- Automation: Automatically responds to messages according to defined rules or notifies the user to attend personally.
- AI Integration: Uses embeddings generated by OpenAI to train classification models and improve response accuracy.
- Scalability: Modular design and adaptable to different industries or workflows.
- Puppeteer and Node.js: Uses Puppeteer to interact with Facebook Messenger’s web interface, extracting messages and executing automated actions.
- functions
- constants
- selectors.ts # CSS Selectors and XPaths used by Puppeteer
- settings.ts # General bot settings
- text.ts # Predefined texts and messages
- switch # Switcher module that manages responses
- disp.ts # Automatic response function
- embeddings.ts # Function to send text to the OpenAI API
- images.ts # Function to send images and responses
- noti.ts # Function to notify via Telegram
- repr.ts # Automatic response function
- switcher.ts # Orchestrates all the above functions
- utils
- extractAndSaveConversation.ts # Extracts and saves conversations
- move.ts # Accesses Messenger and navigates chats
- save.ts # Saves conversations to JSON
- timer.ts # Timer functions
- utils.ts # Utilities to identify chats and new messages
- index.ts # Main entry point, calls login and main
- login.ts # Logs into Facebook, saves and loads cookies
- main.ts # Heart of the bot, coordinates modules and workflow
- Node.js v14+
- Python 3.8+
- Puppeteer for Node.js
- Flask for Python
- XGBoost, Pandas, NumPy, and OpenAI API in Python
-
Clone the repository:
git clone https://github.com/santiagocanepa/facebook-chat-bot.git cd facebook-chat-bot
-
Install Node.js dependencies:
pnpm install
-
Install Python dependencies:
pip install -r requirements.txt
-
Set up environment variables:
- Create a
.env
file at the root of the project to store your credentials and configurations:USERNAME= PASSWORD= BOTTOKEN= CHATID= USERAGENT= WIDTH= HEIGHT= REPR='' OPENAI_API_KEY= DISP='' SENDPHOTOS='' PHOTOSLOCATION='' TELEGRAM=''
- Create a
-
Train the Model: Use the
extractAndSaveConversation.ts
function to extract and save conversations. Subsequently, manually label these conversations. It is ideal to use multiple labels that segment different instances of the conversation to optimize the model's precision. Ensure to always use the same responses for the first instances of conversations and reuse these responses in subsequent instances, thus establishing a pattern that allows automatically redirecting the rest of the conversation to the corresponding model. For embedding the conversations, you can refer to this example in a Jupyter Notebook available on https://www.kaggle.com/code/ivancanepa/embedding-of-conversations. Generate the embeddings using the provided Python script and train an XGBoost model with these embeddings and labels. For more details on how to train the XGBoost models with the already embedded conversations, check this other Jupyter Notebook on https://www.kaggle.com/code/ivancanepa/chatbot-facebook. -
Start the Bot:
pnpm run init
The bot will log in to Facebook, load the saved cookies, and start monitoring chats.
-
Customize Responses: Modify the modules within
switch
to adapt automatic responses and notifications to your specific needs.
The extractAndSaveConversation.ts
module extracts conversations from Facebook chat and saves them to a JSON file. Over time, you can label these conversations to train a more accurate classification model.
A Kaggle notebook is provided to train an XGBoost model with the generated embeddings and labels. This notebook allows users to replicate the training and customize their model according to their needs.
If you want to contribute to the project, please open an issue or send a pull request with your improvements. All help is welcome.
This project is licensed under the MIT License. See the LICENSE
file for more details.