Facebook Messenger Bot With Flask & Google App Engine Nazrul Kamaruddin [email protected] PyCon Malaysia 2016
Who am i?
@nazrollSoftware Engineer, Sparkline Pte. Ltd.Organizer, Google Developer Groups Kuala Lumpur
Previously: Media Prima Digital (Digital Media & Online Services), Chalkboard (Hyperlocal Ad Network) & a long list of adventures.
Building software for the World Wide Web since 1999.
First things first...
Local development setup
Python (2.7 and above)
pip
virtualenv
Google Cloud SDK (http://cloud.google.com/sdk)
Install the Python component from Google Cloud SDK
First things first...
Facebook Page & Messenger API setup
It takes about 5 steps. RTFM PLZ http://goo.gl/gUKgA0
Caveats
Testing your code with localhost:8080 does not work with Facebook Messenger
CANNOT USE LOCALHOST:8080?
NOOOOOOOOOOOOOOOOOOOOOOO
Caveats
Testing your code with localhost:8080 does not work with Facebook Messenger
Testing your bot can only happen in a live environment. Literally.
Create a Google Cloud project
Why Google App Engine?
It just scale for you automatically.
That’s it.
Nothing else to say.
Get the App Engine application URL
Example URL for your webhook: https://noobotkit.appspot.com
Setup a Webhook
http://goo.gl/gUKgA0
Put that URL here
http://noobotkit.appspot.com/webhook
Summary
1. Setup local development with Google Cloud SDK installed
2. Setup your Google Cloud project.
a. Save the project URL for App Engine: https://myprojectname.appspot.com
3. Setup your Facebook Messenger App
a. Use the URL, e.g http://myprojectname.appspot.com, as the webhook URL.
Let’s build something
Clone the repohttps://github.com/nazroll/noobotkit
Let’s look into requirements.txt
Only need the flask-restful package(s)
main.py
Create a RESTful URL route
Why Flask? Why RESTful?
Flask is a framework that with a small footprint IMHO
You can use other frameworks like Bottle, etc. Your choice amigo.
flask-restful allows me to have a clean structure.
HTTP request as a method. Example:
HTTP POST > def post:
HTTP GET > def get:
bot/facebook.pyHandle GET requests.Only useful for token verification.
Handle POST requests.This is where the action is. FB only send payload as HTTP POST to the bot app.
bot/facebook.pyHandling text inputsIt’s like a SMS shortcode.A set of text commands to request for data (news stories, event schedule, etc).
bot/facebook.pyexample_message_textText only reply from the app.
recipient_id:Facebook ID of the user
message:The text message that you want respond back to the user.
bot/facebook.py example_message_imageText + Image reply from the app.
recipient_id:Facebook ID of the user
message:The image payload that you want to send to user and display on the Messenger app(s).attachment
type:payload:
url:
bot/facebook.py
Sending the response back to FBMust include the Facebook Page access tokenUse urlfetch to make those requests
DEMO
Summary
1. Setup local development with Google Cloud SDK installed
2. Setup your Google Cloud project.
a. Save the project URL for App Engine: https://myprojectname.appspot.com
3. Setup your Facebook Messenger App
a. Use the URL, e.g http://myprojectname.appspot.com, as the webhook URL.
4. To test your bot, you have to push it to a live instance.
5. Flask’s flask-restful library gives you a production ready interface to build RESTful endpoints
6. There’s a lot more different types of responses that you can create.
RTFM: http://goo.gl/2GVAXC
7. You are welcome to contribute in the Github project!
Referencesnoobotkithttps://github.com/nazroll/noobotkit
facebook-chatbot-pythonhttps://github.com/hult/facebook-chatbot-python
PyMessengerhttps://github.com/enginebai/PyMessager
How to build and deploy a Facebook Messenger bot with Python and Flaskhttp://goo.gl/LS6Qgg
Facebook Messenger API docshttps://developers.facebook.com/docs/messenger-platform