How I Built a WhatsApp Translation Bot Using Node.js
In a world where instant communication is key, language barriers can be a major challenge. So, I built a WhatsApp translation bot that automatically detects and translates messages between English and Malayalam. This bot uses Node.js, whatsapp-web.js, translate-google, and franc for language detection and translation.
In this article, I’ll walk you through how I built it — step by step — with the exact code I used. If you’re looking to create your own WhatsApp translator, this guide will help you get started.
🔹 Step 1: Installing the Required Dependencies
Before writing the code, we need to install a few essential Node.js libraries. Open your terminal and run:
npm install whatsapp-web.js qrcode-terminal translate-google franc
Here’s what each package does:
- whatsapp-web.js — Allows us to automate WhatsApp messages.
- qrcode-terminal — Displays a QR code for authentication.
- translate-google — Enables text translation using Google Translate.
- franc — Detects the language of a given text.
Once installed, we can move on to writing the bot’s logic.
🔹 Step 2: Writing the WhatsApp Bot Code
Now, let’s create a file called index.js
and paste the following code:
const { Client, LocalAuth } = require('whatsapp-web.js');
const client = new Client({
authStrategy: new LocalAuth({
dataPath: '.wwebjs_auth'
})
});
const qrcode = require('qrcode-terminal');
client.on('ready', () => {
console.log('✅ Client is ready!');
});
client.on('qr', qr => {
qrcode.generate(qr, { small: true });
});
client.initialize();
// Importing the translation and language detection modules
const translate = require('translate-google');
client.on('message_create', async (message) => {
if (message.fromMe) return; // Ignore messages sent by the bot
const franc = await import('franc').then(module => module.franc);
let langCode = franc(message.body);
// Assume English for short text if franc returns "und"
if (langCode === 'und' && /^[A-Za-z0-9\s.,!?"'()]+$/.test(message.body)) {
langCode = 'eng';
}
const malayalamCodes = ['ml', 'mal']; // Codes for Malayalam
if (langCode === 'eng') {
console.log("✅ Detected English. Translating to Malayalam...");
try {
const translatedText = await translate(message.body, { to: 'ml' });
console.log("📌 Translation:", translatedText);
await message.reply(translatedText);
} catch (error) {
console.error("❌ Translation error:", error);
await message.reply("Sorry, I couldn't translate that.");
}
} else if (malayalamCodes.includes(langCode)) {
console.log("✅ Detected Malayalam. Translating to English...");
try {
const translatedText = await translate(message.body, { to: 'en' });
console.log("📌 Translation:", translatedText);
await message.reply(translatedText);
} catch (error) {
console.error("❌ Translation error:", error);
await message.reply("Sorry, I couldn't translate that.");
}
} else {
console.log("🚫 Unsupported language detected.");
}
console.log("🔍 Detected language code:", langCode);
});
💡 Breaking Down the Code:
- Authentication: Uses
whatsapp-web.js
with local storage (LocalAuth
) to keep the session active. - QR Code Login: On first-time setup, a QR code appears in the terminal. Scan it with WhatsApp to link the bot.
- Message Listener: The bot listens for new messages and ignores its own messages to avoid infinite loops.
- Language Detection: The
franc
library determines whether the message is in English or Malayalam. - Translation Logic:
- English → Malayalam
- Malayalam → English
- Other languages are ignored (but can be expanded).
6. Error Handling: If translation fails, the bot informs the user.
🔹 Step 3: Running the Bot for the First Time
Now, it’s time to test our bot! Simply run:
node index.js
If it’s your first time, a QR code will appear in the terminal. Open WhatsApp on your phone, go to Linked Devices, and scan the code.
Once scanned, the bot will start running and responding to messages.
🔹 Step 4: Improving the Bot (Optional Enhancements)
Now that the bot works, here are a few ways to improve it:
✅ Support More Languages: Instead of only English and Malayalam, add more languages dynamically.
✅ Deploy on a VPS: Host it on a server so it runs 24/7.
✅ Use a More Advanced Language Detector: Replace franc
with fasttext
for better accuracy.
🔹 Final Thoughts
Building this WhatsApp translation bot was a great experience, and I’ve made the complete code available on my GitHub. You can check it out here: GitHub — FAWAZMALIK/whatsapp-translation-bot.
Feel free to explore, modify, and improve it! 🚀 If you have any suggestions or want to contribute, drop by the repository and share your thoughts.