0

所以我一直在关注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);
    }
  }); 
}

4

2 回答 2

1
  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。

于 2021-09-07T07:31:54.067 回答
0

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

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

快乐编码!

于 2020-06-30T10:45:34.450 回答