Skip to content

Instantly share code, notes, and snippets.

@ntuaha
Last active November 28, 2018 01:54
Show Gist options
  • Save ntuaha/7922a876e7b18e8b731979dc9c488f1b to your computer and use it in GitHub Desktop.
Save ntuaha/7922a876e7b18e8b731979dc9c488f1b to your computer and use it in GitHub Desktop.
Python version of FB bot Google Cloud Functions
from flask import abort
import requests
def getToken(request):
VERIFY_TOKEN = "test"
mode = request.args.get('hub.mode')
token = request.args.get('hub.verify_token')
challenge = request.args.get('hub.challenge')
# Checks if a token and mode is in the query string of the request
if mode and token:
# Checks the mode and token sent is correct
if (mode == 'subscribe') and (token == VERIFY_TOKEN):
# Responds with the challenge token from the request
print('WEBHOOK_VERIFIED')
return challenge
else:
# Responds with '403 Forbidden' if verify tokens do not match
return abort(403)
return abort(405)
def handle_event(e):
recipient = e['sender']['id']
messageData = {"text": e['message']['text']+" aha"}
opt = {"messaging_type": "RESPONSE", "recipient": {
"id": recipient}, "message": messageData}
token = [token]
result = requests.post(
'https://graph.facebook.com/v3.2/me/messages?access_token={}'.format(token), data=opt)
print(result)
messageData = {"text": e['message']['text']+" aha"}
api_url = 'https://graph.facebook.com/v2.9/me/messages?access_token=%s'%token
headers = {'Content-Type': 'application/json'}
payload = {
'recipient': {'id':e['sender']['id']},
"messaging_type": "RESPONSE",
'message': messageData
}
print(payload)
r = requests.post(api_url,json=payload,headers=headers)
def ai(request):
request_json = request.get_json(silent=True)
# Checks this is an event from a page subscription
if request_json['object'] == 'page':
# Iterates over each entry - there may be multiple if batched
for entry in request_json['entry']:
# Gets the message. entry.messaging is an array, but
# will only ever contain one message, so we get index 0
webhook_event = entry['messaging'][0]
handle_event(webhook_event)
print(webhook_event)
# Returns a '200 OK' response to all requests
return 'EVENT_RECEIVED'
else:
# Returns a '404 Not Found' if event is not from a page subscription
abort(404)
def chatbot(request):
"""Responds to any HTTP request.
Args:
request (flask.Request): HTTP request object.
Returns:
The response text or any set of values that can be turned into a
Response object using
# flask.Flask.make_response>`.
`make_response <http://flask.pocoo.org/docs/1.0/api/
"""
# request_json = request.get_json()
# if request.args and 'message' in request.args:
# return request.args.get('message')
# elif request_json and 'message' in request_json:
# return request_json['message']
# else:
# return f'Hello World!'
if request.method == 'GET':
return getToken(request)
# return 'Hello World!'
elif request.method == 'POST':
# return abort(403)
return ai(request)
else:
return abort(405)
# Function dependencies, for example:
# package>=version
requests>=2.20.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment