我一直在尝试逐步按照本指南(https://developers.facebook.com/docs/messenger-platform/getting-started/app-setup)进行一个简单的node js facebook chatbot。我已经使用curl和postman测试了后端,并且工作正常,我将Web应用程序订阅了webhook,但是一旦我从facebook.com或Messenger中向页面发送消息,Facebook就不会将消息发送到后端。我期望req.body.entry.messaging.message
有一定的价值,但Facebook不会发送任何东西。
https://github.com/cristiantorresf/ChatBotLearning
'use strict';
// Imports dependencies and set up http server
const
express = require('express'),
bodyParser = require('body-parser'),
app = express().use(bodyParser.json());
// token variable de entorno
const PAGE_ACCESS_TOKEN = process.env.PAGE_ACCESS_TOKEN;
// Adds support for GET requests to our webhook verificaion
app.get('/webhook', (req, res) => {
// Your verify token. Should be a random string.
let VERIFY_TOKEN = "1014238588"
// Parse the query params
// endpoint?hub.mode=subscribe&hub.verify_token=1014238588&hub.challenge=nose
let mode = req.query['hub.mode'];
let token = req.query['hub.verify_token'];
let challenge = req.query['hub.challenge'];
// Checks if a token and mode is in the query string of the request
if (mode && token) {
// Checks the mode and token sent is correct
if (mode === 'subscribe' && token === VERIFY_TOKEN) {
// Responds with the challenge token from the request
console.log('WEBHOOK_VERIFIED');
res.status(200).send(challenge);
} else {
// Responds with '403 Forbidden' if verify tokens do not match
res.sendStatus(403);
}
}
});
// Creates the endpoint for our webhook recive mensajes
app.post('/webhook', (req, res) => {
let body = req.body;
// Checks this is an event from a page subscription
if (body.object === 'page') {
// Iterates over each entry - there may be multiple if batched
body.entry.forEach(function(entry) {
// Gets the message. entry.messaging is an array, but
// will only ever contain one message, so we get index 0
// req.body.entry
let webhook_event = entry.messaging[0];
console.log(webhook_event);
});
// Returns a '200 OK' response to all requests
res.status(200).send('EVENT_RECEIVED');
} else {
// Returns a '404 Not Found' if event is not from a page subscription
res.sendStatus(404);
}
});
// creates express http server
// Sets server port and logs message on success
app.listen(process.env.PORT || 1337, () => console.log('webhook is listening'));