This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

CloudwaysCDN — a powerful solution that offers superior performance and satisfied global audience for your business. Read More

How to Create a Facebook Messenger Bot Using Cloudways Laravel Cloud

Updated on  28th July

4 Min Read
Reading Time: 4 minutes

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

image02

– Click on WWW

facebook developers

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.

Setup Webhooks

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. 🙂

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.

Facebook Messenger on Cloudways

This is a test product.

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.

Share your opinion in the comment section. COMMENT NOW

Noor Ali

Noor Ali is an Associate Software Engineer at Cloudways. He loves to solve technical problems through programming and mathematics.

Create Laravel apps without the worry of server management.

Deploy your app on optimized PHP hosting servers for Laravel.

Get Our Newsletter
Be the first to get the latest updates and tutorials.

THERE’S MORE TO READ.