Skip to content

Facebook Chat Bot with Embeddings. A customizable bot for Facebook Messenger using text embeddings and XGBoost for automated responses. Easily adaptable to various industries and workflows.

Notifications You must be signed in to change notification settings

santiagocanepa/ChatBot_Facebook_IA

Repository files navigation

🤖 Facebook Chat Bot with Embeddings and Classification

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.

🌟 Features

  • 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.

📁 Project Structure

- 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

🔧 Requirements

  • Node.js v14+
  • Python 3.8+
  • Puppeteer for Node.js
  • Flask for Python
  • XGBoost, Pandas, NumPy, and OpenAI API in Python

🚀 Installation

  1. Clone the repository:

    git clone https://github.com/santiagocanepa/facebook-chat-bot.git
    cd facebook-chat-bot
  2. Install Node.js dependencies:

    pnpm install
  3. Install Python dependencies:

    pip install -r requirements.txt
  4. 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=''
      

🛠️ Usage

  1. 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.

  2. Start the Bot:

    pnpm run init

    The bot will log in to Facebook, load the saved cookies, and start monitoring chats. image

  3. Customize Responses: Modify the modules within switch to adapt automatic responses and notifications to your specific needs.

🧠 Model Training

Conversation Collection

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.

Training in Kaggle

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.

🤝 Contributions

If you want to contribute to the project, please open an issue or send a pull request with your improvements. All help is welcome.

📄 License

This project is licensed under the MIT License. See the LICENSE file for more details.

About

Facebook Chat Bot with Embeddings. A customizable bot for Facebook Messenger using text embeddings and XGBoost for automated responses. Easily adaptable to various industries and workflows.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published