首页 > 解决方案 > SendAPI 收不到 Facebook Messenger 平台发送的消息

问题描述

所以我一直在关注Facebook Messenger Platform 上的快速入门指南。一切运行完美。但是,我没有收到从 Messenger 的 SendAPI 发送的消息。控制台日志显示消息已发送,但我在信使平台上没有收到消息。除了 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);
    }
  }); 
}

标签: javascriptfacebookrequestfacebook-messenger-botfacebook-send-api

解决方案


  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 and Verify

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

  3. 欲了解更多信息,请访问Facebook 开发人员快速入门指南 或 dm me。


推荐阅读