-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflask_server.py
More file actions
38 lines (26 loc) · 1.04 KB
/
Copy pathflask_server.py
File metadata and controls
38 lines (26 loc) · 1.04 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
"""Minimal Flask receiver for EveryPage webhooks.
The one gotcha: verification needs the RAW body bytes. Use
request.get_data() - request.json parses (and can re-order) them.
"""
import json
import os
from flask import Flask, request
from verify import verify
# The full secret from webhook creation, whsec_ prefix included.
SECRET = os.environ["EVERYPAGE_WEBHOOK_SECRET"]
app = Flask(__name__)
@app.post("/webhooks/everypage")
def everypage_webhook():
body = request.get_data() # raw bytes, before any parsing
header = request.headers.get("X-Everypage-Signature", "")
if not verify(SECRET, body, header):
return "invalid signature", 400
# Safe to parse now: {event, timestamp, data:{fileUuid, fileName, ...}}.
envelope = json.loads(body)
data = envelope["data"]
print(f"{envelope['event']} at {envelope['timestamp']}: "
f"{data['fileName']} ({data['fileUuid']})")
# Any 2xx counts as delivered. Respond fast; do slow work async.
return "", 204
if __name__ == "__main__":
app.run(port=3000)