问题
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:
POST /Facebook_Messenger_Token 500 Internal Server Error
and in routs file in Laravel I use Get and Post functions as follow:
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:
[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:
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.
回答1:
The $id is being defined on line 19 (that is after line 17). in order to use it on
$user = json_decode($this->getUser($id));
you should place the above line after line 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
$id = $input['entry'][0]['messaging'][0]['sender']['id'];
is using $input that is defined before.
all you have to do is move line 17 under line 19.
so the full function now should look like this:
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);
}
来源:https://stackoverflow.com/questions/62268746/post-500-internal-server-error-from-messenger-bot-in-laravel