简体   繁体   English

POST:来自 laravel 中 Messenger Bot 的 500 内部服务器错误

[英]POST: 500 Internal Server Error from Messenger Bot in laravel

I setup my Webhok by Ngrok URL for my Facebook page, and I applied all of the requirements for the Messenger Platform, but when I send the messages to my Facebook page I encounter the following error:我通过 Ngrok URL 为我的 Facebook 页面设置了我的 Webhok,并应用了 Messenger 平台的所有要求,但是当我将消息发送到我的 ZD85544FCE402C7A2A936A48078ED 页面时,我遇到了以下错误:

POST /Facebook_Messenger_Token 500 Internal Server Error 

and in routs file in Laravel I use Get and Post functions as follow:在 Laravel 的 routs 文件中,我使用 Get 和 Post 函数,如下所示:

Route::get('Facebook_Messenger_Token', 'MessengerController@index');
Route::post('Facebook_Messenger_Token', 'MessengerController@index');

When I send the messages I get the following error in storage/app.logs/laravel:当我发送消息时,我在 storage/app.logs/laravel 中收到以下错误:

[2020-06-08 18:44:21] local.ERROR: Undefined variable: id {"exception":"[object] (ErrorException(code: 0): Undefined variable: id at C:\\xampp\\htdocs\\AzadApp\\app\\Http\\Controllers\\MessengerController.php:17)
[stacktrace]

my public function index:我的公共 function 索引:

public function index()
    {
        // here we can verify the webhook.
        // i create a method for that.
        $this->verifyAccess();

        $user    = json_decode($this->getUser($id)); --this is line 17
        $input   = json_decode(file_get_contents('php://input'), true);
        $id      = $input['entry'][0]['messaging'][0]['sender']['id'];
        $message = $input['entry'][0]['messaging'][0]['message']['text'];

        $response = [
            'recipient'     =>  ['id'   => $id ],
            'message'       =>  ['text' => "Thanks for watching {$user->first_name} {$user->last_name}! :)"]
        ];

        $this->sendMessage($response);
    }

Please support and thanks.请支持和感谢。

The $id is being defined on line 19 (that is after line 17). $id 是在第 19 行(即第 17 行之后)定义的。 in order to use it on为了使用它

$user = json_decode($this->getUser($id));

you should place the above line after line 19你应该把上面的行放在第 19 行之后

$id = $input['entry'][0]['messaging'][0]['sender']['id'];

do not move line 19 up (as i said in my comment) instead move line 17 down since不要将第 19 行向上移动(正如我在评论中所说),而是将第 17 行向下移动,因为

$id = $input['entry'][0]['messaging'][0]['sender']['id'];

is using $input that is defined before.正在使用之前定义的 $input。

all you have to do is move line 17 under line 19.您所要做的就是将第 17 行移到第 19 行下。

so the full function now should look like this:所以完整的 function 现在应该如下所示:

public function index()
    {
        // here we can verify the webhook.
        // i create a method for that.
        $this->verifyAccess();


        $input   = json_decode(file_get_contents('php://input'), true);
        $id      = $input['entry'][0]['messaging'][0]['sender']['id'];
        $message = $input['entry'][0]['messaging'][0]['message']['text'];
        $user    = json_decode($this->getUser($id));

        $response = [
            'recipient'     =>  ['id'   => $id ],
            'message'       =>  ['text' => "Thanks for watching {$user->first_name} {$user->last_name}! :)"]
        ];

        $this->sendMessage($response);
    }

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

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