Skip to content

Instantly share code, notes, and snippets.

@codehimanshu
Forked from remmel/facebook-messenger-bot.php
Last active February 20, 2018 08:10
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save codehimanshu/5c91c6d4e6d492b4a7bfbd6d2f7fc563 to your computer and use it in GitHub Desktop.
Save codehimanshu/5c91c6d4e6d492b4a7bfbd6d2f7fc563 to your computer and use it in GitHub Desktop.
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'];
if(!empty($messageText))
{
$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);
}
@indiarocks08
Copy link

This is working fine , but How to Add New lines to message Text , Suppose I need to Send this Message :
Hello ,
How are you ...

This is being sent as Hello , How are you ... All text in displayed same line in Messenger Chat ...

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