I am developing a facebook chatbot. I am facing a issue which I cannot solve. I am developing this in laravel. Here I cannot get the postback payload. here is my code
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); }
and my route is
Route::any('chatbot', 'ChatbotController@index');
here the message is working . but the postback payload request is not going to server. on the other hand using the same code in normal php file I am able to get postback 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);
How to solve this issue? can anyone show me a path?
Solved it by checking at laravel.log file.
$this->messageText = isset($this->input['entry'][0]['messaging'][0]['message']['text']) ? $this->input['entry'][0]['messaging'][0]['message']['text'] : '' ;
messageText should be like this. that's why its causing an error.