base_send_gotify.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. # -*- coding: utf-8 -*-
  2. import httpx
  3. class GotifyNotifier:
  4. def __init__(self, title, message):
  5. self.gotify_url = 'https://gotify.erhe.top'
  6. self.app_token = 'Aw7XKE2Ppk7Dgwk'
  7. self.title = title
  8. self.message = message
  9. def send_message(self):
  10. # 构建POST请求的headers
  11. headers = {
  12. 'Content-Type': 'application/json'
  13. }
  14. # 构建POST请求的body
  15. body = {
  16. 'title': self.title,
  17. 'message': self.message
  18. }
  19. # 发送POST请求
  20. with httpx.Client() as client:
  21. response = client.post(
  22. url=f"{self.gotify_url}/message?token={self.app_token}",
  23. headers=headers,
  24. json=body
  25. )
  26. # 或者可以使用 curl
  27. # curl -k "https://gotify.erhe.top/message?token=A0Xg6ZE5946iBYg" -F "title=测试发送信息" -F "message=假装有信息,测试发送" -F "priority=5"
  28. # 检查响应状态码
  29. if response.status_code == 200:
  30. print('Gotify Message sent successfully!')
  31. else:
  32. print('Failed to send message:', response.text)