json - 处理从 Facebook Messenger 传递的 JSON 消息

标签 json node.js facebook-messenger chatbot facebook-chatbot

您好,我正在 try catch Facebook Messenger 为我的机器人传递的 JSON 响应消息,以便在我知道图像库已渲染时可以发送后续消息。

我已阅读 ( https://developers.facebook.com/docs/messenger-platform/webhook-reference/message-delivered ) 并且可以看到消息传递的 JSON。

{
   "sender":{
      "id":"USER_ID"
   },
   "recipient":{
      "id":"PAGE_ID"
   },
   "delivery":{
      "mids":[
         "mid.1458668856218:ed81099e15d3f4f233"
      ],
      "watermark":1458668856253,
      "seq":37
   }
}

我也订阅了消息传递 webhook。

我已经编写了代码来捕获消息传递 JSON,但没有运气:

// handler receiving messages
app.post('/webhook', function (req, res) {

     if(req.hasOwnProperty('watermark')){
       console.log('message delivery found');
     }
}

任何帮助将不胜感激。

最佳答案

我只是发表评论,但我是新来的:)

我不确定您是否使用 bodyparser,但我会像下面这样检查 req.body

// add this to the app file

var bodyParser = require('body-parser');
app.use(bodyParser.json());

app.post('/webhook', function (req, res) {
    console.log(req.body); // inspect the body data
    if(req.body.delivery && req.body.delivery.watermark)
        console.log('message delivery found');
    }
}

关于json - 处理从 Facebook Messenger 传递的 JSON 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40334417/

相关文章:

json - 获取 package.json 文件中另一个字段的值(npm)

c# - 将 JSON 反序列化为对象,然后运行 ​​foreach 循环以添加到 ListView 中

javascript - Node.js Web 应用程序 API 调用结果为 ENOTFOUND

node.js - 删除 Google Cloud Functions 的 tmp 文件夹中的文件

wordpress - 拒绝显示..... frame-ancestors https ://www. facebook.com

webhooks - Facebook Messenger webhook 由按钮点击触发,但不是由短信触发

jquery - 如何通过json发布来发布多个数据?

c# - 使用 Newtonsoft json.net 在 C# 中反序列化 JSON 对象

css - 加载本地服务器时样式不连接

facebook-messenger - 自定义负载不适用于 Messenger