Created
March 29, 2017 10:40
-
-
Save anonymous/5f7f59bc967c6f240f2bdf6ce739f81e to your computer and use it in GitHub Desktop.
Confusion with Flask
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import sys | |
import json | |
import time | |
import requests | |
from flask import Flask, request | |
prior_payload = {} | |
app = Flask(__name__) | |
@app.route('/', methods=['POST']) | |
def webhook(): | |
log(prior_payload.keys()) | |
log(prior_payload.values()) | |
# endpoint for processing incoming messaging events | |
data = request.get_json() | |
if data["object"] == "page": | |
for entry in data["entry"]: | |
for messaging_event in entry["messaging"]: #user sent a message | |
sender_id = str(messaging_event["sender"]["id"]) | |
#if sender not currently stored in dictionary (prior payload outside of webhook) then set the id as key, and value as 0 until an action has taken place | |
if sender_id not in prior_payload.keys(): | |
prior_payload[sender_id] = 0 | |
if messaging_event.get("message"): # someone sent us a message | |
if "attachments" in messaging_event['message']: | |
sender_id = str(messaging_event["sender"]["id"]) | |
recipient_id = messaging_event["recipient"]["id"] | |
payload = "uploaded random file" | |
mistake_message(sender_id, payload ,prior_payload[sender_id]) | |
elif messaging_event["message"]["text"] == 'done' or messaging_event["message"]["text"] == 'Done': | |
sender_id = str(messaging_event["sender"]["id"]) | |
payload = messaging_event["message"]["text"] | |
send_qresponse(sender_id, payload) | |
elif "quick_reply" in messaging_event["message"]: | |
sender_id = str(messaging_event["sender"]["id"]) | |
payload = messaging_event["message"]["quick_reply"]['payload'] | |
prior_payload[sender_id] = None | |
prior_payload[sender_id] = str(payload) | |
send_qresponse(sender_id, payload) | |
elif messaging_event['message']['text'] == "ready" or messaging_event['message']['text'] == "Ready": | |
sender_id = str(messaging_event["sender"]["id"]) | |
payload = messaging_event["message"]["text"] | |
send_qresponse(sender_id, payload) | |
#This is the conditional for open ended questions. Hence, why it's now using the dictionary as the conditional rather than the json postback from the user (like above). | |
#This executes as such - If a message is received (above in the for loop) and the previous action (or payload as it's stored) was read_vacancies then execute code. | |
elif prior_payload[sender_id] == "ready_vacancies": | |
sender_id = str(messaging_event["sender"]["id"]) | |
prior_payload[sender_id] = "second_question" | |
recipient_id = messaging_event["recipient"]["id"] | |
payload = "second_question" | |
send_qresponse(sender_id, payload) | |
elif prior_payload[sender_id] == "second_question": | |
sender_id = str(messaging_event["sender"]["id"]) | |
prior_payload[sender_id] = "third_question" | |
recipient_id = messaging_event["recipient"]["id"] | |
payload = "third_question" | |
send_qresponse(sender_id, payload) | |
elif prior_payload[sender_id] == "third_question": | |
sender_id = str(messaging_event["sender"]["id"]) | |
prior_payload[sender_id] = "fourth_question" | |
recipient_id = messaging_event["recipient"]["id"] | |
payload = "fourth_question" | |
send_qresponse(sender_id, payload) | |
else: | |
sender_id = str(messaging_event["sender"]["id"]) | |
recipient_id = messaging_event["recipient"]["id"] | |
payload = str(messaging_event["message"]["text"]) | |
mistake_message(sender_id, payload) | |
if messaging_event.get("delivery"): | |
pass | |
if messaging_event.get("optin"): | |
pass | |
if messaging_event.get("postback"): | |
sender_id = str(messaging_event["sender"]["id"]) | |
recipient_id = messaging_event["recipient"]["id"] | |
payload = str(messaging_event['postback']['payload']) | |
prior_payload[sender_id] = str(payload) | |
send_qresponse(sender_id, payload) | |
return "ok", 200 | |
#Example of two of the commands in send_qresponse(sender_id,payload) - one is the standard 'button' responses the other is an open ended question | |
def send_qresponse(recipient_id,payload): | |
if payload == "about_us": | |
#Send post to user with 3 button choices | |
log("sending message to {recipient}".format(recipient=recipient_id)) | |
params = { | |
"access_token": os.environ["PAGE_ACCESS_TOKEN"] | |
} | |
headers = { | |
"Content-Type": "application/json" | |
} | |
data = json.dumps({ | |
"recipient": { | |
"id": recipient_id | |
}, | |
"message": { | |
"text":"OK! Let's begin...", | |
"quick_replies":[ | |
{ | |
"content_type":"text", | |
"title":"Philosophy", | |
"payload":"philosophy", | |
}, | |
{ | |
"content_type":"text", | |
"title":"Team", | |
"payload":"team", | |
}, | |
{ | |
"content_type":"text", | |
"title":"Location", | |
"payload":"location", | |
} | |
] | |
} | |
}) | |
r = requests.post("https://graph.facebook.com/v2.6/me/messages", params=params, headers=headers, data=data) | |
if r.status_code != 200: | |
log(r.status_code) | |
log(r.text) | |
# send open ended question to user - this is based on the prior_payload variable being X after them typing a general message in response to the question, in the messenger | |
elif payload == "second_question": | |
log("sending message to {recipient}".format(recipient=recipient_id)) | |
params = { | |
"access_token": os.environ["PAGE_ACCESS_TOKEN"] | |
} | |
headers = { | |
"Content-Type": "application/json" | |
} | |
data = json.dumps({ | |
"recipient": { | |
"id": recipient_id | |
}, | |
"message": { | |
"text":"Nice, how many times have you practised that in the mirror? Up next..." | |
} | |
}) | |
r = requests.post("https://graph.facebook.com/v2.6/me/messages", params=params, headers=headers, data=data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment