snac2

Fork of https://codeberg.org/grunfink/snac2
git clone https://git.inz.fi/snac2
Log | Files | Refs | README | LICENSE

auto_follower_webhook.py (1534B)


      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.")