An intelligent and contextual AI bot for WhatsApp groups, powered by LLMs (OpenAI/OpenRouter) and whatsapp-web.js.
- Easy Login: Scan the QR code once, either in the terminal or via the web.
- Contextual: Understands the group chat history to provide relevant answers.
- Smart Content Moderation: Automatically analyzes messages to detect inappropriate content (abusive, pornographic, etc.) and gives dynamic warnings using AI.
- Flexible: Supports various LLM models from OpenAI and OpenRouter.
- Persona Customization: Adjust the bot’s tone and language to your preference via the
.envfile. - Local Storage: All conversations and session data are stored locally in an SQLite database.
- Group Filter: Control in which groups the bot will respond.
- Runtime: Node.js
- Language: TypeScript
- WhatsApp Integration:
whatsapp-web.js - Web Server: Express
- Database: SQLite
- ORM: Prisma
- LLM: OpenAI, OpenRouter
-
Clone the Repository
git clone <this-repository-url> cd whatsapp-ai-bot
-
Install Dependencies
npm install
-
Configure Environment
-
Copy
.env.exampleto.env.cp .env.example .env
-
Open
.envand fill in all required variables, especially:LLM_PROVIDER: Chooseopenaioropenrouter.OPENAI_API_KEY: Your OpenAI API key.OPENROUTER_API_KEY: Your OpenRouter API key (if used).OPENROUTER_REFERER: Your application or website URL (required for OpenRouter).ALLOWED_GROUPS: (Optional) List of ALLOWED WhatsApp group NAMES, separated by commas (example:My Cool Group,Another Chat). If left empty, the bot will respond in all groups.TRIGGER_KEYWORDS: (Optional) List of keywords separated by commas (example:bot,ask,hi). If a message contains any of these keywords, the bot will respond even without being mentioned. If empty, the bot only responds when mentioned.MENTION_TRIGGER_KEYWORDS: (Optional) List of keywords separated by commas (example:what,how). If the bot is mentioned, it will only respond if the message also contains one of these keywords. If empty, the bot responds every time it’s mentioned.CONTENT_MODERATION_ENABLED: (Optional) Set totrueto enable automatic content moderation.MODERATION_PROMPT: (Optional) Customize the prompt used to detect inappropriate content.MODERATION_WARNING_PROMPT: (Optional) Customize the prompt used to generate dynamic warning messages.
-
-
Setup Database
Run Prisma migrations to create the SQLite database file and the required tables.
npm run prisma:migrate
-
Run the Bot
npm run dev
-
Login to WhatsApp
- Option 1 (Terminal): The QR code will appear directly in your terminal. Scan it using WhatsApp on your phone.
- Option 2 (Web): Open your browser and go to
http://localhost:3000/qr. Scan the displayed QR code.
After logging in successfully, the session will be stored locally. You don’t need to scan again every time you run the bot.
- Invite the bot to your WhatsApp group.
- To ask a question, mention the bot in your message (example:
@BotName how are you?). - The bot will read the group’s message history for context, then give a relevant answer.