Facebook Chatbot回发不起作用 [英] Facebook Chatbot postback not working

查看:112
本文介绍了Facebook Chatbot回发不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用PHP设置了一个可工作的Facebook聊天机器人,并使用以下其中一个回发按钮构建了一个通用模板轮播:

I've setup a working facebook chatbot in PHP and built a generic template carousel with one of the postback buttons being:

[
type"=>"postback",
"title"=>"Opening Hours",
"payload"=>"Opening Hours"                                                                                              
],

按下回发按钮并查看我的PHP日志,我得到了:

Pressing the postback button and checking my PHP logs I am getting:

{"object":"page","entry":[{"id":"457107221010xxx","time":1513219207386,
    "messaging": [{"recipient":
              {"id":"457107221010xxx"},"timestamp":1513219207386,"sender":
              {"id":"1510264525690xxx"},"postback":{"payload":"Opening 
            Hours","title":"Opening Hours"}}]}]}

我正在通过以下方式在代码中处理此回发:

I'm handling this postback in my code by:

$postback = $input['entry'][0]['messaging'][0]['postback']['payload'];
if ($postback!="") {
 $answer = ["text"=> $openingHours];
}

但是在按了"postback"按钮后,在Messenger窗口中,Messenger似乎正在键入",显示了三点对话框,显示了几秒钟,但是随后它停止了,没有任何回复.我确实在webhooks中启用了"message_postback"选项,并且其他查询仍在起作用(例如,如果我手动输入"Opening Hours",则会收到"Opening Hours"答复).我使用以下代码处理其他查询,并且可以正常工作:

But in messenger window after pressing said postback button, messenger seem to be "typing" with the three dots dialog showing for a few seconds, but then it just stops without any replies. I did enable "message_postback" option in webhooks, and other queries are working (e.g. if I type "Opening Hours" manually I'll get the Opening Hours reply). I process other queries with following code and it works:

$sender     = $input['entry'][0]['messaging'][0]['sender']['id'];
$message    = $input['entry'][0]['messaging'][0]['message']['text'];
if(preg_match('[opening|hours]', strtolower($message))) {
        $answer =     ["text"=>"
                Opening Hours:
10:30 am – 1:00 am (Sun-Thu)"];
} else {
   //show menu
}

任何建议,不胜感激!

推荐答案

由于我没有看到触发实际发送的代码,因此可以在其中找到错误.如果您已经复制了基础教程,则可能看起来像是我很久以前开始的教程:

As I dont see the code, which triggers the actual sending, the error could be found there. If you've copied the basic tutorial it might look like this one I started with a long time ago:

if(preg_match('[time|current time|now]', strtolower($message))) {
    $message_to_reply = date('l jS \of F Y h:i:s A');
} else {
    $message_to_reply = 'Huh! what do you mean?';
}

// your code here
$postback = $input['entry'][0]['messaging'][0]['postback']['payload'];
if ($postback!="") {
    $message_to_reply = "postback!";
    $foundPostback = true;
}

$url = 'https://graph.facebook.com/v2.11/me/messages?access_token='.$access_token;
$ch = curl_init($url);
$jsonData = '{
    "recipient":{
        "id":"'.$sender.'"
    },
    "message":{
        "text":"'.$message_to_reply.'"
    }
}';

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

if(!empty($input['entry'][0]['messaging'][0]['message']) || $foundPostback ){
    $result = curl_exec($ch);
}

注意$foundPostback.如果您的发送触发器在本教程中看起来像这样,它将不会发送消息,因为回发消息中没有$input['entry'][0]['messaging'][0]['message']属性.因此,如果您检测到回发,则必须保留该标志.

Notice the $foundPostback. If your send trigger looks like this from one the tutorials, it will not send messages, as there is no $input['entry'][0]['messaging'][0]['message']attribute in postback messages. So if you detect a postback, you have to keep that flag.

但是,我强烈建议建立自己的类来处理消息,回发,传递,回声等.有关这些内容的更多信息,请单击此处: Facebook Messenger文件

However, I strongly suggest to build own classes for handling messages, postback, deliveries, echos and so on. More about those you can find here: Facebook Messenger Docs

这篇关于Facebook Chatbot回发不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆