Facebook Messenger Bot with Flask & Google App Engine

download
  • date post

    21-Jan-2017
  • Category

    Software
  • view

    152
  • download

    1

Embed Size (px)

transcript

Facebook Messenger BotWith Flask & Google App EngineNazrul Kamaruddinnazroll@gmail.comPyCon 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 setupPython (2.7 and above)pipvirtualenvGoogle Cloud SDK (http://cloud.google.com/sdk)

Install the Python component from Google Cloud SDK

First things first...Facebook Page & Messenger API setupIt takes about 5 steps. RTFM PLZ http://goo.gl/gUKgA0

http://goo.gl/gUKgA0

Create an Facebook App & a Page

http://goo.gl/gUKgA0

Setup a Webhookhttp://goo.gl/gUKgA0

CaveatsTesting your code with localhost:8080 does not work with Facebook Messenger

CANNOT USE LOCALHOST:8080?NOOOOOOOOOOOOOOOOOOOOOOO

CaveatsTesting your code with localhost:8080 does not work with Facebook MessengerTesting your bot can only happen in a live environment. Literally.

NEXT: Create a google cloud project to give you a LIVE url.

Create a Google Cloud project

Why Google App Engine?It just scale for you automatically. Thats it. Nothing else to say.

Get the App Engine application URL

Example URL for your webhook: https://noobotkit.appspot.com

Setup a Webhookhttp://goo.gl/gUKgA0

Put that URL herehttp://noobotkit.appspot.com/webhook

Get FB Page Access Tokenhttp://goo.gl/gUKgA0

Subscribe the App to a Pagehttp://goo.gl/gUKgA0

SummarySetup local development with Google Cloud SDK installedSetup your Google Cloud project.Save the project URL for App Engine: https://myprojectname.appspot.comSetup your Facebook Messenger AppUse the URL, e.g http://myprojectname.appspot.com, as the webhook URL.

Lets build somethingClone the repohttps://github.com/nazroll/noobotkit

Lets look into requirements.txt

Only need the flask-restful package(s)

main.pyCreate a RESTful URL route

Why Flask? Why RESTful?Flask is a framework that with a small footprint IMHOYou 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 inputsIts like a SMS shortcode.A set of text commands to request for data (news stories, event schedule, etc).

Show demo

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.

Show demo

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).attachmenttype:payload:url:

Show demo

bot/facebook.pySending the response back to FBMust include the Facebook Page access tokenUse urlfetch to make those requests

Show demo

DEMO

SummarySetup local development with Google Cloud SDK installedSetup your Google Cloud project.Save the project URL for App Engine: https://myprojectname.appspot.comSetup your Facebook Messenger AppUse the URL, e.g http://myprojectname.appspot.com, as the webhook URL.To test your bot, you have to push it to a live instance.Flasks flask-restful library gives you a production ready interface to build RESTful endpointsTheres a lot more different types of responses that you can create. RTFM: http://goo.gl/2GVAXCYou are welcome to contribute in the Github project!

Referencesnoobotkithttps://github.com/nazroll/noobotkitfacebook-chatbot-pythonhttps://github.com/hult/facebook-chatbot-pythonPyMessengerhttps://github.com/enginebai/PyMessagerHow to build and deploy a Facebook Messenger bot with Python and Flaskhttp://goo.gl/LS6QggFacebook Messenger API docshttps://developers.facebook.com/docs/messenger-platform

Thank you!nazroll@gmail.comnazroll.comhttps://github.com/nazroll/noobotkit