Facebook Messenger 重复发送用户输入
Facebook Messenger repeats sending user input
我在 PHP 中设置了带有聊天机器人的 Facebook Messenger。
我将通常是单个回复的内容分解为多个回复消息,这样它在聊天中看起来更自然,比单个大聊天气泡更容易阅读。
这是我的做法:
for ($i=0; $i<count($response_array); $i++){
$message_fb_format = [
'recipient' => [
'id' => $userID,
],
'message' => [
'text' => $response_array[$i],
],
];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $replyMessageJSON);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-Hub-Signature: xxxxxxxxxxxxxxxxxxxxxxxxx', 'Content-Type: application/json'));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTP_VERSION, 'CURLOPT_HTTP_VERSION_NONE');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
sleep(2);
}
我发现当我将 sleep()
设置为 2 秒或更长时间时,Facebook Messenger 会重复发送原始用户输入。然后聊天 window 就好像我的聊天机器人多次重复它对单个输入的响应,但我的日志清楚地显示我的服务器多次收到来自 Facebook 的相同消息,并且聊天机器人对每一个都正确响应。
问题是,为什么?
为什么在多个响应消息之间停顿 2 秒会导致 Facebook Messenger 重复发送原始消息?
因为他们希望您及时用 200 OK
响应 webhook 请求,否则就会假设一定是出了什么问题,您的 bot 没有收到发送数据,所以它再次发送。
https://developers.facebook.com/docs/messenger-platform/webhook#unsubscribe
您必须首先使用 200 OK 和第一个回复来回答传入的 webhook 请求,然后将您的附加消息发送给收件人,这与初始请求无关。 (由于“24 + 1”政策,您将无法无限制地使用标准消息传递。)
根据 Facebook 的文档,您必须在 20 秒内响应任何请求。
When you receive a webhook event, you must always return a 200 OK HTTP response. The Messenger Platform will resend the webhook event every 20 seconds, until a 200 OK response is received. Failing to return a 200 OK may cause your webhook to be unsubscribed by the Messenger Platform.
您可以在此处阅读更多相关信息:https://developers.facebook.com/docs/messenger-platform/webhook#response
可能是当大量响应到来时,队列变大并且它不断周期性地一次又一次地重复相同的响应。
请务必在 20 秒内回复。
我在 PHP 中设置了带有聊天机器人的 Facebook Messenger。
我将通常是单个回复的内容分解为多个回复消息,这样它在聊天中看起来更自然,比单个大聊天气泡更容易阅读。
这是我的做法:
for ($i=0; $i<count($response_array); $i++){
$message_fb_format = [
'recipient' => [
'id' => $userID,
],
'message' => [
'text' => $response_array[$i],
],
];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $replyMessageJSON);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-Hub-Signature: xxxxxxxxxxxxxxxxxxxxxxxxx', 'Content-Type: application/json'));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTP_VERSION, 'CURLOPT_HTTP_VERSION_NONE');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
sleep(2);
}
我发现当我将 sleep()
设置为 2 秒或更长时间时,Facebook Messenger 会重复发送原始用户输入。然后聊天 window 就好像我的聊天机器人多次重复它对单个输入的响应,但我的日志清楚地显示我的服务器多次收到来自 Facebook 的相同消息,并且聊天机器人对每一个都正确响应。
问题是,为什么?
为什么在多个响应消息之间停顿 2 秒会导致 Facebook Messenger 重复发送原始消息?
因为他们希望您及时用 200 OK
响应 webhook 请求,否则就会假设一定是出了什么问题,您的 bot 没有收到发送数据,所以它再次发送。
https://developers.facebook.com/docs/messenger-platform/webhook#unsubscribe
您必须首先使用 200 OK 和第一个回复来回答传入的 webhook 请求,然后将您的附加消息发送给收件人,这与初始请求无关。 (由于“24 + 1”政策,您将无法无限制地使用标准消息传递。)
根据 Facebook 的文档,您必须在 20 秒内响应任何请求。
When you receive a webhook event, you must always return a 200 OK HTTP response. The Messenger Platform will resend the webhook event every 20 seconds, until a 200 OK response is received. Failing to return a 200 OK may cause your webhook to be unsubscribed by the Messenger Platform.
您可以在此处阅读更多相关信息:https://developers.facebook.com/docs/messenger-platform/webhook#response
可能是当大量响应到来时,队列变大并且它不断周期性地一次又一次地重复相同的响应。 请务必在 20 秒内回复。