feishu_webhook.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import requests
  2. import json
  3. import time
  4. from commons.config import FEISHU_WEBHOOK_URL
  5. WEBHOOK_URL = FEISHU_WEBHOOK_URL
  6. def send_text(text):
  7. data = {
  8. "msg_type": "text",
  9. "content": {"text": text},
  10. }
  11. headers = {"Content-Type": "application/json"}
  12. try:
  13. response = requests.post(
  14. WEBHOOK_URL,
  15. headers=headers,
  16. data=json.dumps(data),
  17. timeout=5
  18. )
  19. except Exception as e:
  20. pass
  21. def send_error_card(task_name, err_msg, mention_all=False):
  22. """发送异常红色卡片"""
  23. content = (
  24. f"**任务**:{task_name}\n"
  25. f"**状态**:❌ 失败\n"
  26. f"**错误**:{err_msg}\n"
  27. f"**时间**:{time.strftime('%Y-%m-%d %H:%M:%S')}"
  28. )
  29. if mention_all:
  30. content = f"<at id=all></at>\n{content}"
  31. data = {
  32. "msg_type": "interactive",
  33. "card": {
  34. "config": {"wide_screen_mode": True},
  35. "header": {
  36. "template": "red",
  37. "title": {"tag": "plain_text", "content": "异常告警"},
  38. },
  39. "elements": [
  40. {
  41. "tag": "div",
  42. "text": {"tag": "lark_md", "content": content},
  43. }
  44. ],
  45. },
  46. }
  47. headers = {"Content-Type": "application/json"}
  48. response = requests.post(
  49. WEBHOOK_URL,
  50. headers=headers,
  51. data=json.dumps(data),
  52. timeout=5,
  53. )
  54. print(response.json())
  55. # 使用示例
  56. if __name__ == "__main__":
  57. account1 = "aaaaa"
  58. plat_form = "京东"
  59. drug = "999小儿感冒颗粒24粒"
  60. drug_count = 128
  61. text = f"**重要通知** {str(time.strftime("%Y-%m-%d %H:%M:%S"))}\n 账号{account1}, {plat_form} 采集 {drug} 数据 {drug_count} 条"
  62. send_text(text)