send_feishu_msg.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #!/usr/bin/env python
  2. # -*- encoding: utf-8 -*-
  3. """
  4. Author renyupeng
  5. coding=utf-8
  6. @Time : 2023/2/7 下午
  7. @Site :
  8. @File : send_feishu_msg.py
  9. @Software: PyCharm
  10. @contact: renyupeng@c-top.com.cn
  11. @Tel 1501435553
  12. encoding=utf-8
  13. """
  14. import logging
  15. import json
  16. import requests
  17. import traceback
  18. class SendFeiShuMsg:
  19. @staticmethod
  20. def send_robot_msg(msg_content):
  21. try:
  22. url = 'https://open.feishu.cn/open-apis/bot/v2/hook/6f1e77f2-cdc3-48d6-957e-4c9953da55c0'
  23. req_head = {"Content-Type": "application/json"}
  24. req_data = {
  25. "msg_type": "text",
  26. "content": {
  27. "text": msg_content
  28. }
  29. }
  30. data = json.dumps(req_data)
  31. logging.info("send_robot_msg req_url:{}, req_data:{}".format(url, data))
  32. response = requests.post(url=url, data=data, headers=req_head)
  33. result = response.json()
  34. logging.info("send_robot_msg response:{}".format(response.json()))
  35. if result.get("StatusCode") == 0:
  36. logging.info("飞书通知发送成功")
  37. return True
  38. else:
  39. logging.error("飞书通知发送失败,请检查!")
  40. return False
  41. except Exception as e:
  42. logging.error("send_robot_msg Error:{}".format(traceback.format_exc()))
  43. @staticmethod
  44. def send_cookie_robot_msg(msg_content):
  45. try:
  46. url = 'https://open.feishu.cn/open-apis/bot/v2/hook/ce075a9a-e224-4435-8028-55ae509b4b32'
  47. req_head = {"Content-Type": "application/json"}
  48. req_data = {
  49. "msg_type": "text",
  50. "content": {
  51. "text": msg_content
  52. }
  53. }
  54. data = json.dumps(req_data)
  55. print(data)
  56. logging.info("send_robot_msg req_url:{}, req_data:{}".format(url, data))
  57. response = requests.post(url=url, data=data, headers=req_head)
  58. print(response.text)
  59. result = response.json()
  60. print(result)
  61. logging.info("send_robot_msg response:{}".format(response.json()))
  62. if result.get("StatusCode") == 0:
  63. logging.info("飞书通知发送成功")
  64. return True
  65. else:
  66. logging.error("飞书通知发送失败,请检查!")
  67. return False
  68. except Exception as e:
  69. logging.error("send_robot_msg Error:{}".format(traceback.format_exc()))
  70. if __name__ == '__main__':
  71. SendFeiShuMsg.send_robot_msg('11111')