feishuBotApi.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. """
  2. @author: luojunhui
  3. """
  4. import json
  5. import requests
  6. from applications.decoratorApi import retryOnTimeout
  7. mention_all = {
  8. "content": "<at id=all></at>\n",
  9. "tag": "lark_md",
  10. }
  11. def create_feishu_columns_sheet(sheet_type, sheet_name, display_name, width="auto",
  12. vertical_align="top", horizontal_align="left", number_format=None):
  13. """
  14. create feishu sheet
  15. :param number_format:
  16. :param horizontal_align:
  17. :param vertical_align:
  18. :param width:
  19. :param display_name:
  20. :param sheet_type:
  21. :param sheet_name:
  22. """
  23. match sheet_type:
  24. case "plain_text":
  25. return {
  26. "name": sheet_name,
  27. "display_name": display_name,
  28. "width": width,
  29. "data_type": "text",
  30. "vertical_align": vertical_align,
  31. "horizontal_align": horizontal_align
  32. }
  33. case "lark_md":
  34. return {
  35. "name": sheet_name,
  36. "display_name": display_name,
  37. "data_type": "lark_md"
  38. }
  39. case "number":
  40. return {
  41. "name": sheet_name,
  42. "display_name": display_name,
  43. "data_type": "number",
  44. "format": number_format,
  45. "width": width
  46. }
  47. case "date":
  48. return {
  49. "name": sheet_name,
  50. "display_name": display_name,
  51. "data_type": "date",
  52. "date_format": "YYYY/MM/DD"
  53. }
  54. def create_feishu_table(title, columns, rows, mention):
  55. """
  56. create feishu table
  57. :param mention:
  58. :param title:
  59. :param columns:
  60. :param rows:
  61. """
  62. table_base = {
  63. "header": {
  64. "template": "blue",
  65. "title": {
  66. "content": title,
  67. "tag": "plain_text"
  68. }
  69. },
  70. "elements": [
  71. {
  72. "tag": "div",
  73. "text": mention_all,
  74. } if mention else {},
  75. {
  76. "tag": "table",
  77. "page_size": len(rows) + 1,
  78. "row_height": "low",
  79. "header_style": {
  80. "text_align": "left",
  81. "text_size": "normal",
  82. "background_style": "grey",
  83. "text_color": "default",
  84. "bold": True,
  85. "lines": 1
  86. },
  87. "columns": columns,
  88. "rows": rows
  89. }
  90. ]
  91. }
  92. return table_base
  93. @retryOnTimeout()
  94. def bot(title, detail, mention=True, table=False):
  95. """
  96. 报警机器人
  97. """
  98. url = "https://open.feishu.cn/open-apis/bot/v2/hook/b44333f2-16c0-4cb1-af01-d135f8704410"
  99. # dev_url = "https://open.feishu.cn/open-apis/bot/v2/hook/f32c0456-847f-41f3-97db-33fcc1616bcd"
  100. headers = {"Content-Type": "application/json"}
  101. if table:
  102. card = create_feishu_table(
  103. title=title,
  104. columns=detail["columns"],
  105. rows=detail["rows"],
  106. mention=mention
  107. )
  108. else:
  109. card = {
  110. "elements": [
  111. {
  112. "tag": "div",
  113. "text": mention_all if mention else {},
  114. },
  115. {
  116. "tag": "div",
  117. "text": {
  118. "content": json.dumps(
  119. detail, ensure_ascii=False, indent=4
  120. ),
  121. "tag": "lark_md",
  122. },
  123. },
  124. ],
  125. "header": {"title": {"content": title, "tag": "plain_text"}},
  126. }
  127. payload = {
  128. "msg_type": "interactive",
  129. "card": card
  130. }
  131. res = requests.request("POST", url=url, headers=headers, data=json.dumps(payload), timeout=10)
  132. return res