Every day there are more and more chat bots which are useful for various businesses and even individuals. Imagine that you have created simple app with few options and you do not want (or you are too lazy) to make frontend (or even REST API) for it. You have limited number of users which are willing to use your app and are happy with simple things that do not require install or registration. Chat bots come handy in this situation. In this article, I will explain how to set up your own chat bot on Facebook Messenger with library I have developed. Currently library only supports text messages which is enough in most applications but in the future I plan to introduce more functionality.

Installation

Library is Python library which you can download here.

To install it simply run:

python setup.py install

Make sure your user have proper privileges or to run it inside virtualenv.

Now we can make simple “hello world” bot. Just create your Python script with the following code:

from src import * 
class MyBot(WebHookServer):
	def handle_message(self, sender_id, text, timestamp):
		if text == "hello":
			return "hello!"
TOKEN = ""
VERIFY_TOKEN = ""
MyBot(TOKEN, port=563, verify_token=VERIFY_TOKEN).serve()

Let’s analyse what have we done here. First of all, we subclass WebHookServer and implement function on_message. This is function which is called when our bot receives message and provides access to ID of sender, text of message and timestamp of message. Function should return response to message. If it returns None or “” then bot won’t respond to given message. If function returns some string, that string will be response from our bot. As simple as that!

Then we have defined two variables, token and verify token. They will be set properly when we register our bot on Messenger developer panel. In the end, we run our bot as web server. When we register our bot to Messenger, every message will be passed to this server and server will respond in a form of HTTP request to Messenger.

Registration to Messenger

Now we have to register our bot to Messenger. First of all, we need to set up web server properly to use SSL and to correctly route to our bot instance. I will give example of nginx configuration:

        location /webhook/ {
            proxy_set_header Host $host;
            proxy_pass http://127.0.0.1:563/;
        }

As you can see, we just need to redirect requests from /webhook location on our web server to internal bot running on specified port. We also need to set up HTTPS, please see here.

Now we have to register our bot, so go to Developer console and go to “My apps” and and click on “New app”. If you are not registered to developer console, you will need to do it first. When creating app, you will enter URL to your webhook and your verification token. Also you will see your token and you need to update these two variables in code and re-run app. After that, you will be prompted from developer console to verify your application. You just click to verify and library will handle verification for you. After that, you will be able to chat with your bot. :)