Facebook Messenger Bot with Flask & Google App Engine

31
Facebook Messenger Bot With Flask & Google App Engine Nazrul Kamaruddin [email protected] PyCon Malaysia 2016

Transcript of Facebook Messenger Bot with Flask & Google App Engine

Page 1: Facebook Messenger Bot with Flask & Google App Engine

Facebook Messenger BotWith Flask & Google App Engine

Nazrul [email protected]

PyCon Malaysia 2016

Page 2: Facebook Messenger Bot with Flask & Google App Engine

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.

Page 3: Facebook Messenger Bot with Flask & Google App Engine
Page 4: Facebook Messenger Bot with Flask & Google App Engine

First things first...

Local development setup

Python (2.7 and above)

pip

virtualenv

Google Cloud SDK (http://cloud.google.com/sdk)

Page 5: Facebook Messenger Bot with Flask & Google App Engine

Install the Python component from Google Cloud SDK

Page 6: Facebook Messenger Bot with Flask & Google App Engine

First things first...

Facebook Page & Messenger API setup

It takes about 5 steps. RTFM PLZ http://goo.gl/gUKgA0

Page 7: Facebook Messenger Bot with Flask & Google App Engine

Create an Facebook App & a Page

http://goo.gl/gUKgA0

Page 8: Facebook Messenger Bot with Flask & Google App Engine

Setup a Webhook

http://goo.gl/gUKgA0

Page 9: Facebook Messenger Bot with Flask & Google App Engine

Caveats

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

Page 10: Facebook Messenger Bot with Flask & Google App Engine

CANNOT USE LOCALHOST:8080?

NOOOOOOOOOOOOOOOOOOOOOOO

Page 11: Facebook Messenger Bot with Flask & Google App Engine

Caveats

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

Testing your bot can only happen in a live environment. Literally.

Page 12: Facebook Messenger Bot with Flask & Google App Engine
Page 13: Facebook Messenger Bot with Flask & Google App Engine

Create a Google Cloud project

Page 14: Facebook Messenger Bot with Flask & Google App Engine

Why Google App Engine?

It just scale for you automatically.

That’s it.

Nothing else to say.

Page 15: Facebook Messenger Bot with Flask & Google App Engine

Get the App Engine application URL

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

Page 16: Facebook Messenger Bot with Flask & Google App Engine

Setup a Webhook

http://goo.gl/gUKgA0

Put that URL here

http://noobotkit.appspot.com/webhook

Page 17: Facebook Messenger Bot with Flask & Google App Engine

Get FB Page Access Token

http://goo.gl/gUKgA0

Page 18: Facebook Messenger Bot with Flask & Google App Engine

Subscribe the App to a Page

http://goo.gl/gUKgA0

Page 19: Facebook Messenger Bot with Flask & Google App Engine

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.

Page 20: Facebook Messenger Bot with Flask & Google App Engine

Let’s build something

Clone the repohttps://github.com/nazroll/noobotkit

Let’s look into requirements.txt

Only need the flask-restful package(s)

Page 21: Facebook Messenger Bot with Flask & Google App Engine

main.py

Create a RESTful URL route

Page 22: Facebook Messenger Bot with Flask & Google App Engine

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:

Page 23: Facebook Messenger Bot with Flask & Google App Engine

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.

Page 24: Facebook Messenger Bot with Flask & Google App Engine

bot/facebook.pyHandling text inputsIt’s like a SMS shortcode.A set of text commands to request for data (news stories, event schedule, etc).

Page 25: Facebook Messenger Bot with Flask & Google App Engine

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.

Page 26: Facebook Messenger Bot with Flask & Google App Engine

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:

Page 27: Facebook Messenger Bot with Flask & Google App Engine

bot/facebook.py

Sending the response back to FBMust include the Facebook Page access tokenUse urlfetch to make those requests

Page 28: Facebook Messenger Bot with Flask & Google App Engine

DEMO

Page 29: Facebook Messenger Bot with Flask & Google App Engine

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!

Page 30: Facebook Messenger Bot with Flask & Google App Engine

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

Page 31: Facebook Messenger Bot with Flask & Google App Engine

Thank [email protected]

nazroll.com

https://github.com/nazroll/noobotkit