utils_send_serverchan.py 884 B

12345678910111213141516171819202122232425262728293031
  1. # -*- coding: utf-8 -*-
  2. import httpx
  3. class ServerChanNotifier:
  4. def __init__(self, title, message):
  5. self.serverchan_url = 'https://sctapi.ftqq.com/SCT158272TRDcSniEdGR0TKhqhUl66LPHb.send'
  6. self.title = title
  7. self.message = message
  8. def send_message(self):
  9. # 构建POST请求的body
  10. body = {
  11. 'text': self.title,
  12. 'desp': self.message
  13. }
  14. # 发送POST请求
  15. with httpx.Client() as client:
  16. response = client.post(
  17. url=self.serverchan_url,
  18. headers={'Content-Type': 'application/x-www-form-urlencoded'},
  19. data=body
  20. )
  21. # 检查响应状态码
  22. if response.status_code == 200:
  23. print('ServerChan Message sent successfully!')
  24. else:
  25. print('Failed to send message:', response.text)