{"slug": "production-facebook-messenger-ai-sales-bot-webhook-server-in-node-js-express-e", "title": "Production Facebook Messenger AI Sales Bot Webhook Server in Node.js / Express | Sajedar E-Commerce Automation", "summary": "A developer published a Node.js/Express webhook server for a Facebook Messenger AI sales bot aimed at e-commerce pages, implementing Meta Graph API verification, messaging event handling, and quick-reply dispatch within Meta's 20-second acknowledgement window. The code includes signature-ready raw body capture, a Send API helper targeting Graph API v19.0, and keyword-triggered replies with Nepali-language price handling.", "body_md": "|  | /** | \n|  | * Production Facebook Messenger Webhook Server | \n|  | * Implements Meta Graph API Verification, Messaging Event Receiver, | \n|  | * and 2-second Quick Reply Dispatch for E-commerce Pages. | \n|  | * | \n|  | * 📖 Complete Tutorial & Setup Guide: | \n|  | * https://www.sajedar.com/tutorials/facebook-messenger-chatbot-ecommerce | \n|  | * | \n|  | * 🤖 Full AI Sales Agent Capabilities in Nepal: | \n|  | * https://www.sajedar.com/facebook-messenger-ai-chatbot-nepal | \n|  | * | \n|  | * 🛠️ Messenger JSON Payload Builder Tool: | \n|  | * https://www.sajedar.com/tools/messenger-json-builder | \n|  | */ | \n|  |  | \n|  | const express = require('express'); | \n|  | const crypto = require('crypto'); | \n|  | const app = express(); | \n|  |  | \n|  | app.use(express.json({ | \n|  | verify: (req, res, buf) => { | \n|  | req.rawBody = buf; | \n|  | } | \n|  | })); | \n|  |  | \n|  | const PAGE_ACCESS_TOKEN = process.env.PAGE_ACCESS_TOKEN; | \n|  | const VERIFY_TOKEN = process.env.VERIFY_TOKEN; | \n|  | const APP_SECRET = process.env.APP_SECRET; | \n|  |  | \n|  | // 1. Meta Webhook Verification Endpoint (GET) | \n|  | app.get('/webhook', (req, res) => { | \n|  | const mode = req.query['hub.mode']; | \n|  | const token = req.query['hub.verify_token']; | \n|  | const challenge = req.query['hub.challenge']; | \n|  |  | \n|  | if (mode === 'subscribe' && token === VERIFY_TOKEN) { | \n|  | console.log('✅ Webhook verified by Meta Graph API'); | \n|  | res.status(200).send(challenge); | \n|  | } else { | \n|  | res.sendStatus(403); | \n|  | } | \n|  | }); | \n|  |  | \n|  | // 2. Message Event Receiver (POST) | \n|  | app.post('/webhook', (req, res) => { | \n|  | const body = req.body; | \n|  |  | \n|  | if (body.object === 'page') { | \n|  | body.entry.forEach(entry => { | \n|  | const webhookEvent = entry.messaging?.[0]; | \n|  | if (!webhookEvent) return; | \n|  |  | \n|  | const senderPsid = webhookEvent.sender.id; | \n|  |  | \n|  | if (webhookEvent.message) { | \n|  | handleIncomingMessage(senderPsid, webhookEvent.message); | \n|  | } else if (webhookEvent.postback) { | \n|  | handlePostback(senderPsid, webhookEvent.postback); | \n|  | } | \n|  | }); | \n|  |  | \n|  | // Acknowledge receipt within 20 seconds to prevent Meta retries | \n|  | res.status(200).send('EVENT_RECEIVED'); | \n|  | } else { | \n|  | res.sendStatus(404); | \n|  | } | \n|  | }); | \n|  |  | \n|  | // Send Message API helper | \n|  | async function callSendAPI(senderPsid, responsePayload) { | \n|  | const url = 'https://graph.facebook.com/v19.0/me/messages?access_token=' + PAGE_ACCESS_TOKEN; | \n|  |  | \n|  | try { | \n|  | const res = await fetch(url, { | \n|  | method: 'POST', | \n|  | headers: { 'Content-Type': 'application/json' }, | \n|  | body: JSON.stringify({ | \n|  | recipient: { id: senderPsid }, | \n|  | messaging_type: 'RESPONSE', | \n|  | message: responsePayload | \n|  | }) | \n|  | }); | \n|  | const data = await res.json(); | \n|  | return data; | \n|  | } catch (err) { | \n|  | console.error('Error calling Send API:', err); | \n|  | } | \n|  | } | \n|  |  | \n|  | async function handleIncomingMessage(senderPsid, receivedMessage) { | \n|  | const text = receivedMessage.text?.toLowerCase() \\|\\| ''; | \n|  |  | \n|  | // Handle Quick Inquiries | \n|  | if (text.includes('price') \\|\\| text.includes('kati ho') \\|\\| text.includes('cost')) { | \n|  | await callSendAPI(senderPsid, { | \n|  | text: 'Namaste! 🙏 Our bestsellers start at Rs. 1,499 with free delivery inside Kathmandu Valley. Would you like to see available colors or sizes?', | \n|  | quick_replies: [ | \n|  | { content_type: 'text', title: '👗 View Catalog', payload: 'VIEW_CATALOG' }, | \n|  | { content_type: 'text', title: '📍 Delivery Info', payload: 'DELIVERY_INFO' }, | \n|  | { content_type: 'text', title: '💬 Human Agent', payload: 'TALK_HUMAN' } | \n|  | ] | \n|  | }); | \n|  | } else { | \n|  | await callSendAPI(senderPsid, { | \n|  | text: 'Thank you for reaching out! How can we help your shopping today?' | \n|  | }); | \n|  | } | \n|  | } | \n|  |  | \n|  | const PORT = process.env.PORT \\|\\| 3000; | \n|  | app.listen(PORT, () => console.log('Messenger Webhook running on port ' + PORT)); |", "url": "https://wpnews.pro/news/production-facebook-messenger-ai-sales-bot-webhook-server-in-node-js-express-e", "canonical_source": "https://gist.github.com/Dimanjan/875ba9ea9c1af0f2047e08e134efb785", "published_at": "2026-09-18 15:38:28+00:00", "updated_at": "2026-10-04 05:38:06.695399+00:00", "lang": "en", "topics": ["ai-agents", "ai-products", "developer-tools", "natural-language-processing"], "entities": ["Meta", "Facebook Messenger", "Node.js", "Express", "Sajedar", "Meta Graph API"], "also_reported_by": [], "alternates": {"html": "https://wpnews.pro/news/production-facebook-messenger-ai-sales-bot-webhook-server-in-node-js-express-e", "markdown": "https://wpnews.pro/news/production-facebook-messenger-ai-sales-bot-webhook-server-in-node-js-express-e.md", "text": "https://wpnews.pro/news/production-facebook-messenger-ai-sales-bot-webhook-server-in-node-js-express-e.txt", "jsonld": "https://wpnews.pro/news/production-facebook-messenger-ai-sales-bot-webhook-server-in-node-js-express-e.jsonld"}}