Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
49 lines (39 loc) · 1.75 KB
/
Copy pathapp.py
File metadata and controls
49 lines (39 loc) · 1.75 KB
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
"""A Replyze webhook receiver in Flask that checks every request.
pip install -r requirements.txt
REPLYZE_WEBHOOK_SECRET=... flask --app app run --port 3000
Then add http(s)://<your host>/replyze/webhook in
Settings > Integrations > Developer > Webhooks.
"""
import json
import os
from flask import Flask, abort, request
from verify import is_fresh, verify_replyze_signature
SECRET = os.environ.get("REPLYZE_WEBHOOK_SECRET")
if not SECRET:
raise SystemExit("Set REPLYZE_WEBHOOK_SECRET (the signing secret shown when you added the webhook).")
app = Flask(__name__)
# Keep events you have handled; use your database in production.
seen: set[str] = set()
@app.post("/replyze/webhook")
def replyze_webhook():
# get_data() is the exact bytes: the signature is over the raw body, not re-serialized JSON.
raw = request.get_data()
if not verify_replyze_signature(raw, request.headers.get("X-Replyze-Signature"), SECRET):
abort(401)
# Signed, so now it is safe to parse. Take the time and the id from the body:
# the signature covers it. X-Replyze-Timestamp and X-Replyze-Event-Id are
# unsigned copies that anyone replaying an old request can change.
event = json.loads(raw)
if not is_fresh(event.get("created_at")):
abort(400)
if event["id"] in seen:
return "", 200 # a retry of an event you already have
# Only needs to remember ids for as long as is_fresh lets an event in (5 minutes).
seen.add(event["id"])
if event["event"] == "message.created":
m = event["data"]["message"]
print(f"[{m['sender_type']}] in #{m['conversation_id']}: {m['content']}")
else:
print(event["event"], json.dumps(event["data"]))
# Answer fast: hand slow work to a queue.
return "", 200