1
0

auto_follower_webhook.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #!/usr/bin/env python3
  2. # This is an example of a snac webhook that automatically follows all new followers.
  3. # To use it, configure the user webhook to be http://localhost:12345, and run this program.
  4. # copyright (C) 2025 grunfink et al. / MIT license
  5. from http.server import BaseHTTPRequestHandler, HTTPServer
  6. import time
  7. import json
  8. import os
  9. host_name = "localhost"
  10. server_port = 12345
  11. class SnacAutoResponderServer(BaseHTTPRequestHandler):
  12. def do_POST(self):
  13. self.send_response(200)
  14. self.end_headers()
  15. content_type = self.headers["content-type"]
  16. content_length = int(self.headers["content-length"])
  17. payload = self.rfile.read(content_length).decode("utf-8")
  18. if content_type == "application/json":
  19. try:
  20. noti = json.loads(payload)
  21. type = noti["type"]
  22. if type == "Follow":
  23. actor = noti["actor"]
  24. uid = noti["uid"]
  25. basedir = noti["basedir"]
  26. cmd = "snac follow %s %s %s" % (basedir, uid, actor)
  27. os.system(cmd)
  28. except:
  29. print("Error parsing notification")
  30. if __name__ == "__main__":
  31. webServer = HTTPServer((host_name, server_port), SnacAutoResponderServer)
  32. print("Webhook started http://%s:%s" % (host_name, server_port))
  33. try:
  34. webServer.serve_forever()
  35. except KeyboardInterrupt:
  36. pass
  37. webServer.server_close()
  38. print("Webhook stopped.")