Have you ever wished your WhatsApp could automatically reply to messages just like a chatbot? In this post, weβll walk you through how to connect your personal WhatsApp account to an auto-responder bot powered by OpenAI API. This solution is built using Node.js and a library called whatsapp-web.js. The final bot reads incoming messages and responds intelligently using OpenAI's ChatGPT.
Create a new folder for your project and initialize Node.js:
mkdir whatsapp-ai-bot
cd whatsapp-ai-bot
npm init -y
npm install whatsapp-web.js qrcode-terminal openai dotenv
const { Client } = require('whatsapp-web.js');
const qrcode = require('qrcode-terminal');
const { OpenAI } = require('openai');
require('dotenv').config();
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
const client = new Client();
client.on('qr', qr => {
qrcode.generate(qr, { small: true });
});
client.on('ready', () => {
console.log('WhatsApp bot is ready!');
});
client.on('message', async message => {
if (message.fromMe) return;
try {
const chatCompletion = await openai.chat.completions.create({
messages: [{ role: "user", content: message.body }],
model: "gpt-3.5-turbo"
});
const reply = chatCompletion.choices[0].message.content;
message.reply(reply);
} catch (err) {
message.reply("Sorry, something went wrong.");
console.error(err);
}
});
client.initialize();
OPENAI_API_KEY=your_openai_key_here
node index.js
Scan the QR code with your WhatsApp. Once connected, the bot will auto-reply to any message it receives.
pm2.
Looking to earn money online by doing simple microtasks on your phone or laptop?
ClickWorker pays real cash for jobs like data entry, AI training, and surveys.
(Affiliate Link β Supports our blog π)
You now have a working system that lets your WhatsApp auto-reply to messages using AI. You can extend this to filter replies, respond to specific commands, or integrate your own database. Use pm2 or a cloud server to keep it running without interruption.
Would you like this as a downloadable template or app? Leave a comment below!