The GPT WhatsApp bot is a simple WhatsApp bot that uses the GPT-3.5 model using OpenAI API to generate responses to messages sent by the user. The bot is built using the Twilio API and the OpenAI API. The project is a FastAPI application that uses the OpenAI API to generate responses to incoming messages. It also logs the conversation in a database using SQLAlchemy.
-
main.py
: This file is a Python script that sets up a FastAPI server to handle incoming webhook requests. It uses the OpenAI API to generate responses using ChatGPT. The conversation data is stored in a postgres database and the response is sent back to the user via a messaging service. -
utils.py
: This file contains a function send_message that uses the Twilio Messaging API to send WhatsApp messages. It splits the message into substrings of 1600 characters or less and sends each substring as a separate message. This has been done to prevent the 1600 character limit exception by Twilio API. The Twilio client is initialized with the necessary credentials from the decouple library. -
models.py
: This file contains the SQLAlchemy models for the database. It contains the Conversation model that stores the conversation data in the database. -
.env
: Structure the env file like this:DB_USER= DB_PASSWORD= TWILIO_ACCOUNT_SID= TWILIO_AUTH_TOKEN= TWILIO_NUMBER= OPENAI_API_KEY=
-
requirements.txt
: This file contains the list of all the dependencies required to run the project. -
Dockerfile
: This file contains the Dockerfile for the project. It contains the instructions to build the Docker image for the project.
- User sends the message to the twilio sandbox number
- Twilio sends a webhook request to the FastAPI server
- The FastAPI server requests the OpenAI API
- The API response is received from the OpenAI API
- The response is stred in the database
- The confirmation is sent to the FastAPI server
- The FastAPI server sends the response to the user via Twilio API
- The user receives the response from the Twilio sandbox number
- Make sure python3 is installed on your system. If not, install it from here.
- Clone the repo
git clone https://github.com/ayushgml/chatgpt-whatsapp-bot cd chatgpt-whatsapp-bot
- Create a virtual environment and activate it
python -m venv venv . venv/bin/activate
- Install the dependencies
pip install -r requirements.txt
- Install postgreSQL. You can refer the postgres installation process for your own system from here.
- Create a postgres database:
createdb mydb
- Create a .env file in the root directory of the project and add the following environment variables:
DB_USER= DB_PASSWORD= TWILIO_ACCOUNT_SID= TWILIO_AUTH_TOKEN= TWILIO_NUMBER= OPENAI_API_KEY=
- Run the project
uvicorn main:app --reload
- Open the ngrok tunnel
ngrok http 8000
- Copy the ngrok url and paste it in the Twilio sandbox settings for the WhatsApp sandbox number.
- Send a message to the Twilio sandbox number and you will receive a response from the bot.
Ayush Gupta - @itsayush__ - [email protected]