16th Dec 2018

Building a Facebook Echo-Bot in Python


Author

Mayank Bajaj

Software Engineer

6 min readBackend Engineering
6 min readBackend Engineering

Gartner estimates that by 2020, chatbots will be handling 85 percent of customer-service interactions; they are already handling about 30 percent of transactions now.

In this blog we will

  • Create a Facebook app.
  • Create a Facebook page.
  • Enable the messaging service for your Facebook app and setup the Webhook.

Go to https://developers.facebook.com and create your developer account by providing the various details.

Then click on My Apps and then navigate to Add New App.

After creating the app it will show up in left navigation app of your Dev Portal.

Click on Products , and add Messenger Service to your app.

Assuming that you have preinstalled python in your PC. Its time to install the other dependencies needed for our bot.

pip install flaskpip install git+https://github.com/nikhilkumarsingh/pymessenger.git

Now we are ready with all weapons to make our bot.

Initial code to connect to BOT via pymessenger.

from flask import Flask, request
from pymessenger import Botapp = Flask("My echo bot")
FB_ACCESS_TOKEN = "PAGE_ACCESS_TOKEN_HERE"
bot = Bot(FB_ACCESS_TOKEN)
VERIFICATION_TOKEN = "TOKEN007"

@app.route('/', methods=['GET'])

def verify(): if request.args.get("hub.mode") == "subscribe" and request.args.get("hub.challenge"): if not request.args.get("hub.verify_token") == VERIFICATION_TOKEN: return "Verification token mismatch", 403 return request.args["hub.challenge"], 200 return "Its Working", 200

Handling the incoming messages

@app.route('/', methods=['POST'])

def webhook(): data = request.get_json()

Request from the Facebook is now stored in your data variable.

#data
{'entry': [{'id': '1361232110605734',
            'messaging': [{'message': {'app_id': 1603387687,
                                       'is_echo': True,
                                       'mid': 'mid.$cAASM_deujm5mqtsdsdsgeZB_rCKAL',
                                       'seq': 183656,
                                       'text': 'Hello Buddy'},
                           'recipient': {'id': '130897260224'},
                           'sender': {'id': '13612321734'},
                           'timestamp': 1513868001583}],
            'time': 1513868001147}],
 'object': 'page'}

Our message text is in data[‘entry’][‘messaging’][‘text’] and sender id in data[‘entry’][‘sender’][‘text’] respectively.

To send the message back to the sender.

bot.send_text_message(sender_id, message)

Now its time to start the server.

if __name__ == "__main__": 
   app.run(port=8000, use_reloader = True)

If your app runs fine on local it time to deploy it on cloud to get a callback URL.

Heroku or Digital Ocean Droplets provides fast and reliable cloud solutions.

After deploying, go to your Facebook app setting and setup the Webhoook.

Press Verify and Save and your Echo Bot is ready to interact. If you had any problems while running the bot, here is the Code.

We bring the best in technology combined with an Agile process which results in higher efficiency and desired results for our clients.

Talk to us!

LATEST FROM OUR BLOG

Building a Facebook Echo-Bot in Python

Gartner estimates that by 2020, chatbots will be handling 85 percent of customer-service interactions; they are already handling about 30 percent of transactions now.

Read more

How to Send an Email from Flutter - SMS App Part 3

In this blog, we will see how we can send emails from our flutter application using mailgun credentials. This post is in continuation of the Flutter SMS app series.

Read more

How To Listen for SMS using Flutter - SMS App Part 2

In continuation with the previous blog, we would continue to build our new basic SMS app using Flutter. In this blog, we would see how we can add the functionality of listening for incoming SMS messages on the device.

Read more