Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Basic example of a Facebook Messenger Bot
<?php
// parameters
$hubVerifyToken = 'TOKEN123456abcd';
$accessToken = "xxx";
// 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'];
$answer = "I don't understand. Ask me 'hi'.";
if($messageText == "hi") {
$answer = "Hello";
}
$response = [
'recipient' => [ 'id' => $senderId ],
'message' => [ 'text' => $answer ]
];
$ch = curl_init('https://graph.facebook.com/v2.6/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']);
curl_exec($ch);
curl_close($ch);
//based on http://stackoverflow.com/questions/36803518
@tapos12
Copy link

tapos12 commented May 25, 2016

hey have you tried button template integrating with this example? if you have could you give some ideas?
i am trying out , but can't fetch button postbacks

Copy link

ghost commented Jun 4, 2016

@andreazorzi
Copy link

andreazorzi commented Jun 29, 2016

Hi @remmel, I followed the guide step by step, however, the bot will not respond to messages.
After a while there comes a message from Facebook saying "Webhooks Failing", it seems that when I write a message, facebook can not contact the Webhooks page. The challenge went well

@crjgonato
Copy link

crjgonato commented Jul 2, 2016

This is awesome. Thanks man. Its working.

@jonathan-nwosu
Copy link

jonathan-nwosu commented Jul 9, 2016

For some reason, I get 'I don't understand. Ask me 'hi' over and over again, even if the person sends 1 message ?

@sushant0077
Copy link

sushant0077 commented Jul 12, 2016

Hey Remmel.
I followed all step by step tutorial (https://www.youtube.com/watch?v=Jm9oCrAf-L0). but i am not able to set webhook. i am getting following error.
boterror

Can you please help me . Thanks in advance.

@deperucom
Copy link

deperucom commented Oct 3, 2016

@codehimanshu
Copy link

codehimanshu commented Nov 27, 2016

@jonathan-nwosu @remmel
This might flood your chat with continuous replies to a single message. Add a condition to check if the $messageText is empty before executing curl.
Try this Gist

@jabCraftPLPlayGames
Copy link

jabCraftPLPlayGames commented Mar 5, 2017

pozdro

@marouaneterai
Copy link

marouaneterai commented Apr 1, 2017

First, thank you very much for this tuto and code, it's helpful and i spent a lot of time to do this before i find this page.
And i agree with @codehimanshu , we need to test if the $messageText is not empty before we send a reply to the webhook.

@tiptopnet
Copy link

tiptopnet commented May 14, 2017

Hi
Please, I need your help to change a little bit the code.
With the code below, i would like to get 2 different text answers.
When I write "Hi", the bot should answer the 1st answer "Hello" and I'd like it answer too another separate answer. Example "Salut"
How i can code the 2 answers?
if($messageText == "Hi") {
$answer = ["text" => "Hello"];
$response = [
'recipient' => [ 'id' => $senderId ],
'message' => $answer
];
}
Thanks for your help !

@stevemalisa
Copy link

stevemalisa commented Feb 8, 2018

$response = [
'recipient' => [ 'id' => $senderId ],
'message' => [ 'text' => $answer ]
];

can you please create an attachment format? like mine but

$response = [
'recipient' => [ 'id' => $senderId ],
'message' => ['attachment' =>
'type' => 'image',
'payload' => ['url' => $answer]
]
];

i cant seem to get it to work. i dont know if im doing the code above right.

@indiarocks08
Copy link

indiarocks08 commented Feb 20, 2018

Is there any possibility of sending 2 messages separately for a single reply by user ??
Example if users Types : Hi
Reply Should be :
Hello
I am fine
Both in separate messages , one followed by another

@kinekimalone
Copy link

kinekimalone commented Jul 13, 2018

Hi
Please, I need your help to change a little bit the code.
With the code below, i would like to get 2 different text answers.
When I write "Hi", the bot should answer the 1st answer "Hello" and I'd like it answer too another separate answer. Example "Salut"
How i can code the 2 answers?
if($messageText == "Hi") {
$answer = ["text" => "Hello"];
$response = [
'recipient' => [ 'id' => $senderId ],
'message' => $answer
];
}
Thanks for your help !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment