feishuBotApi.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. """
  2. @author: luojunhui
  3. """
  4. import json
  5. import requests
  6. from applications.decoratorApi import retryOnTimeout
  7. def create_feishu_table(title, columns, rows):
  8. """
  9. create feishu table
  10. :param title:
  11. :param columns:
  12. :param rows:
  13. """
  14. table_base = {
  15. "header": {
  16. "template": "blue",
  17. "title": {
  18. "content": title,
  19. "tag": "plain_text"
  20. }
  21. },
  22. "elements": [
  23. {
  24. "tag": "table",
  25. "page_size": len(rows) + 1,
  26. "row_height": "low",
  27. "header_style": {
  28. "text_align": "left",
  29. "text_size": "normal",
  30. "background_style": "none",
  31. "text_color": "grey",
  32. "bold": True,
  33. "lines": 1
  34. },
  35. "columns": columns,
  36. "rows": rows
  37. }
  38. ]
  39. }
  40. return table_base
  41. @retryOnTimeout()
  42. def bot(title, detail, mention=True):
  43. """
  44. 机器人
  45. """
  46. title_obj = {
  47. "content": "{}<at id=all></at>\n".format(title) if mention else "{}\n".format(title),
  48. "tag": "lark_md",
  49. }
  50. head_title = "【重点关注】" if mention else "【普通通知】"
  51. url = "https://open.feishu.cn/open-apis/bot/v2/hook/b44333f2-16c0-4cb1-af01-d135f8704410"
  52. headers = {"Content-Type": "application/json"}
  53. payload = {
  54. "msg_type": "interactive",
  55. "card": {
  56. "elements": [
  57. {
  58. "tag": "div",
  59. "text": title_obj,
  60. },
  61. {
  62. "tag": "div",
  63. "text": {
  64. "content": json.dumps(
  65. detail, ensure_ascii=False, indent=4
  66. ),
  67. "tag": "lark_md",
  68. },
  69. },
  70. ],
  71. "header": {"title": {"content": head_title, "tag": "plain_text"}},
  72. },
  73. }
  74. requests.request("POST", url=url, headers=headers, data=json.dumps(payload), timeout=10)