// 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') {
// Iterate over each entry - there may be multiple if batched body.entry.forEach(function(entry) {
// 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]; console.log(webhook_event);
// Get the sender PSID 获得发送者的 PSID let sender_psid = webhook_event.sender.id; console.log('Sender PSID: ' + sender_psid);
// 解析 Webhook 事件类型 // 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.game_play) { var senderId = webhook_event.sender.id; // Messenger sender id var playerId = webhook_event.game_play.player_id; // Instant Games player id var contextId = webhook_event.game_play.context_id; handleMessage(senderId, 'hello!' + playerId); }
});
// 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); }
});
// Accepts GET requests at the /webhook endpoint app.get('/webhook', (req, res) => {
// Parse params from the webhook verification request let mode = req.query['hub.mode']; let token = req.query['hub.verify_token']; let challenge = req.query['hub.challenge'];
// Check if a token and mode were sent if (mode && token) {
// Check the mode and token sent are correct if (mode === 'subscribe' && token === VERIFY_TOKEN) {
// Respond with 200 OK and 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); } } });
// Handles messages events function handleMessage(sender_psid, received_message) { let response;
// Check if the message contains text if (received_message.text) {
// Create the payload for a basic text message response = { "text": `You sent the message: "${received_message.text}". Now send me an image!` } }else{ response = { "text": `You sent the message: "${received_message}". Now send me an image!` } }
// Sends the response message callSendAPI(sender_psid, response); }
// Sends response messages via the Send API // 通过发送 API 发送消息 function callSendAPI(sender_psid, response) { // Construct the message body let request_body = { "recipient": { "id": sender_psid }, "message": response } console.log('request_body :' + JSON.stringify(request_body)) // Send the HTTP request to the Messenger Platform request({ "uri": "https://graph.facebook.com/v2.6/me/messages", "qs": { "access_token": PAGE_ACCESS_TOKEN }, "method": "POST", "json": request_body }, (err, res, body) => { if (!err) { console.log('message sent!') } else { console.error("Unable to send message:" + err); } }); }