简体   繁体   English

Facebook Chatbot回发不起作用

[英]Facebook Chatbot postback not working

I've setup a working facebook chatbot in PHP and built a generic template carousel with one of the postback buttons being: 我已经用PHP设置了一个可用的Facebook聊天机器人,并使用以下其中一个回发按钮构建了一个通用模板轮播:

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

Pressing the postback button and checking my PHP logs I am getting: 按回发按钮并检查我的PHP日志,我得到:

{"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];
}

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. 但是在按了“ postback”按钮后,在Messenger窗口中,messenger似乎在“键入”三个点对话框,显示了几秒钟,但是随后它停止了,没有任何回复。 I did enable "message_postback" option in webhooks, and other queries are working (eg if I type "Opening Hours" manually I'll get the Opening Hours reply). 我确实在webhooks中启用了“ message_postback”选项,并且其他查询仍在工作(例如,如果我手动键入“ Opening Hours”,我将收到“ Opening Hours”答复)。 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
}

Any advice much appreciated! 任何建议,不胜感激!

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

Notice the $foundPostback . 注意$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. 如果您的发送触发器在本教程中看起来像这样,它将不会发送消息,因为回发消息中没有$input['entry'][0]['messaging'][0]['message']属性。 So if you detect a postback, you have to keep that flag. 因此,如果检测到回发,则必须保留该标志。


在此处输入图片说明


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 Messenger Docs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM