我正在做一个 facebook 信使机器人。启动它后,它会调用 WebHook。 不幸的是,在第一次启动后不会停止使用相同的参数抛出相同的调用。 设置是:
- 消息传递;
- message_reads;
- 消息;
- messaging_optins;
- messaging_postbacks。
源代码是这样的:https://github.com/Ellusu/nuraghebot-facebookmessenger/blob/master/index.php
我哪里错了? 为什么只有一个电话?
最佳答案
根据你的代码,我决定你不能设置你的 webhook,所以来自 documentation
At your webhook URL, add code for verification. Your code should expect the Verify Token you previously defined, and respond with the challenge sent back in the verification request. Click the "Verify and Save" button in the New Page Subscription to call your webhook with a GET request.
因此,要使 PHP 成功设置 webhook,您必须返回 hub_challenge 参数。
使用您的 token 定义 $verify_token 并添加如下内容:
if (!empty($_REQUEST['hub_mode']) && $_REQUEST['hub_mode'] == 'subscribe' && $_REQUEST['hub_verify_token'] == $verify_token) {
// Webhook setup request
echo $_REQUEST['hub_challenge']; exit;
}
设置成功后,您可以从脚本中删除此代码。
或者,如果您的 webhook 已经 Hook :
您应该跳过任何已读 和已发送 消息,如下所示:
if (!empty($input['entry'][0]['messaging'])) {
foreach ($input['entry'][0]['messaging'] as $message) {
// Skipping delivery messages
if (!empty($message['delivery'])) {
continue;
}
// Skipping read messages
if (!empty($message['read'])) {
continue;
}
}
}
或者,您可以在 Facebook 页面设置/Webhooks 的页面订阅部分中取消选择 message_reads 和 message_deliveries 复选框。
关于php - WebHook 中的无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39377533/