Build Your First Chatbot using Facebook Messenger Platform

Build Your First Chatbot using Facebook Messenger Platform

Yes you heard it right! You can now enhance customer experience at your Business with conversational bots. Facebook Messenger offers very powerful messenger platform to build personalized bot for your Business. The simple and easy conversational interface provides customers with immediate responses. 

I am sure there will be many questions in your mind by now! What is a Chatbot? How it can empower my Business achieving higher Revenues and hard savings? How will it give my customers a personalized assistance?

So let’s get started with what exactly is a Chatbot? Chatbot is an artificial conversational messenger which provides powerful conversation and sharing capabilities and makes it easy offer instant customer support. Chatbots can be setup in two different ways - Artificial Intelligence based or Knowledge Base driven. You can also read more about chat-bots in great detail.

Now, we got familiar with that basic question- What is Chatbot?, here comes the other question- how it will me empowering my business and give users a prompt and personalized comfort?

Let’s consider a business case here, assume you are running an E-commerce website. The marketplace servers more than 20 thousand daily visitors but with a very low conversion ratio i.e 5-10 closable orders a day.

Now here is the thing, the bot can help you sell more if you're running an e-commerce store.

As we know ,everyone likes to check out all the details about the product, he or she interested to buy in a physical store but that is certainly not possible in an online store.

So why not to create a Chatbot on Facebook messenger platform to drive better engagement with website visitors in real-time, and respond to customer queries, to make them feel more personalized and connected to your e-commerce platform.

Similarly, there are many other related customer service examples that are built using Facebook Messenger platform such as CNN (The Cable News Network is an American basic cable and satellite television news channel owned by the Turner Broadcasting System division of Time Warner.) , KAYAK( is a fare aggregator and travel metasearch engine operated by the The Priceline Group)

I know things are getting Interesting now, you can’t wait any more, so let’s get started with our first Chatbot built over Facebook messenger platform.

So here is a Step by Step Guide for setting Facebook Messenger Chatbot-

Prerequisites

  • UBUNTU 16.04 VPS with NGINX , PYTHON , PIP , GUNICORN
  • FACEBOOK DEVELOPER ACCOUNT
  • SSL CERTIFICATE FOR DOMAIN

Assuming you have all the above pre-requisites ready up and running, here we start with commands:

  1. SSH into your VPS and create a directory ./messenger in /var/www/

          sudo mkdir -p /var/www/messenger

  1. Go to link messenger.py and Copy Messenger.py file and create in messenger directory
  2. Setup messenger.py file to listen on 443 on Nginx and use gunicorn to configure the sock file to pass in proxy settings
  3. You can have a look at the following Nginx Conf file and adjust according to your settings messenger-nginx.conf

If you are not sure how to setup the above 4 steps then here are some useful references.  SSL CONFIG: lets encrypt

GUNICORN FLASK NGINX : nginx gunicorn Ubuntu

Now we are all set with our secure domain running the messeenger code, so lets create facebook an app.

  1. Goto https://developers.facebook.com and click on top Right to create a new App
  2. Enter Display name and contact email and click on create APP

  1. Now App is created with a unique App ID , click on ADD Product

Facebook_Messenger_Add_App

  1. Scroll to Messenger and click Get started.
  1. Facebook will ask for the permissions grant for your New App created.

Facebook_Messenger_App

  1. Review the Permissions Correctly

Facebook_Messenger

  • Now Choose the a facebook page from the list of pages you created on facebook and incase you don’t have any page then add a new page accordingly.

  1. Setup Webhooks now by selecting the services. So in this you will need to provide 2 inputs 1st is Secure facebook callback Url which we just created initially and 2nd is Verify Token. Be careful with the Verify token becuase you will need to use the same in your messenger.py which is available on messenger.py

          Now that you have verified all the inputs click on verify and save.

          Remember facebook will not allow you to save until you secret_token matches on both messenger.py and this Input.

Facebook_Messenger_Token

  •    Once all the inputs are correct you will be redirected and success message of complete will be there

Facebook_Messenger_Webhook

  1. Now Subscribe to the newly created page below and we are done thats’it.

Remember the APP is not published yet, so only user logged in Facebook can access this functionality. If you need to add more users then you can add them as Testers and Developers until your APP is Live for public.

For Making your APP live you will need to submit for review to facebook. Currently in the above code on github we have set our bot to reply with same message which you will be type.

Example -->

  • You : hello there!
  • Bot : you wrote hello there!
  • You : OK!
  • Bot : you wrote OK!

Now you can edit the messenger.py file according to your Business Logic and echo on basis of Artificial Intelligence Or Knowledge base.

About The Author
Mayank Grover
Mayank Grover Mayank Grover is a Technology Architect at Techpillar. He is a seasoned technologist with rich experience in software & systems architecture, product strategy, roadmap and management.