We are always in touch
Ukraine: +380674822702

How to create a Facebook Messenger chatbot

You can create a Facebook Messenger chatbot using tools (on development platforms) or build it from scratch in any programming language which allows you sending POST requests.
We will focus on writing a simple bot from scratch (with an example in PHP) and then we’ll describe the creation of a chatbot without coding.

Preparations: creating a page and an app

A Messenger chatbot must be connected to a Facebook page, so we have to create a public page. We can do it in advance, connect the bot to an existing public page or create it later, when we have to connect our app.
If you want to build a chatbot using a development platform, the preparations are over for you, and you can go to the platform (and skip to the paragraph of our article called How to create a Facebook Messenger bot without coding).
If we are going to code the bot, we have to create an app on the website developers.facebook.com to get access to API Messenger.

To do so, we press “Add a new app” button in “My apps” page on developers.facebook.com.

Fill out the form (bot name and email) and go to the app.
On the left panel, we press “Add Product” and choose Messenger.

Then we find a Generate token section in Messenger Product and choose a page to connect with the chatbot. We get a token that will be necessary to work with API.

Set Up Webhooks

We want our chatbot to know when somebody is writing to it, so we have to subscribe to its events.
In the Messenger Product on the App’s panel, we go to webhooks section and press Setup Webhooks (first, we have to locate on the server a script that will receive all the events and send answers). It is important to use a server with SSL certificate because you can’t subscribe to events without it.

Fill out the form: indicate bot’s address and tick necessary events (you can get detailed information about each of them in the documentation).

What does “Verify Token” mean? It is a field which will be sent to your server in JSON and will allow to make subscription process secure. Another developer will not be able to subscribe your script to the events of his bot if he doesn’t know your “Verify Token”

How the subscription process goes on?
When Facebook sends a form mentioned above it also sends to your script a request with such fields: hub_verify_token and hub_challenge
Hub_challange is a field which has to return your script on request.
Hub_verify_token serves for you to check whether this request is received from you or not.
Here is an example of the code for subscription process.

After you have done everything right, you will see an inscription in green – it means everything goes fine. Now select the page:

Backend development

Now let’s look at our script:

  • we receive data on the event
  • we choose a required answer
  • we send a request to Facebook

We get necessary fields for our JSON events, in this case, it is an ID, a message, a button and a quick reply.

This is how the conversation looks in the bot:


Until the app goes through the review and approval, the chatbot will work only for admins/ developers/ quality testers which are set up in “Roles” sections in your app.
To make the bot available to all users, it is necessary to fill out the form on developers.facebook.com and send for review of a Facebook moderator.
Before you get approval you have to fill out all the fields in the app settings:

Go to developers.facebook.com/apps, to your app, to Messenger tab and find the section “App Review for Messenger”.
Press “Add items” button for each permission your bot needs.

There are three types of permissions

pages_messaging is to exchange messages with users inside the 24-hour standard messaging window
pages_messaging_subscriptions is for sending messages to users outside this window
pages_messaging_phone_number enables an app to match phone numbers that a bot owner already has to Messenger accounts and to send messages.
The third permission is paid, it costs $99.

In the “Approved Items “section added permissions will be displayed.

Press the button “Edit notes” next to each submitted permission, and you will see the form for submission of additional information. Fill it out.
To get pages_messaging_subscriptions permission you have to make a screencast of your chatbot’s work.
The approval process takes up to 5 days.
After the permission is received don’t forget to set the Make Your_app_name_App public switch to Yes.

How to create a Facebook Messenger bot without coding

All the popular development platforms offer to create a chatbot for Facebook Messenger: Chatfuel, ManyChat, Motion.ai, Flow.xo, Botsify
The platforms make the process of chatbot creation and launch easier and faster, especially after you get to know the interface of the platform.

  • First, they undertake backend development and take care of bot functioning according to the concepts of its creator. The creator has only to compose desired interaction process in a constructor without coding, as a rule, in a simple and intuitive interface. It may look different but on all the platforms, you build the chat flow selecting necessary bot interface elements (mentioned above) as if you were making a house from Lego parts of different forms and sizes, and you indicate how these blocks should be connected. In motion.ai, they offer to do it in drag and drop fashion: you take elements from a kind of palette and drag them to the canvas, like if you were drawing a chat flow on paper. In Chatfuel, you can add and view blocks on the left of the screen and edit them on the right. In Manychat, there is a Basic Builder, where they offer a set of message templates, even with advice on the content structure but also you can go to a visual constructor called Flow Builder. We are going to present a detailed comparison of these platforms in one of the future articles.
  • Second, the development platforms undertake a server part (setting up Webhooks) and connecting your chatbot to the Facebook page – everything is done in one click.
  • Third, and with Facebook Messenger it is very important, when you use platforms, you can skip a long and tedious process of publication (submitting your bot for review and approval of Facebook moderators). A platform publishes its own app (with approval) and all the bots based on this platform work through this app. Though, if you want to add your bot to Messenger Discover tab you have to go through official publication.

What can you do with this bot now?

You can share your bot using a link or a Messenger code, and submit it for Messenger Discover tab.
You can use paid methods of chatbot promotion: place Facebook, Instagram and Messenger Ads and send sponsored messages.
The last Facebook Messenger update, a chat plugin, allows to integrate a chatbot into a website.

The authors:

Vadim Sharafin, php-developer, dialog systems specialist of Singularika

Valeriia Maliarenko, Singularika copywriter

Contact us!

We respond within 2 hours