|
messenger职能助手用官方的js代码'use strict';
const PAGE_ACCESS_TOKEN = process.env.PAGE_ACCESS_TOKEN;
// Imports dependencies and set up http server
const
request = require('request'),
express = require('express'),
body_parser = require('body-parser'),
app = express().use(body_parser.json()); // creates express http server
// Sets server port and logs message on success
app.listen(process.env.PORT || 1337, () => console.log('webhook is listening'));
// Accepts POST requests at /webhook endpoint
app.post('/webhook', (req, res) => {
// Parse the request body from the POST
let body = req.body;
// Check the webhook event is from a Page subscription
if (body.object === 'page') {
body.entry.forEach(function(entry) {
// Gets the body of the webhook event
let webhook_event = entry.messaging[0];
console.log(webhook_event);
// Get the sender PSID
let sender_psid = webhook_event.sender.id;
console.log('Sender ID: ' + 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);
}
});
每次有json数据时,内置的JSON解析就会报错:
SyntaxError: Unexpected token ' in JSON at position 0
at JSON.parse (<anonymous>)
at createStrictSyntaxError (F:\facebook_game\webhook\node_modules\body-parser\lib\types\json.js:158:10)
at parse (F:\facebook_game\webhook\node_modules\body-parser\lib\types\json.js:83:15)
at F:\facebook_game\webhook\node_modules\body-parser\lib\read.js:121:18
at invokeCallback (F:\facebook_game\webhook\node_modules\raw-body\index.js:224:16)
at done (F:\facebook_game\webhook\node_modules\raw-body\index.js:213:7)
at IncomingMessage.onEnd (F:\facebook_game\webhook\node_modules\raw-body\index.js:273:7)
at IncomingMessage.emit (events.js:182:13)
at endReadableNT (_stream_readable.js:1094:12)
at process._tickCallback (internal/process/next_tick.js:63:19)
有朋友遇到过这种问题吗?
|
|