SendAPI收不到Facebook Messenger平台发送的消息

Not receiving the message sent by Facebook's Messenger platform by the SendAPI

所以我一直在关注Quick Start guide on Facebook's Messenger Platform。一切都完美运行。 但是,我没有收到从 Messenger 的 SendAPI 发送的消息。控制台日志显示消息已发送,但我未在 Messenger 平台上收到消息。除了 Token 密钥外,我没有更改指南给出的大部分代码。

'use strict';

const PAGE_ACCESS_TOKEN = process.env.key_one;

// Imports dependencies and set up http server
const request = require('request');

const
  express = require('express'),
  bodyParser = require('body-parser'),
  app = express().use(bodyParser.json()); // creates express http server

// Sets server port and logs message on success
app.listen(process.env.PORT || 1337, () => console.log('webhook is listening'));



// Creates the endpoint for our webhook 
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
      let webhook_event = entry.messaging[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);
      }
    });

    // 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);
  }

});



// Adds support for GET requests to our webhook
app.get('/webhook', (req, res) => {

  // Your verify token. Should be a random string.
  let VERIFY_TOKEN = process.env.key_one;
    
  // 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);      
    }
  }
});



// 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!`
        }
    }  
  
    // Sends the response message
    callSendAPI(sender_psid, response);  
}

// Handles messaging_postbacks events
function handlePostback(sender_psid, received_postback) {

}

function callSendAPI(sender_psid, response) {
  // Construct the message body
  let request_body = {
    "recipient": {
      "id": sender_psid
    },
    "message": response
  }

  // Send the HTTP request to the Messenger Platform
  request({
    "uri": "https://graph.facebook.com/v2.6/me/messages",
    "qs": { "access_token": process.env.key_one },
    "method": "POST",
    "json": request_body
  }, (err, res, body) => {
    if (!err) {
      console.log('message sent!')
    } else {
      console.error("Unable to send message:" + err);
    }
  }); 
}

好的,我修好了。我实际上是 重用 webhook 验证密钥和 facebook 页面的令牌。 一旦我确定它正在回复。 :)

注意:Webhook Token 和 Page Token 是不同的!您自己创建一个 供 Facebook 访问您的服务器。而另外一个是Facebook生成的供你的Server访问Facebook的Server

编码愉快!

  1. 一般**MY_VERIFY_TOKEN**=Your Custom made Token (e.g:hulululu123) 和'PAGE_ACCESS_TOKEN=Facebook 为开发人员生成的令牌 > Messenger > 设置 > 访问令牌 > 生成令牌'

  1. 您必须将 **MY_VERIFY_TOKEN**=Your Custom made Token (e.g:hulululu123) 放在 Facebook for Developers > Messenger > Settings > Webhooks > Edit Callback Url > Verify Token 和 Verify

  2. 您必须在 .env 和 Heroku > Project > Settings > Config Vars 或您正在使用的任何其他服务器中声明它们。

  3. 有关详细信息,请访问 Facebook for developers Quick Start Guide 或私信我。