Calculator Facebook Bot in PHP

Facebook messenger bots are getting hot these days. Its one of the simple yet powerful platform to create apps which does not require installation and are super responsive.

If you are looking forward to create a Facebook messenger bot, this post is for you.

What is a ChatBot?

So bot is the short form for Robot (Which I trust you guys should be knowing by now!). Chat bot is something similar, but instead its not a machine, but an automated way of replying to messages.

What are the uses of chat bots?

  1. Create a automated customer service chat.
  2. Create a simple chat based store.
  3. Make simple games that you can play in messenger.

How does a chat bot work?

Facebook Messenger Flow
Facebook Messenger Flow

So what we need to build is only the Server.

Steps to building the server.

  1. Register a web domain name (Because Facebook does not allow IP addresses).
  2. Buy a hosting (ex. digital ocean) and install required components.
  3. Configure SSL (Facebook only accepts https)
  4. Create and configure Facebook Page and App.
  5. Write the logic in PHP.

1.Register a web domain name.

Good domain names are hard to find, but if you are planning to make a test app, you can always buy a test domain name.
To buy a domain name you can go to, search for an available domain name, continue to cart and finish the payment. This is a 5 mins job. I used to purchase “.in” domains for Rs.200 per year. There is no need to buy anything other than domain name from Godaddy.


2. Create a droplet in digital ocean

Never heard about digital ocean?
DigitalOcean is a cloud infrastructure provider focused on simplifying web infrastructure for software developers.
Click below to go to Digital Ocean and create an account. You might have to pay $5 upfront as a minimum payment.

Go ahead and create a droplet. Use the below configuration. Thats only necessary for this tutorial.

Once you have created a droplet in Digital ocean there will be an email sent by digital ocean team which will have your IP address and password.
You can ssh to your server using

Now you have your Domain name and Hosting setup. Time to connect both.

You have to do 2 things.

  1. In your domain name service (Godaddy) change you name servers as the ones provided by Digital Ocean.
    Goto Manage -> Domains->Manage DNS->NameServers->Change
    Provide the names ervers as mentioned below.

  2. In your hosting (Digital Ocean) you have to add this domain to your droplet. You can do this as shown below.

Now lets install minimum tech that is required for your bot to run in server (Apache and PHP).
Follow the below steps for the same

Now that we have installed apache and PHP lets test our web server.
Lets create a simple PHP Script that prints PHP information.
Create a file info.php in the public folder

Add the following script into it

Restart apache so as the changes to take effect.

Now open your browser and go to “http://<>/info.php”. It should show you PHP info.

3. Configure SSL.

Now you can obtain free SSL Certificate using LetsEncrypt.
LetsEncrypt is a new free, automated and open new Certificate authority.
How to configure SSL using LetsEncrypt in DigitalOcean is neatly mentioned here.

4. Create and configure Facebook Page and App.

Create a new Facebook Page is not explained here. Its pretty simple and you should be knowing that.
Now go ahead and create a new Facebook App from
Once the app is created Go to app dashboard.
Under Product Settings click “Add Product” and select “Messenger”.

Now that we have setup the Facebook app, we need to setup the webhook.

What is Webhook?
So before in the flow diagram I have explained that facebook server will ping our server for all the chat that comes in our page, So webhook is exactly that endpoint which is being used by facebook.

So to start with go to Webhooks section and click “Setup Webhooks

In the url section use something like “http://<>/verify.php and select message_deliveries, messages, messaging_optins, and messaging_postbacks under Subscription Fields.

in Verify Token field you can give some random string, and make sure to use the same token in your verify.php script.

Don’t click ‘Verify and Save’ as we have to create the verify.php file in our server.

Add the below code into it

Once you save the above file. You can press “Verify and Save” in the Webhook settings.

Now that we have setup the webhook, we need to connect the facebook page with the app, so that whenever someone chats with your page its being sent to your webhook.

Go to Messenger Settings and in the Webhooks section, you can subscript the Webhook for a specific page as shown below.

Save the access_token for use in next step.

5. Write the logic in PHP.

Facebook sends data to our web server as raw php data, which is a json string. You can get the data using the below code.

The same data is like the below JSON.

Now if you closely observe, this is how the data is represented.
There is a page object which has multiple entries, and each entry will have multiple messages. So we have to process all the requests. Facebook might be sending 1 call per N chats to process.
Below code will go through each message in each entries and just log it to error log.

Sometimes you might get a error curl not found. Use the below steps to install curl in your webserver


Now that we have got the input from the user, lets try responding to the user with the same message.

So how do we send a message back to users?
Its pretty simple you have to send a json object back to Facebook endpoint. The object looks like the below.

Mainly it has recipient id, which is the id of the sender (who we are replying to) and the message details.

Now how do we send this to facebook. We can use curl. Copy the function that will do the same.

Now use the above function to return whatever the user has typed back to him.

Now go to the chat box and type something, the bot will reply back the same!

Congratz you have created your first bot!

Now lets make it some more intelligent. Lets make a calculator.
Lets first write a function that evaluates the given expression.

Now just attach it our main bot code.

Open the chat bot and enjoy your new calculator!







Just another WordPress site