utils_send_gotify.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # -*- coding: utf-8 -*-
  2. import httpx
  3. class GotifyNotifier:
  4. def __init__(self, title, message, token_name=''):
  5. self.gotify_url = 'https://gotify.erhe.top'
  6. self.app_token = self.match_token_name(token_name)
  7. self.title = title
  8. self.message = message
  9. def match_token_name(self, name):
  10. token_name_dict = {
  11. 'base': 'A8EVb0Cmxnb2vfk',
  12. 'coin': 'AgfOJESqDKftBTQ',
  13. 'dlt': 'A3bqt9Dlbs.fPUb',
  14. 'AirdropTasksNews': 'Aoe0VKt-kkZnm8d',
  15. 'weather': 'A9KF--mx_12PjSu',
  16. 'news': 'AT2QGp_vyCX4akW',
  17. 'CheckAndRemind': 'Aw7XKE2Ppk7Dgwk',
  18. 'test': 'A0Xg6ZE5946iBYg',
  19. }
  20. token = token_name_dict.get(name)
  21. if token:
  22. return token
  23. else:
  24. return token_name_dict['base']
  25. def send_message(self):
  26. # 构建POST请求的headers
  27. headers = {
  28. 'Content-Type': 'application/json'
  29. }
  30. # 构建POST请求的body
  31. body = {
  32. 'title': self.title,
  33. 'message': self.message
  34. }
  35. # 发送POST请求
  36. with httpx.Client() as client:
  37. response = client.post(
  38. url=f"{self.gotify_url}/message?token={self.app_token}",
  39. headers=headers,
  40. json=body
  41. )
  42. # 或者可以使用 curl
  43. # curl -k "https://gotify.erhe.top/message?token=A0Xg6ZE5946iBYg" -F "title=测试发送信息" -F "message=假装有信息,测试发送" -F "priority=5"
  44. # 检查响应状态码
  45. if response.status_code == 200:
  46. print('Gotify Message sent successfully!')
  47. else:
  48. print('Failed to send message:', response.text)