-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathYNCFoodbotserver.py
113 lines (87 loc) · 3.61 KB
/
YNCFoodbotserver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import json
import time
import requests
from flask import Flask, request
import message_objects as msg
import match_keyword as match
app = Flask(__name__)
PAT = 'EAAZAygcjNS3sBAPG5AC9WEt9FFm3Fi8DZBjb24POoGgm5OpidWyzAJVDHy7bD4ZCsAK9XUzRVnXaCbeopf0RuWaKlvHdvefZBE2SASfivlCPZAC96GBCK9XQCMlVUSkxPxJMxVr7MN3ibJRQ3zJA3ZA7IhjUJ4rT2b7UmAiR5DZAgZDZD '
@app.route('/', methods=['GET'])
def handle_verification():
print "Handling Verification."
if request.args.get('hub.verify_token', '') == 'my_voice_is_my_password_verify_me':
print "Verification successful!"
return request.args.get('hub.challenge', '')
else:
print "Verification failed!"
return 'Error, wrong validation token'
@app.route('/', methods=['POST'])
def handle_messages():
print "Handling Messages"
payload = request.get_data()
data = json.loads(payload)
if isinstance(data, dict):
for sender, message in messaging_events(data):
#print "Incoming from %s: %s" % (sender, message)
print "*************** SENDING RESPONSE: *************** \n", message, "\n"
send_message(sender, message)
return "ok"
elif isinstance(data, list):
if data[0]["gdrive_cms"] == True:
handle_CMS_Json(data[0])
return "ok"
else:
return "json not from gdrive_cms"
else:
print "Json array or obj?"
def handle_CMS_Json(data):
print "MADE CONTACT", data, data["body"]
def messaging_events(data):
"""Generate tuples of (sender_id, message_text) from the
provided payload.
"""
messaging_events = data["entry"][0]["messaging"]
for event in messaging_events:
# IF TEXT MSG:
sender_id = event["sender"]["id"]
if "postback" in event and "payload" in event["postback"]:
postback = event["postback"]["payload"]
print "############### RECEIVED ###############\n############### POSTBACK: ###############\n", postback, "\n"
#responses = match_text_or_payload(postback)
responses = match.match_text_or_payload(postback, sender_id)
elif "message" in event and "text" in event["message"]:
message_text = event["message"]["text"]
print "############### RECEIVED ###############\n############### MESSAGE: ###############\n", message_text.encode("unicode-escape"), "\n"
responses = match.match_text_or_payload(message_text, sender_id)
# ELSE IF POSTBACK:
# ELSE (NOT TEXT MSG && NOT POSTBACK):
else:
print "ERROR: message not text or postback"
responses = msg.sorry_msg
for response in responses:
yield sender_id, response
def send_message(recipient, message):
"""Send the message text to recipient with id recipient.
"""
r = requests.post("https://graph.facebook.com/v2.6/me/messages",
params={"access_token": PAT},
data=json.dumps({
"recipient": {"id": recipient},
"message": message
}),
headers={'Content-type': 'application/json'})
if r.status_code != requests.codes.ok:
print r.text
if __name__ == '__main__':
app.run()
#GIT
# git add .
# git commit -m "commit"
# git push heroku master
#heroku logs -t
#TESTING WITH SHELL SCRIPT
# create test.sh.
# Add permission: chmod +x ./test.sh (https://askubuntu.com/questions/38661/how-do-i-run-sh-files).
# Run python server file: $ python YNCFoodbotserver.py
# ./test.sh
# Unsubscribe to page to ensure messages dont get sent to heroku while testing mode.