我想在messenger bot中检测快速回复有效负载但是得到错误 - nodejs

时间:2017-08-06 20:11:43

标签: javascript json node.js facebook-messenger-bot

我使用带有快速回复按钮的nodejs制作messenger bot但我不知道如何检测快速回复有效负载

我使用了这段代码

app.post('/webhook', function (req, res) {
  console.log(req.body);
  var data = req.body;

  if (data.object === 'page') {


    data.entry.forEach(function(entry) {
      var pageID = entry.id;
      var timeOfEvent = entry.time;

      entry.messaging.forEach(function(event) {
        var QrPayload = event.message.quick_reply.payload;  //Quick Reply 
        if (QrPayload) {
          receivedQReply(QrPayload);    //Quick Reply call to Function
        }
        if (event.message) {
          receivedMessage(event);
        } else if (event.postback) {
          receivedPostback(event);   
        } else {
          console.log("Webhook received unknown event: ", event);
        }
      });
    });

    res.sendStatus(200);
  }
});

这是JSON等于" var data"我的代码必须检测quick_reply =>有效载荷

{  
   "object":"page",
   "entry":[  
      {  
         "id":"249154065570606",
         "time":1502049701146,
         "messaging":[  
            {  
               "sender":{  
                  "id":"1253233841465195"
               },
               "recipient":{  
                  "id":"249154065570606"
               },
               "timestamp":1502049681094,
               "message":{  
                  "quick_reply":{  
                     "payload":"fp_2"
                  },
                  "mid":"mid.$cAAC4aS96rI1j6miGxlduSMyrB0ir",
                  "seq":1198384,
                  "text":"More"
               }
            }
         ]
      }
   ]
}

我的代码

出现此错误
  

TypeError:无法读取属性' quick_reply'未定义的           在/home/ubuntu/workspace/index.js:241:30           at Array.forEach(native)           在/home/ubuntu/workspace/index.js:240:23           at Array.forEach(native)           在/home/ubuntu/workspace/index.js:239:16           在Layer.handle [as handle_request](/home/ubuntu/workspace/node_modules/express/lib/router/layer.js:95:5)           在下一个(/home/ubuntu/workspace/node_modules/express/lib/router/route.js:137:13)           在Route.dispatch(/home/ubuntu/workspace/node_modules/express/lib/router/route.js:112:3)           在Layer.handle [as handle_request](/home/ubuntu/workspace/node_modules/express/lib/router/layer.js:95:5)           at /home/ubuntu/workspace/node_modules/express/lib/router/index.js:281:22

我该如何解决我的问题?或其他代码可以获得我的messenger bot的快速回复有效负载

0 个答案:

没有答案