gpt4 book ai didi

node.js - Facebook Messenger Bot API 发送多条消息并显示错误

转载 作者:太空宇宙 更新时间:2023-11-04 00:35:40 25 4
gpt4 key购买 nike

我正在使用 Facebook Messenger Bot API,并使用我的 Facebook 帐户(管理员)和一个作为测试人员添加的虚拟 Facebook 帐户来测试 Heroku 托管的 Node.js 机器人。每次我向聊天机器人发送消息时,聊天机器人都能回复我。但是当我检查 Heroku 日志时,我发现机器人正在为其收到的一条消息发送两条消息。我只收到了机器人的一条回复,但为什么机器人又发送了第二条消息?

这是 post web hook 和 sendMessage 函数的代码

app.post('/webhook', function (req, res) {
var events = req.body.entry[0].messaging;
for (i = 0; i < events.length; i++) {
var event = events[i];
var sender = event.sender.id
if (event.message && event.message.text) {
sendMessage(sender, {text: "Hey, did you just say: " + event.message.text});
console.log("sent message to: " + sender);
}
}
res.sendStatus(200);
});

function sendMessage(recipientId, message) {
request({
url: 'https://graph.facebook.com/v2.6/me/messages',
qs: {access_token: process.env.PAGE_ACCESS_TOKEN},
method: 'POST',
json: {
recipient: {id: recipientId},
message: message,
}
}, function(error, response, body) {
if (error) {
console.log('Error sending message: ', error);
} else if (response.body.error) {
console.log('Error: ', response.body.error);
}
});
};

这是我在 Heroku 中构建聊天机器人应用程序并从我的管理员帐户发送消息时的日志

2016-08-25T21:16:02.303705+00:00 heroku[web.1]: Restarting
2016-08-25T21:16:02.304448+00:00 heroku[web.1]: State changed from up to starting
2016-08-25T21:16:03.588649+00:00 heroku[web.1]: Starting process with command `node index.js`
2016-08-25T21:16:05.731964+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2016-08-25T21:16:06.609304+00:00 heroku[web.1]: Process exited with status 143
2016-08-25T21:16:07.266798+00:00 heroku[web.1]: State changed from starting to up
2016-08-25T21:16:39.746253+00:00 app[web.1]: sent message to: 1160285130676682
2016-08-25T21:16:40.082592+00:00 app[web.1]: sent message to: 284635001929051
2016-08-25T21:16:40.206265+00:00 app[web.1]: Error: { message: '(#100) No matching user found',
2016-08-25T21:16:40.206267+00:00 app[web.1]: type: 'OAuthException',
2016-08-25T21:16:40.206268+00:00 app[web.1]: code: 100,
2016-08-25T21:16:40.206268+00:00 app[web.1]: fbtrace_id: 'BSxdusFbWB6' }
2016-08-25T21:16:39.756640+00:00 heroku[router]: at=info method=POST path="/webhook" host=warm-depths-81393.herokuapp.com request_id=7035f788-f103-49d7-99ec-21cf58d30674 fwd="69.63.188.113" dyno=web.1 connect=1ms service=66ms status=200 bytes=196
2016-08-25T21:16:40.265285+00:00 heroku[router]: at=info method=POST path="/webhook" host=warm-depths-81393.herokuapp.com request_id=71deb2ab-f1db-40cc-a5d1-af926a35b6de fwd="173.252.120.112" dyno=web.1 connect=0ms service=8ms status=200 bytes=196
2016-08-25T21:16:40.084913+00:00 heroku[router]: at=info method=POST path="/webhook" host=warm-depths-81393.herokuapp.com request_id=61be8207-456c-4337-8c2b-ced6ff742f52 fwd="66.220.158.102" dyno=web.1 connect=0ms service=7ms status=200 bytes=196

这是日志的第二部分,显示我从我的虚拟帐户(测试人员)发送了另一条消息

2016-08-25T21:16:44.788335+00:00 app[web.1]: sent message to: 1107394752663896
2016-08-25T21:16:45.195498+00:00 app[web.1]: sent message to: 284635001929051
2016-08-25T21:16:45.307382+00:00 app[web.1]: Error: { message: '(#100) No matching user found',
2016-08-25T21:16:45.307384+00:00 app[web.1]: type: 'OAuthException',
2016-08-25T21:16:45.307385+00:00 app[web.1]: code: 100,
2016-08-25T21:16:45.307385+00:00 app[web.1]: fbtrace_id: 'GKqZtMZawEv' }
2016-08-25T21:16:45.194037+00:00 heroku[router]: at=info method=POST path="/webhook" host=warm-depths-81393.herokuapp.com request_id=fce94f53-bd7e-4020-a0b0-ff05638e0b25 fwd="173.252.120.121" dyno=web.1 connect=0ms service=12ms status=200 bytes=196
2016-08-25T21:16:44.786566+00:00 heroku[router]: at=info method=POST path="/webhook" host=warm-depths-81393.herokuapp.com request_id=3d650955-c9db-47c2-8b69-b919e70bb94e fwd="173.252.120.100" dyno=web.1 connect=2ms service=4ms status=200 bytes=196
2016-08-25T21:16:45.515394+00:00 heroku[router]: at=info method=POST path="/webhook" host=warm-depths-81393.herokuapp.com request_id=f497c7b2-a9ee-4f9c-83b9-c23a2e358cc1 fwd="66.220.158.111" dyno=web.1 connect=0ms service=2ms status=200 bytes=196

编辑:我刚刚发现机器人重复发送消息的第二个用户实际上是聊天机器人 Facebook 页面本身。聊天机器人也向自己发送消息是否有原因?

最佳答案

问题是 webhook 订阅了传递消息等。

关于node.js - Facebook Messenger Bot API 发送多条消息并显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39155044/

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