diff options
| author | 2025-07-16 20:21:20 +0200 | |
|---|---|---|
| committer | 2025-07-16 20:21:20 +0200 | |
| commit | 3554a73aa50526631961efcca78c6c8eb2a16911 (patch) | |
| tree | fc4c21a65337ab8ecb4fb00aa551ee21997faf6f /examples/auto_follower_webhook.py | |
| parent | po/de_DE.po aktualisiert (diff) | |
| parent | Updated RELEASE_NOTES. (diff) | |
| download | penes-snac2-3554a73aa50526631961efcca78c6c8eb2a16911.tar.gz penes-snac2-3554a73aa50526631961efcca78c6c8eb2a16911.tar.xz penes-snac2-3554a73aa50526631961efcca78c6c8eb2a16911.zip | |
Merge pull request 'master' (#9) from grunfink/snac2:master into master
Reviewed-on: https://codeberg.org/zen/snac2/pulls/9
Diffstat (limited to 'examples/auto_follower_webhook.py')
| -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..eb632a3 --- /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, configure the user webhook to be http://localhost:12345, and run this program. | ||
| 6 | |||
| 7 | # copyright (C) 2025 grunfink et al. / MIT license | ||
| 8 | |||
| 9 | from http.server import BaseHTTPRequestHandler, HTTPServer | ||
| 10 | import time | ||
| 11 | import json | ||
| 12 | import os | ||
| 13 | |||
| 14 | host_name = "localhost" | ||
| 15 | server_port = 12345 | ||
| 16 | |||
| 17 | class SnacAutoResponderServer(BaseHTTPRequestHandler): | ||
| 18 | |||
| 19 | def do_POST(self): | ||
| 20 | self.send_response(200) | ||
| 21 | self.end_headers() | ||
| 22 | |||
| 23 | content_type = self.headers["content-type"] | ||
| 24 | content_length = int(self.headers["content-length"]) | ||
| 25 | payload = self.rfile.read(content_length).decode("utf-8") | ||
| 26 | |||
| 27 | if content_type == "application/json": | ||
| 28 | try: | ||
| 29 | noti = json.loads(payload) | ||
| 30 | |||
| 31 | type = noti["type"] | ||
| 32 | |||
| 33 | if type == "Follow": | ||
| 34 | actor = noti["actor"] | ||
| 35 | uid = noti["uid"] | ||
| 36 | basedir = noti["basedir"] | ||
| 37 | |||
| 38 | cmd = "snac follow %s %s %s" % (basedir, uid, 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.") | ||