首页 文章

FB messenger Bot没有得到回发有效载荷

提问于
浏览
1

我正在开发一个facebook聊天机器人 . 我面临一个我无法解决的问题 . 我正在用laravel开发它 . 在这里,我无法获得回发有效负载 . 这是我的代码

public function index(Request $request) {
        if ($request->hub_verify_token === $this->verifyToken) {
            echo $request->hub_challenge;
            exit;
        }

        $input          = json_decode(file_get_contents("php://input"), true);
        $senderId       = $input['entry'][0]['messaging'][0]['sender']['id'];
        $messageText    = $input['entry'][0]['messaging'][0]['message']['text'];
        $pageId         = $input['entry'][0]['id'];

        $accessToken = "access_token_that_got_from_fb";

        //set Message
        if($messageText != "") {
            $answer = "Howdy! User";
        }

        if($messageText == "hello") {
            $answer = 'Testing hello from bot';
        }

        foreach ($input['entry'][0]['messaging'] as $message) {
            // When bot receive message from user
            if (!empty($message['postback'])) {
                $answer = 'got it tada';
            }
        }

        //send message to facebook bot
        $response = [
            'recipient' => [ 'id' => $senderId ],
            'message' => [ 'text' =>  $answer ] //json_encode($request->getContent())
        ];


        $ch = curl_init('https://graph.facebook.com/v2.11/me/messages?access_token='.$accessToken);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($response));
        curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);

        $result = curl_exec($ch);
        curl_close($ch);
    }

我的路线是

Route::any('chatbot', 'ChatbotController@index');

这里的消息正在发挥作用 . 但是回发有效负载请求不会转到服务器 . 另一方面,在普通的php文件中使用相同的代码,我能够获得回发paylod .

$hubVerifyToken = 'chatbot';
    $accessToken =   "access_token";
    // check token at setup
    if ($_REQUEST['hub_verify_token'] === $hubVerifyToken) {
        echo $_REQUEST['hub_challenge'];
        exit;
    }
    // handle bot's anwser
    $input          = json_decode(file_get_contents('php://input'), true);
    $senderId       = $input['entry'][0]['messaging'][0]['sender']['id'];
    $messageText    = $input['entry'][0]['messaging'][0]['message']['text'];
    $postback       = isset($input['entry'][0]['messaging'][0]['postback']['payload']) ? $input['entry'][0]['messaging'][0]['postback']['payload'] : '' ;

    //set Message
    if($messageText == "hi") {
        $answer = "Hello";
    }
    if($messageText == "hello") {
        $answer = "Hello there, welcome to Chatleads";
    }

    if($postback) {
        $answer = "postback TADA";
    }

    //send message to facebook bot
    $response = [
        'recipient' => [ 'id' => $senderId ],
        'message' => [ 'text' => $answer ]
    ];

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, "https://graph.facebook.com/v2.11/me/messages?access_token=$accessToken");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($response));
    curl_setopt($ch, CURLOPT_POST, 1);

    $headers = array();
    $headers[] = "Content-Type: application/json";
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    $result = curl_exec($ch);
    if (curl_errno($ch)) {
        echo 'Error:' . curl_error($ch);
    }
    curl_close ($ch);

如何解决这个问题?有人能告诉我一条路吗?

1 回答

  • 0

    通过检查laravel.log文件解决了这个问题 .

    $this->messageText  = isset($this->input['entry'][0]['messaging'][0]['message']['text']) ? $this->input['entry'][0]['messaging'][0]['message']['text'] : '' ;
    

    messageText应该是这样的 . 这就是它导致错误的原因 .

相关问题