Я слежу за уроком прямо с Facebook и отправляю тестовые события с их консоли. (Получено в POST) Я успешно передаю событие в webhook, но объект body с данными о событии не определен. Я использую анализатор тела, как показано на них. Должен ли я сделать это по-другому?
'use strict';
// Imports dependencies and set up http server
const
express = require('express'),
bodyParser = require('body-parser'),
request = require('request'),
app = express().use(bodyParser.json()); // creates express http server
const PORT = process.env.PORT;
const PAGE_TOKEN = process.env.PAGE_TOKEN;
// Sets server port and logs message on success
app.listen(PORT, () => console.log(`Server is listening on port ${PORT}`));
// Adds support for GET requests to our webhook
app.get('/webhook', (req, res) => {
// Your verify token. Should be a random string.
let VERIFY_TOKEN = "hello_token_success"
// Parse the query params
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);
}
}
});
app.post('/webhook', (req, res) => {
console.log(req);
// Parse the request body from the POST
let body = req.body;
console.log('Body: ' + body);
let bodyObject = body.object;
console.log('Object: ' + bodyObject);
let webhookEntry = body.entry;
console.log('Entry: ' + webhookEntry);
// Check the webhook event is from a Page subscription
if (body.object === 'page') {
// Iterate over each entry - there may be multiple if batched
body.entry.forEach(function(entry) {
console.log('Entry inside forEach: ' + entry);
console.log('Object of Entry: ' + entry.object);
// Get the webhook event. entry.messaging is an array, but
// will only ever contain one event, so we get index 0
//let webhook_event = entry.messaging[0];
// let webhook_event = entry.conversations[0];
// console.log(webhook_event);
// // Get the sender PSID
// let sender_psid = webhook_event.sender.id;
// console.log('Sender PSID: ' + sender_psid);
// // Check if the event is a message or postback and
// // pass the event to the appropriate handler function
// if (webhook_event.message) {
// handleMessage(sender_psid, webhook_event.message);
// } else if (webhook_event.postback) {
// handlePostback(sender_psid, webhook_event.postback);
// }
});
// Return a '200 OK' response to all events
res.status(200).send('EVENT_RECEIVED');
} else {
// Return a '404 Not Found' if event is not from a page subscription
res.sendStatus(404);
}
});