Facebook Messenger Bot Development Guide
Overview#
Facebook Messenger bots allow you to create interactive experiences for your customers. This guide covers everything from setup to advanced features.
Prerequisites#
- Facebook Developer Account
- Node.js and npm
- ngrok for local development
Step 1: Create Facebook App#
- Go to Facebook Developers
- Create a new app
- Add Messenger product
- Generate access token
Step 2: Setup Webhook#
const express = require('express');
const app = express();
app.get('/webhook', (req, res) => {
const VERIFY_TOKEN = process.env.VERIFY_TOKEN;
const mode = req.query['hub.mode'];
const token = req.query['hub.verify_token'];
const challenge = req.query['hub.challenge'];
if (mode && token) {
if (mode === 'subscribe' && token === VERIFY_TOKEN) {
res.status(200).send(challenge);
} else {
res.sendStatus(403);
}
}
});Step 3: Handle Messages#
app.post('/webhook', (req, res) => {
const body = req.body;
if (body.object === 'page') {
body.entry.forEach(entry => {
const webhookEvent = entry.messaging[0];
if (webhookEvent.message) {
handleMessage(webhookEvent);
}
});
res.status(200).send('EVENT_RECEIVED');
} else {
res.sendStatus(404);
}
});Advanced Features#
- Quick Replies
- Persistent Menu
- Typing Indicators
- Rich Media
Production Facebook Messenger Setup for Nepal#
Deploying Messenger bots for Kathmandu retail stores involves distinct regional practices:
- Deposit Collection via QR: Automate prompt dispatch of eSewa and Fonepay QR payment requests inside the chat to lock in orders and reduce COD cancellations.
- Courier API Dispatch: Connect approved Messenger orders directly to Pathao or Nepal Can Move delivery creation APIs via webhooks.
- Transparent Pricing in NPR: Always state prices in NPR upfront in welcome flows to filter casual window shoppers from serious buyers.
Conclusion#
You now have a fully functional Facebook Messenger bot!