Last active
November 5, 2021 18:01
-
-
Save remmel/fcbf60fd5364c443e74f407593ad50c8 to your computer and use it in GitHub Desktop.
Basic example of a Facebook Messenger Bot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 |
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
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
$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.