max13
max13

Reputation: 63

PHP how to handle get started button Messenger postback with a chat bot

I am working on a Messenger chatbot in development mode and have made some progress dealing with messages and quick replies but can't find the way to detect the Postback payload event sent by Facebook after the user press the "Get Started Button"

I have set the Get Started button postback and put the payload string by sending a POST request to the Messenger Profile API which returned "success", and had also set the "messaging_postbacks" event to my webhook.

However when the button is clicked the event is not detected by the webhook.

This is part of my code:

$input = json_decode(file_get_contents('php://input'), true);
$sender = $input['entry'][0]['messaging'][0]['sender']['id'];

//this handles the message text properly
$message = $input['entry'][0]['messaging'][0]['message']['text'];

//this deals correctly with quick reply payload
$quickreply = $input['entry'][0]['messaging'][0]['message']['quick_reply']['payload'];

I have tried separately and unsuccessfully each one of this lines of code to get the postback input triggered by the get started button:

$getstarted = $input['entry'][0]['messaging'][0]['get_started']['payload'];
$getstarted = $input['entry'][0]['messaging'][0]['message']['get_started']['payload'];
$getstarted = $input['entry'][0]['messaging'][0]['postback']['payload'];
$getstarted = $input['entry'][0]['messaging'][0]['message']['postback']['payload']; 

I will appreciate any suggestions :)

Upvotes: 0

Views: 887

Answers (1)

max13
max13

Reputation: 63

The way to retrieve the value of the get_started button was among the ones I had tried, specifically:

$input['entry'][0]['messaging'][0]['postback']['payload']

The issue had to do with a separate part of my code in the cURL thats sends the POST request.

It said:

if(!empty($input['entry'][0]['messaging'][0]['message'])){
$result = curl_exec($ch);

And needed to say:

if(!empty($input['entry'][0]['messaging'][0]['postback'])){
$result = curl_exec($ch);

The get_started button sends a postback to the webhook so the first script wasn't initiating the cUrl because there was no 'message' in the $input variable but a 'postback' property

A good video resource that dealt the same issue: https://www.youtube.com/watch?v=JQkmznEfVDo

Upvotes: 0

Related Questions