import hashlib, hmac, os, time
from flask import Flask, request, abort
app = Flask(__name__)
SECRET = os.environ["TELLEO_WEBHOOK_SECRET"].encode()
@app.post("/telleo/webhook")
def telleo_webhook():
raw = request.get_data() # raw bytes, before any JSON parsing
parts = dict(p.split("=", 1) for p in request.headers.get("Telleo-Signature", "").split(",") if "=" in p)
t, sig = parts.get("t", ""), parts.get("v1", "")
expected = hmac.new(SECRET, t.encode() + b"." + raw, hashlib.sha256).hexdigest()
if not t.isdigit() or abs(time.time() - int(t)) > 300 or not hmac.compare_digest(sig, expected):
abort(400)
event = request.get_json()
queue_for_processing(event) # keep the response fast
return "", 200