Using WhatsApp with your local LLM (Large Language Model)—like via a tool called WhatsApp MC (possibly referring to "Message Connector" or a similar bridge)—requires bridging WhatsApp’s communication protocol with your locally hosted LLM. Since WhatsApp doesn’t offer a public API for personal use anymore (WhatsApp Business API is costly and limited), most developers use unofficial methods like WhatsApp Web automation or tools such as pywhatkit, whatsapp-web.js, or Yowsup (although many of these are getting harder to maintain due to WhatsApp's restrictions).
Here’s a step-by-step guide on how to set this up using whatsapp-web.js and local LLM (like LLaMA, GPT-Neo, or Mistral running locally via Ollama, LM Studio, or Text Generation Web UI).
✅ Prerequisites
- Node.js installed
- Python (optional, if using local LLM APIs)
- Local LLM running (e.g., via Ollama, LM Studio, or Text Generation Web UI)
- WhatsApp account
- A phone connected to WhatsApp (for QR code scanning)
🔧 Step 1: Set Up Your Local LLM
You can skip this if you already have a local LLM running.
Using Ollama (Recommended):
curl -fsSL https://ollama.com/install.sh | sh
ollama run llama3
This will download and run the LLaMA3 model locally.
Test it:
curl http://localhost:11434/api/generate \
-d '{"model":"llama3","prompt":"Hello, who are you?"}'
📲 Step 2: Set Up WhatsApp Web Bridge (whatsapp-web.js)
1. Create a new folder and initialize Node.js project:
mkdir whatsapp-llm-bot
cd whatsapp-llm-bot
npm init -y
2. Install required packages:
npm install whatsapp-web.js qrcode-terminal express body-parser axios
qrcode-terminal shows the QR code in terminal; alternatively, you can use qrcode-reader + browser pop-up.
3. Create index.js file:
const { Client, LocalAuth } = require('whatsapp-web.js');
const qrcode = require('qrcode-terminal');
const express = require('express');
const bodyParser = require('body-parser');
const axios = require('axios');
const app = express();
app.use(bodyParser.json());
// Start Express server for local LLM API proxy (optional)
app.post('/ask', async (req, res) => {
const userMessage = req.body.message;
// Replace with your actual local LLM endpoint
const response = await axios.post('http://localhost:11434/api/generate', {
model: 'llama3',
prompt: userMessage,
stream: false
});
const reply = response.data.response;
res.json({ reply });
});
app.listen(3000, () => console.log("LLM API Proxy running on port 3000"));
// WhatsApp Client
const client = new Client({
authStrategy: new LocalAuth()
});
client.on('qr', qr => {
qrcode.generate(qr, { small: true });
});
client.on('ready', () => {
console.log('WhatsApp is ready!');
});
// Handle incoming messages
client.on('message_create', msg => {
if (msg.body && !msg.fromMe) {
console.log(`Received: ${msg.body}`);
// Send message to local LLM API
axios.post('http://localhost:3000/ask', { message: msg.body })
.then(response => {
const reply = response.data.reply;
client.sendMessage(msg.from, reply);
})
.catch(err => {
console.error(err);
client.sendMessage(msg.from, "Sorry, I couldn't process that.");
});
}
});
client.initialize();
Wait — small caveat: message_create is not the correct event. The correct one is message. Let me fix that 👇
Replace:
client.on('message_create', ...)
With:
client.on('message', msg => {
if (msg.body && !msg.fromMe && msg.type === 'chat') {
console.log(`Received: ${msg.body}`);
axios.post('http://localhost:3000/ask', { message: msg.body })
.then(response => {
const reply = response.data.reply;
client.sendMessage(msg.from, reply);
})
.catch(err => {
console.error(err);
client.sendMessage(msg.from, "Sorry, I couldn't process that.");
});
}
});
🧪 Step 3: Run Everything
In your terminal:
node index.js
- Scan the QR code with your WhatsApp phone app.
- Once connected, send a message to your bot number.
- It will forward the message to your local LLM and return the response.
🛠️ Alternative: Use Webhook Instead of Express
If you're running a local LLM UI like LM Studio or TextGen WebUI, some support HTTP API already. You may not need Express — just call their internal endpoints directly from Node.js.
For example, TextGen WebUI supports:
POST http://localhost:7860/api/v1/completions
You’ll need to match the prompt format they expect.
🤖 Bonus: Make It Smarter
- Add system prompts to control behavior.
- Stream responses using WebSockets (if supported).
- Add memory using SQLite or vector DB (like FAISS).
- Use OpenAI-compatible APIs via local adapters like GPT4All, llamafile, etc.
⚠️ Notes & Caveats
- WhatsApp Web automation can break if WhatsApp updates its UI.
- For production, consider using WhatsApp Business API (official but costly).
- Rate-limiting & abuse protection may apply.
- Never share sensitive data over WhatsApp unless encrypted.
✅ Summary
| Step | Description |
|---|
| 1 | Set up local LLM (Ollama / LM Studio) |
| 2 | Create Node.js app with whatsapp-web.js |
| 3 | Connect WhatsApp via QR code |
| 4 | Forward messages to local LLM |
| 5 | Return LLM response to sender |
Would you like a version using Python instead of Node.js? Or instructions for using WhatsApp Business API with your LLM?