Category: Programming | Reading time: 8 minutes
🇮🇩 Baca versi Bahasa Indonesia
A few months ago, I got tired of getting the same WhatsApp messages from coworkers every day: "WiFi on the 2nd floor is down," "Printer won't connect," "Need a password reset." The answers were often nearly identical to what I'd sent someone else the week before.
Instead of copy-pasting the same replies over and over, I decided to build a WhatsApp bot that could answer those common questions on its own. That's how NEXI was born — a small IT helpdesk bot that's now genuinely used to help with my day-to-day work.
In this post, I want to share the most basic approach to building a WhatsApp bot with Node.js and the whatsapp-web.js library — the simple version first, well before it reached NEXI's current level. But this is the exact same foundation I still use today.
Why whatsapp-web.js?
There are a few ways to build a WhatsApp bot — using the official WhatsApp Business API (which is paid and requires Meta's approval), or using an unofficial library that works by "mimicking" WhatsApp Web in a headless browser.
I went with whatsapp-web.js because:
- It's free and open-source
- No business approval from Meta required
- Well-suited for small-to-medium scale use (internal team tools, personal automation)
- Documentation is reasonably solid
Honest disclaimer: since this is unofficial, there's a risk of getting your account banned if it's used for spam or mass messaging to a lot of unknown numbers. For internal/personal use like my case, that risk is low as long as it's used reasonably.
What You'll Need
- Node.js installed (version 16 or above)
- A dedicated WhatsApp number for the bot (don't use your primary personal number — keep a backup in case of a ban)
- Basic familiarity with JavaScript
Step 1: Set Up the Project
Create a new folder and install the dependencies:
mkdir simple-whatsapp-bot
cd simple-whatsapp-bot
npm init -y
npm install whatsapp-web.js qrcode-terminal
qrcode-terminal displays the login QR code right in your terminal, so you don't need to open an extra browser window.
Step 2: Initialize the Client
Create an index.js file:
const { Client, LocalAuth } = require('whatsapp-web.js');
const qrcode = require('qrcode-terminal');
const client = new Client({
authStrategy: new LocalAuth(),
});
client.on('qr', (qr) => {
qrcode.generate(qr, { small: true });
console.log('Scan the QR code above using WhatsApp on your phone');
});
client.on('ready', () => {
console.log('Bot is ready!');
});
client.initialize();
LocalAuth is important here — it saves your login session locally, so you don't have to scan the QR code again every time you restart the bot. Run it with:
node index.js
Scan the QR code that appears using WhatsApp on your phone (Linked Devices menu), and if successful, "Bot is ready!" will appear in your terminal.
Step 3: Build Your First Auto-Reply
Now for the fun part — making the bot respond to incoming messages. Add this before client.initialize():
client.on('message', async (message) => {
const text = message.body.toLowerCase();
if (text === 'hi' || text === 'hello') {
await message.reply('Hi there! How can I help?');
}
if (text.includes('wifi')) {
await message.reply('For WiFi issues, try restarting the router first. If the problem persists, please report it to the IT team.');
}
if (text.includes('password')) {
await message.reply('You can reset your password through the employee portal under Account > Forgot Password.');
}
});
Pretty simple, right? This is the most basic level: match a keyword, reply with a template. This is exactly how the early version of NEXI worked, before I integrated it with AI.
Step 4: Level Up — Connect It to AI (Optional)
If you want the bot to be smarter and able to answer unexpected questions (not just keyword matches), you can connect it to an LLM. For NEXI, I use the Groq API with a LLaMA model, since it's fast and has a decent free tier.
Here's a simplified example:
const Groq = require('groq-sdk');
const groq = new Groq({ apiKey: process.env.GROQ_API_KEY });
client.on('message', async (message) => {
const response = await groq.chat.completions.create({
messages: [
{ role: 'system', content: 'You are a friendly, concise IT helpdesk assistant.' },
{ role: 'user', content: message.body },
],
model: 'llama-3.1-8b-instant',
});
await message.reply(response.choices[0].message.content);
});
With this in place, the bot can "think" of its own answers instead of just matching keywords. Just be careful about cost and rate limits once traffic picks up — consider storing conversation history so context carries over, and cap response length to save on tokens.
Storing Data with SQLite (So Nothing Gets Lost)
One thing I learned through trial and error building NEXI: store conversation logs and important data in a local database, not just in memory. I use SQLite because it's lightweight and doesn't require setting up a separate database server:
npm install better-sqlite3
const Database = require('better-sqlite3');
const db = new Database('chatbot.db');
db.exec(`
CREATE TABLE IF NOT EXISTS logs (
id INTEGER PRIMARY KEY AUTOINCREMENT,
number TEXT,
message TEXT,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
)
`);
// Log every incoming message
client.on('message', (message) => {
db.prepare('INSERT INTO logs (number, message) VALUES (?, ?)').run(message.from, message.body);
});
This turns out to be really useful for tracking which questions come up most often — I can see what's bothering the team most without having to ask manually.
Tips From Running This Day-to-Day
- Don't run the bot on a personal laptop that sleeps often — if it needs to run 24/7, consider a small VPS or a Raspberry Pi that stays on
- Back up the
LocalAuthsession folder — if this folder is lost, you'll need to re-scan the QR code and lose your session history - Restrict who can "retrain" the bot if you add admin commands, to prevent misuse
- Monitor rate limiting — WhatsApp can flag a bot that replies too quickly and repeatedly to many different numbers
Closing Thoughts
This is just the basic foundation. NEXI itself has since grown into a system that handles many categories of IT questions, keeps ticket history, and integrates with my team's workflow. But it all started from code as simple as what's above.
If you need a similar bot for your business or team — automated customer service, order notifications, or an internal helpdesk — I'm open for freelance work. Check out my portfolio at april-portfolio-pearl.vercel.app, or reach out via the Contact page.
Have questions about the setup above? Leave a comment and I'll help out.

Tidak ada komentar:
Posting Komentar