我制作了一个Messenger聊天机器人,并首次尝试部署它,在解决了各种错误之后,我确实连接了页面,应用程序和挂钩。
强硬的人似乎可以正常工作,但该机器人没有任何反应。这就是我在错误日志中得到的内容。
我发“ hi”,什么也没有发回来。当我用Google搜索出现的响应错误时,没有适合我的解决方案。
'use strict'
const
express=require('express'),
bodyParser = require('body-parser'),
app=express().use(bodyParser.json()); //creates http server
app.listen(process.env.PORT || 5000, () =>console.log('webhook is listening'));
app.post('/webhook', (req, res) => {
let body=req.body;
if(body.object === 'page'){
body.entry.forEach(function(entry){
//Gets the body of the webhook
let webhook_event=entry.messaging[0];
console.log(webhook_event);
//Gets the sender PSID
let sender_psid=webhook_event.sender.id;
console.log('Sender PSID: ' + sender_psid);
});
res.status(200).send('EVENT_RECEIVED');
}else{
res.sendStatus(404);
}
if(webhook_event.message){
handleMessage(sender_psid, webhook_event.message);
}else if(webhook_event.postback){
handlePostback(sender_psid, webhook_event.postback);
}
});
app.get('/', function (req, res) {
res.send('This is EngiBot Server');
});
app.get('/webhook', (req, res) => {
let VERIFY_TOKEN = "testbot_verify_token"
let mode= req.query['hub.mode'];
let token=req.query['hub.verify_token'];
let challange = req.query['hub.challange'];
if (req.query['hub.verify_token'] === VERIFY_TOKEN) {
res.send(req.query['hub.challenge']);
} else {
res.send('Invalid verify token');
}
if(mode && token){
if(mode==='subscribe' && token === VERIFY_TOKEN){
console.log('WEBHOOK_VERIFIED');
res.status(200).send(challange);
}else{
res.sendStatus(403);
}
}
});
function handleMessages(sender_psid, received_message){
let response;
if(received_message.text){
response = {
"text": 'You sent the message: "${received_message.text}". Now send an image!'
}
}else if(received_message.attachments){
let attachment_url=received_message.attachments[0].payload.url;
response = {
"attachment":{
"type": "template",
"payload":{
"template_type":"generic",
"elements": [{
"title": "Is this the right picture?",
"subtitle": "Tap a button to answer.",
"image_url": attachment_url,
"buttons": [
{
"type": "postback",
"title": "Yes!",
"payload":"yes",
},
{
"type": "postback",
"title": "No!",
"payload": "no",
}
],
}]
}
}
}
}
callSendAPI(sender.psid, response);
}
function handlePostback(sender_psid, received_postback){
let response;
let payload=received_postback.payload;
if(payload==='yes'){
response = {"text": "Thanks!"}
}else if (payload==="no"){
response ={"text": "Oops, try sending another image."}
}
callSendAPI(sender_psid, response);
}
function callSendAPI(sender_psid, response){
let request_body={
"recipient": {
"id": sender_psid
},
"message": response
}
request({
"uri":"",
"qs":{"access_token": PAGE_ACCESS_TOKEN},
"method": "POST",
"json": request_body
}, (err, res, body)=>{
if(!err){
console.log('message sent!')
}else {
console.error("Unable to send message:" + err);
}
});
}
答案 0 :(得分:1)
POST路由器出现问题。 “ webhook_event”在条件块内的foreach块内声明,因此其作用域在该块内。为了解决这个问题,您将重写代码以匹配范围。这是错误的路由器(我添加了一些注释=
app.post('/webhook', (req, res) => {
let body=req.body;
// webhook_event == null -> true
if(body.object === 'page'){
body.entry.forEach(function(entry){
//Gets the body of the webhook
let webhook_event=entry.messaging[0]; // webhook_event declared // webhook_event == null -> false
console.log(webhook_event);
//Gets the sender PSID
let sender_psid=webhook_event.sender.id;
console.log('Sender PSID: ' + sender_psid);
});
res.status(200).send('EVENT_RECEIVED');
if(webhook_event.message){ // ReferenceError cause is not defined
handleMessage(sender_psid, webhook_event.message);
}else if(webhook_event.postback){ // ReferenceError cause is not defined
handlePostback(sender_psid, webhook_event.postback);
}
}else{
res.sendStatus(404);
}
});