Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Very Simple Facebook Chat Bot PHP Webhook Script Example
<?php
$challenge = $_REQUEST['hub_challenge'];
$verify_token = $_REQUEST['hub_verify_token'];
// Set this Verify Token Value on your Facebook App
if ($verify_token === 'testtoken') {
echo $challenge;
}
$input = json_decode(file_get_contents('php://input'), true);
// Get the Senders Graph ID
$sender = $input['entry'][0]['messaging'][0]['sender']['id'];
// Get the returned message
$message = $input['entry'][0]['messaging'][0]['message']['text'];
//API Url and Access Token, generate this token value on your Facebook App Page
$url = 'https://graph.facebook.com/v2.6/me/messages?access_token=<ACCESS-TOKEN-VALUE>';
//Initiate cURL.
$ch = curl_init($url);
//The JSON data.
$jsonData = '{
"recipient":{
"id":"' . $sender . '"
},
"message":{
"text":"The message you want to return"
}
}';
//Tell cURL that we want to send a POST request.
curl_setopt($ch, CURLOPT_POST, 1);
//Attach our encoded JSON string to the POST fields.
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
//Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
//Execute the request but first check if the message is not empty.
if(!empty($input['entry'][0]['messaging'][0]['message'])){
$result = curl_exec($ch);
}
?>
@zeta243

This comment has been minimized.

Copy link

commented Aug 4, 2016

Hola no corre el código veo el mensaje que recibe pero cuando executa $result = curl_exec($ch); no se visualiza nada en el chat

@neeksor

This comment has been minimized.

Copy link

commented Feb 15, 2017

// Get the Senders Graph ID
$sender = $input['entry'][0]['changes'][0]['value']['sender_id'];

// Get the returned message
$message = $input['entry'][0]['changes'][0]['value']['message'];

@HollanderNL

This comment has been minimized.

Copy link

commented Mar 8, 2017

How to set a specific answer/message if someone says hello for example?

@wpnimitz

This comment has been minimized.

Copy link

commented May 30, 2017

Any simple class for this?

@ravigohil999

This comment has been minimized.

Copy link

commented Sep 21, 2017

I am assuming we need to put the page access token for the access token. Thanks for the code by the way.

@ravigohil999

This comment has been minimized.

Copy link

commented Sep 21, 2017

Add a line at 33.
$jsonDataEncoded = json_encode($jsonData);

Change line 38 to
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);

Sometimes this needs to be done.

@ravigohil999

This comment has been minimized.

Copy link

commented Sep 21, 2017

Hello Guys, My bot isn't working. Webhook is accepting the url but the bot is not responding.

@vishnu1991

This comment has been minimized.

Copy link

commented Feb 14, 2018

@ravigohil999 are you still facing issue?
if yes,what error are you getting?

@gralias

This comment has been minimized.

Copy link

commented Feb 16, 2018

`
$input = json_decode(file_get_contents('php://input'), true);

// Get the Senders Graph ID
$sender = $input['entry'][0]['messaging'][0]['sender']['id'];

// Get the returned message
$message = $input['entry'][0]['messaging'][0]['message']['text'];
`
can you explain about some lines above
What about $input?
file_get_contents ??? and which file ?

Unsupported post request. Object with ID 'me' does not exist, cannot be loaded due to missing permissions, or does not support this operation.

@indiarocks08

This comment has been minimized.

Copy link

commented Feb 26, 2018

How to save the sender messages to a file ?

@hdkcreative

This comment has been minimized.

Copy link

commented May 9, 2019

this work today ? 2019- may ?

@tsykiw

This comment has been minimized.

Copy link

commented Jul 24, 2019

this work today ? 2019- may ?

still worked

@cristianjesusb

This comment has been minimized.

Copy link

commented Sep 12, 2019

excellent work! I've modified the code to redirect input messages to a Dialogflow bot and also to handdle pass_thread_control requests (to switch on/off bot allowing human interaction and avoiding bot answers when it should not, just moving conversation from Done to Inbox in Facebook Admin Panel)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.