09 Companion hook_receiver.py (1/2)

File: examples/companions/event-webhooks/hook_receiver.py (44 lines)

#!/usr/bin/env python3
"""Prints CrossPoint plugin events and serves /sleep.bmp.
   HOOK_SECRET=... python3 hook_receiver.py   -> :8090.  Put a sleep.bmp next to this file.
   The secret must match the one saved on the plugin's web page.
"""
import json, os, pathlib
import sys
sys.stdout.reconfigure(line_buffering=True)   # print events immediately, also when piped
from http.server import ThreadingHTTPServer, BaseHTTPRequestHandler

SECRET = os.environ.get("HOOK_SECRET", "change-me")
SEEN = set()                                   # event ids -> at-least-once dedupe
HERE = pathlib.Path(__file__).parent

class H(BaseHTTPRequestHandler):
    def reply(self, code, body=b"ok", ctype="text/plain"):
        self.send_response(code)
        self.send_header("Content-Type", ctype)
        self.send_header("Content-Length", str(len(body)))
        self.end_headers()
        self.wfile.write(body)
‹ PreviousReader file (.txt)Next ›