How To Create A Facebook Messenger Bot Using Laravel

Submitted by azazqadir - 6 months ago

How to create a Facebook (FB) Messenger bot using Cloudways Laravel Cloud Platform. Name the app whatever you wish. Then, scroll down and enter your Web ID. Click Skip Quick Start. Now, enter your email address. Choose an appropriate category for your chat bot. Click Create App ID. Once you’re done with the above steps, follow the steps below. Source: http://www.cloudways.com/blog/set-up-facebook-messenger-bot-in-cloudways/

 <?php
$token = "YOUR TOKEN GOES HERE";
 
file_put_contents("message.txt",file_get_contents("php://input"));
$fb_message = file_get_contents("message.txt");
$fb_message = json_decode($fb_message);
$rec_id = $fb_message->entry[0]->messaging[0]->sender->id; //Sender's ID
$rec_msg= $fb_message->entry[0]->messaging[0]->message->text; //Sender's Message
$data_to_send = array(
'recipient'=> array('id'=>"$rec_id"), //ID to reply
'message' => array('text'=>"Hi I am Test Bot") //Message to reply
);
 
$options_header = array ( //Necessary Headers
'http' => array(
'method' => 'POST',
'content' => json_encode($data_to_send),
'header' => "Content-Type: application/json\n"
)
);
$context = stream_context_create($options_header);
file_get_contents("https://graph.facebook.com/v2.6/me/messages?access_token=$token",false,$context);
?>
comments powered by Disqus

Ads cover the costs of maintaining Laravel-tricks