Welcome to the WhatsApp AI Chatbot! This project uses Node.js and OpenAI's GPT to create a innovative chatbot for WhatsApp, Intelligent chatbot that can respond to user messages and perform actions based on certain commands.
- Project Overview
- Installation
- Using the Chatbot
- Project Structure
- Contributing
- Chat History JSON Structure
- Contact
This WhatsApp AI Chatbot is designed to interact with users in WhatsApp groups or individual chats. It can respond to user messages and perform specific actions when triggered by a message. The core functionality includes:
-
User Messaging: The chatbot can engage in conversations with users and respond to their messages.
-
Chat History: All chat interactions are logged in JSON files, making it easy to track conversations and system actions over time.
To get this project up and running, follow these steps:
-
Clone the Repository: Clone this GitHub repository to your local machine.
git clone https://github.com/OfekiAlm/whatsapp-ai-chatbot.git cd whatsapp-ai-chatbot
-
Install Dependencies: Make sure you have Node.js installed. Then, install the required Node.js packages.
npm install
-
WhatsApp Web Login: In order to use this chatbot, you need to log in to WhatsApp Web using your phone. The chatbot will display a QR code for you to scan using the WhatsApp app on your phone.
-
OpenAI API Key: You need an API key from OpenAI to use the GPT functionality. Visit the OpenAI website to sign up and obtain an API key. Once you have it, create a
.env
file in the project root directory and add your API key as follows:OPENAI_API_KEY=your_api_key_here
-
Make a folder for chat history: Create the directory using the following command:
mkdir chats_history
-
(OPTINAL) for transcriptions funcionality: Create the directory using the following command:
mkdir .audio_cache
And if you haven't installed
ffmpeg
already:Linux -
sudo apt update sudo apt install ffmpeg
Windows -
You can follow this tutorial which is guiding with a step by step explanation
-
Start the Bot: Run the bot using the following command:
node bot.js
-
Setting a phone number For testing purposes(OPTIONAL): This phone number will act as chatGPT reciever of messages and will reply to each message sent to this phone number via Whatsapp.
PHONE_NUMBER_TO_CONTACT = your_number
-
Start Chatting: Once the bot is running and authenticated, you can start chatting with it on WhatsApp!
- Simply send messages to the bot, and it will respond accordingly.
- To trigger system actions, send a message starting with "!act". The chatbot will log and respond to these actions. Further information about this role
- All chat interactions are saved in JSON files in the
chats_history
directory.
The chatbot now supports image generation. Command format:
!generate [number of images] [prompt]
For example, !generate 3 sunset over a beach
.
default number of creations is one image.
bot.js
: Main entry point of the application.chat_funcionality.js
: Module for chat-related functionality and history logging.open_ai_gpt.js
: Module for interacting with OpenAI's GPT API.package.json
: Contains project dependencies and scripts.
The chat history is stored in JSON format, organized as an array of messages. Each message has two main properties:
"role"
: Indicates the sender's role, such as "user" or "system"."content"
: Contains the message content.
This structured format simplifies the storage and retrieval of chat history for analysis and tracking.
{
"history":
[
{ "role": "system", "content": "act like a teacher"},
{ "role": "user", "content": "Hi"},
{ "role": "user", "content": "Can you explain about math equations?"}
]
}
Please note that messages sent by the assistant are not included in this JSON structure (For computing purposes).
In the bot.js
file, you will find a variable named ASISTANT_LOGS
. This variable is used to control whether the chatbot logs the responses generated by the assistant (AI) during conversations. Here's how it works:
-
If
ASISTANT_LOGS
is set totrue
, the chatbot will log the responses generated by the assistant into the chat history, making it possible to review the assistant's interactions in the chat history. -
If
ASISTANT_LOGS
is set tofalse
, the chatbot will not log the assistant's responses in the chat history. This can be useful for scenarios where you only want to track user messages and not the AI-generated responses.
To enable or disable the logging of assistant responses, open the bot.js
file and locate the ASISTANT_LOGS
variable at the beginning of the script. You can change its value to true
or false
as needed.
const ASISTANT_LOGS = true; // Set to true to log assistant responses, or false to exclude them from the chat history.
Feel free to contribute to this project by submitting issues or pull requests. I welcome any improvements or feature additions!
Thank you for using my project. Enjoy your conversations with this intelligent chatbot!
If you have any questions, feedback, or need assistance with the project, please don't hesitate to reach out:
- Email: ofekalm100@gmail.com
Feel free to contact me anytime; I'm here to help!