How to Create a Facebook Messenger Bot Using Cloudways Laravel Cloud

by Noor Ali  July 28, 2016

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.

Create Laravel apps without the worry of server management.

Deploy your app on optimized PHP hosting servers for Laravel.

About Noor Ali

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

Stay Connected:

You Might Also Like...

  • Dave Ganley

    This is very poor and you should be ashamed. This doesn’t use the Laravel PHP framework so don’t promote it, saying it does over on Laravel tricks

    At minimal create the routes for the GET and POST requests from FB and handle then in the routes file or better still in a controller

    For the GET request

    public function getWebhook(Request $request)
    {
    if ($request->get(‘hub_mode’) == ‘subscribe’ and $request->get(‘hub_verify_token’) === env(‘FACEBOOK_HUB_VERIFY_TOKEN’)) {

    return response($request->get(‘hub_challenge’));
    }

    return response(‘Error, verify token doesn’t match’, 400);
    }

    And you can get the POST request content easily with Laravel, something like this

    public function postWebhook(Request $request)
    {
    $content = json_decode($request->getContent());

    // parse content

    return response(‘ok’);
    }

    Messages can be batched so don’t target the first index in the array directly and when using arrays use the later array style of [] not array()

    There are plenty of packages for sending the requests back to FB in the various structures, it would be better practise to use one of them. This one has more of the latest features than most but is still incomplete https://github.com/davidpiesse/facebook_messenger_php

    Either do it properly or don’t do it at all

    • Noor Ali

      Hey Dave.

      Yes, your way is correct too. I will try to enhance it and integrate the wrapper you provided. This is something awesome I just passed by. I am myself learning Laravel and it’s just I like to experiment with things and this tutorial was just a quick “Hello World” kinda tutorial. Thank you very much. You took out time to read it. I will surely put my efforts to bring something crazy good out of the package you have provided.

      Good Day 🙂