XMPP bot written in Python https://gitlab.com/XRevan86/hptoad
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.5KB

  1. #!/usr/bin/env python3
  2. import sys
  3. import slixmpp
  4. opts = {
  5. "jid": "botname@example.com",
  6. "resource": "resource",
  7. "password": "password",
  8. "connect": "xmpp.example.org:5222",
  9. }
  10. def on_failed_all_auth(event):
  11. print("Auth: Could not connect to the server, or password mismatch!")
  12. sys.exit(1)
  13. def on_session_start(event):
  14. # client.send_presence()
  15. client.get_roster()
  16. body = "\n".join(sys.argv[1:]).strip()
  17. try:
  18. if body:
  19. client.send_message(mto=opts["jid"], mbody=body, mtype="chat")
  20. except Exception as e:
  21. print("%s: %s" % (type(e).__name__, str(e)))
  22. finally:
  23. client.disconnect()
  24. def on_session_end(event):
  25. sys.exit(0)
  26. if __name__ == "__main__":
  27. if len(sys.argv) <= 1:
  28. print("At least one argument is required.")
  29. sys.exit(1)
  30. client = slixmpp.ClientXMPP("%s/%s" % (opts["jid"], opts["resource"]),
  31. opts["password"])
  32. if opts["connect"]:
  33. connect = opts["connect"].split(":", 1)
  34. if len(connect) != 2 or not connect[1].isdigit():
  35. print("Connection server format is invalid, should be " +
  36. "example.org:5222")
  37. sys.exit(1)
  38. else:
  39. connect = ()
  40. client.connect(connect)
  41. client.add_event_handler("failed_all_auth", on_failed_all_auth)
  42. client.add_event_handler("session_start", on_session_start)
  43. client.add_event_handler("session_end", on_session_end)
  44. client.process(forever=True)