diff options
| author | 2025-05-30 19:03:49 +0200 | |
|---|---|---|
| committer | 2025-05-30 19:03:49 +0200 | |
| commit | e1b6ea0cbee222839185cdb608bc729c970dd1ec (patch) | |
| tree | d2781952a10a5b1ce08690e236ad85fef80c3163 | |
| parent | Updated RELEASE_NOTES. (diff) | |
| download | snac2-e1b6ea0cbee222839185cdb608bc729c970dd1ec.tar.gz snac2-e1b6ea0cbee222839185cdb608bc729c970dd1ec.tar.xz snac2-e1b6ea0cbee222839185cdb608bc729c970dd1ec.zip | |
auto_follower_webhook.py new example program.
| -rwxr-xr-x | examples/auto_follower_webhook.py | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/examples/auto_follower_webhook.py b/examples/auto_follower_webhook.py new file mode 100755 index 0000000..e1192c1 --- /dev/null +++ b/examples/auto_follower_webhook.py | |||
| @@ -0,0 +1,55 @@ | |||
| 1 | #!/usr/bin/env python3 | ||
| 2 | |||
| 3 | # This is an example of a snac webhook that automatically follows all new followers. | ||
| 4 | |||
| 5 | # To use it, set the variables snac_basedir and snac_user to something reasonable, | ||
| 6 | # configure the user webhook to be http://localhost:12345, and run this program. | ||
| 7 | |||
| 8 | # copyright (C) 2025 grunfink et al. / MIT license | ||
| 9 | |||
| 10 | from http.server import BaseHTTPRequestHandler, HTTPServer | ||
| 11 | import time | ||
| 12 | import json | ||
| 13 | import os | ||
| 14 | |||
| 15 | host_name = "localhost" | ||
| 16 | server_port = 12345 | ||
| 17 | snac_basedir = "/var/snac/example_instance" | ||
| 18 | snac_user = "example_user" | ||
| 19 | |||
| 20 | class SnacAutoResponderServer(BaseHTTPRequestHandler): | ||
| 21 | |||
| 22 | def do_POST(self): | ||
| 23 | self.send_response(200) | ||
| 24 | self.end_headers() | ||
| 25 | |||
| 26 | content_type = self.headers["content-type"] | ||
| 27 | content_length = int(self.headers["content-length"]) | ||
| 28 | payload = self.rfile.read(content_length).decode("utf-8") | ||
| 29 | |||
| 30 | if content_type == "application/json": | ||
| 31 | try: | ||
| 32 | noti = json.loads(payload) | ||
| 33 | |||
| 34 | type = noti["type"] | ||
| 35 | |||
| 36 | if type == "Follow": | ||
| 37 | actor = noti["actor"] | ||
| 38 | cmd = "snac follow %s %s %s" % (snac_basedir, snac_user, actor) | ||
| 39 | |||
| 40 | os.system(cmd) | ||
| 41 | |||
| 42 | except: | ||
| 43 | print("Error parsing notification") | ||
| 44 | |||
| 45 | if __name__ == "__main__": | ||
| 46 | webServer = HTTPServer((host_name, server_port), SnacAutoResponderServer) | ||
| 47 | print("Webhook started http://%s:%s" % (host_name, server_port)) | ||
| 48 | |||
| 49 | try: | ||
| 50 | webServer.serve_forever() | ||
| 51 | except KeyboardInterrupt: | ||
| 52 | pass | ||
| 53 | |||
| 54 | webServer.server_close() | ||
| 55 | print("Webhook stopped.") | ||