Facebook Messenger僵尸程序在显示通用模板后停止工作

时间:2017-10-24 15:57:12

标签: php facebook laravel bots facebook-messenger-bot

机器人回复完美但在显示通用/列表模板后停止工作。对于快速回复,按钮和文本消息等其他消息,它可以正常工作。我的应用程序处于开发模式,我将其更改为公开但无法正常工作。我已附上我的代码和截图。

我的代码有什么问题?

Facebook Messenger bot screenshot

API版本:v2.10
Facebook页面:https://www.facebook.com/yesira.net

namespace App\Http\Controllers;
use Illuminate\Http\Request;

class BotController extends Controller
{
    public function bot(Request $request)
    {
        $data = $request->all();

        //get the user’s id
        $id = $data["entry"][0]["messaging"][0]["sender"]["id"];
        $senderMessage = $data["entry"][0]["messaging"][0]["message"];
         $messageText = $data["entry"][0]["messaging"][0]["message"]["text"];

        if(!empty($senderMessage) && $messageText  == "hi"){
            $this->sendTextMessage($id, "Hi buddy");
        }

        else if($messageText == "blog") {
                    $answer = ["attachment"=>[
      "type"=>"template",
      "payload"=>[
        "template_type"=>"generic",
        "elements"=>[
          [
            "title"=>"Migrate your symfony application",
            "item_url"=>"https://www.cloudways.com/blog/migrate-symfony-from-cpanel-to-cloud-hosting/",
            "image_url"=>"https://www.cloudways.com/blog/wp-content/uploads/Migrating-Your-Symfony-Website-To-Cloudways-Banner.jpg",
            "subtitle"=>"Migrate your symfony application from Cpanel to Cloud.",
            "buttons"=>[
              [
                "type"=>"web_url",
                "url"=>"www.cloudways.com",
                "title"=>"View Website"
              ],
              [
                "type"=>"postback",
                "title"=>"Start Chatting",
                "payload"=>"Even want some more? say me!"
              ]              
            ]
          ]
        ]
      ]
    ]];
    $response = [
                        'recipient' => [ 'id' => $id ],
                        'message' => $answer
                     ];

                    $this->reply($response);
                }
    else{
        $this->sendTextMessage($id, "Hello world!");
    }
    }
    private function reply($jsonData)
    {
        $ch = curl_init('https://graph.facebook.com/v2.6/me/messages?access_token=' . env("PAGE_ACCESS_TOKEN"));

        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($jsonData));




        curl_exec($ch);

        curl_close($ch);
    }
    private function sendTextMessage($recepientId, $messageText)
    {
        $messageData = [
            "recipient" => [
                "id" => $recepientId,
            ],
            "message" => [
                    "text" => $messageText,
                ]
        ];
        $ch = curl_init('https://graph.facebook.com/v2.6/me/messages?access_token=' . env("PAGE_ACCESS_TOKEN"));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_HTTPHEADER, ["Content-Type: application/json"]);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($messageData));
        curl_exec($ch);
        curl_close($ch);
    }
}

1 个答案:

答案 0 :(得分:0)

可能您没有收到新事件,因为平台会继续重试。您需要使用200OK响应webhook事件。