Hey guys, I hope you doing well.
Today, I will discuss how to create a Facebook (FB) Messenger bot using Cloudways Laravel Cloud Platform. I will start with a brief introduction and then move to the implementation part.
Earlier this year, Facebook announced the Facebook Messenger platform and the launch has opened up exciting prospects for businesses all around the world. Using the Messenger platform you can create chat bots for your website. You could use this robust tool as your customer interaction and service point.
There are few requirements for setting up the integration:
- A functional PHP Site on Cloudways. This is where we will hook our FB chat bot. If you do not have the site, Signup Now.
- You should have a domain pointed on your app. A free domain or subdomain will do nicely.
- Get an SSL certificate for the website (HTTPS in front of address). You can use Let’s Encrypt SSL on Cloudways.
- You should have a working Facebook ID.
- Create a Facebook page if you don’t have one.
- You must have a working Facebook Developers account.
If you have assembled all these prerequisites, it is time to proceed. First, go to Facebook Developers account.
Step 1: Setting up the Webhook
Click on the MyApps and then Add a new app
– Click on WWW
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.
Click Add Product as shown below:
Click Messenger. Once added, click Messenger Settings.
Under Token Generation, select the page for which you wish to create the bot.
Copy the token. It will be needed later on. (It is a good idea to save it somewhere.)
Now, click Setup Webhooks under Webhook. This window will appear.
Enter your Domain URL, complete with HTTPS, that points to the Laravel app on Cloudways. (For example: https://yourdomain.com)
Now, enter the Verify Token.
Check all the options in the subscription field.
Now, before clicking Verify and Save, you need to make sure that Facebook Verification Challenge could be fulfilled.
For this,follow these steps:
Inside your app, insert the following code inside index.php. This will ensure that authentication is done for Facebook. You can check the details in their documentation. For now, test this:
Click Verify and Save. This will get your app verified.
Step 2: Communicating With Your Bot
The next thing we would do is to get the bot start communicating with our app. Let’s look at this code below, copy and paste on your index.php and I’ll explain you what else is going inside here. 🙂
$token = "YOUR TOKEN GOES HERE";
$fb_message = file_get_contents("message.txt");
$fb_message = json_decode($fb_message);
$rec_id = $fb_message->entry->messaging->sender->id; //Sender's ID
$rec_msg= $fb_message->entry->messaging->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);
You will put all the data received from the bot inside a message.txt file.
You will grab the JSON data into the variable and decode it into a PHP array.
For generating the necessary response, you require the access token and the actual message. Thus, before sending the message, you need to prepare the message as per Facebook Documentation.
Once done, test this code.
Message your page and see if it replies back
Here is an example. This is a page which I would message.
Step 3: Create Your Own
I have explained everything here in the easiest possible manner. So, you should now get started.
Therefore, setting up the bot is that easy. You could do much more with this chat bot. If you have a query or wish to contribute to the discussion, do leave a comment.
Create Laravel apps without the worry of server management.
Deploy your app on optimized PHP hosting servers for Laravel.