-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
73 lines (59 loc) · 2.46 KB
/
app.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
import os
from flask import request
from flask import Flask
from gcp import access_secret_version, get_notifications, delete_notifications
from config import FB_CHALLENGE, PROJECT_ID
import logging_handler
import logging
from fb import handleMessage, handlePostback, handleOptin, generate_one_time_template
from datetime import date, timedelta
app = Flask(__name__)
CHALLENGE = access_secret_version(
PROJECT_ID, FB_CHALLENGE["name"], FB_CHALLENGE["version"]
)
@app.route("/webhook", methods=["GET"])
def verify():
# when the endpoint is registered as a webhook, it must echo back
# the 'hub.challenge' value it receives in the query arguments
if request.args.get("hub.mode") == "subscribe" and request.args.get(
"hub.challenge"
):
if not request.args.get("hub.verify_token") == CHALLENGE:
return "Verification token mismatch", 403
return request.args["hub.challenge"], 200
return "Waiting for verification", 200
@app.route("/webhook", methods=["POST"])
def webhook():
data = request.json
print(data)
if data["object"] == "page":
for entry in data["entry"]:
for messaging_event in entry["messaging"]:
psid = messaging_event.get("sender").get("id")
if messaging_event.get("message"): # someone sent us a message
handleMessage(psid, messaging_event.get("message"))
elif messaging_event.get("postback"):
handlePostback(psid, messaging_event.get("postback"))
elif messaging_event.get("optin"):
handleOptin(psid, messaging_event.get("optin"))
return "ok", 200
else:
return "Unknown object in body", 404
@app.route("/notifications", methods=["GET"])
def notifications():
today = date.today()
yesterday = date.today() - timedelta(days=1)
today = str(today.year) + str(today.month) + str(today.day)
yesterday = str(yesterday.year) + str(yesterday.month) + str(yesterday.day)
notifications = get_notifications(today)
y_notifs = get_notifications(yesterday)
delete_notifications(today)
delete_notifications(yesterday)
notifications = notifications + y_notifs
for notif in notifications:
generate_one_time_template(
notif["token"], int(notif["cur_assignment"]), int(notif["skill"])
)
return "ok", 200
if __name__ == "__main__":
app.run(debug=True, host="0.0.0.0", port=int(os.environ.get("PORT", 8081)))