gpt4 book ai didi

javascript - Facebook Messenger 机器人问题

转载 作者:行者123 更新时间:2023-11-30 20:07:23 25 4
gpt4 key购买 nike

我制作了一个 messanager 聊天机器人并第一次尝试部署它,在与各种错误作斗争之后,我确实连接了我的页面、应用程序和 Hook 。

Tough al 似乎在工作,但机器人没有任何响应。这是我在错误日志中得到的。

我发送“嗨”,但没有回复。当我用谷歌搜索出现的响应错误时,没有适合我的解决方案。

enter image description here

    '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);
}
});
}

最佳答案

POST 路由器有问题。 'webhook_event' 在条件 block 内的 foreach block 内声明,因此它的范围在该 block 内部。要解决此问题,您应重写代码以匹配范围。这是错误的路由器(我添加了一些评论 =

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);
}

});

关于javascript - Facebook Messenger 机器人问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52759304/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com