async_feishu_api.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. import json
  2. from applications.utils import AsyncHttPClient
  3. class Feishu:
  4. # 服务号分组群发监测机器人
  5. server_account_publish_monitor_bot = "https://open.feishu.cn/open-apis/bot/v2/hook/380fdecf-402e-4426-85b6-7d9dbd2a9f59"
  6. # 外部服务号投流监测机器人
  7. outside_gzh_monitor_bot = "https://open.feishu.cn/open-apis/bot/v2/hook/0899d43d-9f65-48ce-a419-f83ac935bf59"
  8. # 长文 daily 报警机器人
  9. long_articles_bot = "https://open.feishu.cn/open-apis/bot/v2/hook/b44333f2-16c0-4cb1-af01-d135f8704410"
  10. # 测试环境报警机器人
  11. long_articles_bot_dev = "https://open.feishu.cn/open-apis/bot/v2/hook/f32c0456-847f-41f3-97db-33fcc1616bcd"
  12. def __init__(self):
  13. self.token = None
  14. self.headers = {"Content-Type": "application/json"}
  15. self.mention_all = {
  16. "content": "<at id=all></at>\n",
  17. "tag": "lark_md",
  18. }
  19. self.not_mention = {}
  20. async def fetch_token(self):
  21. url = "https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal/"
  22. post_data = {
  23. "app_id": "cli_a51114cf8bf8d00c",
  24. "app_secret": "cNoTAqMpsAm7mPBcpCAXFfvOzCNL27fe",
  25. }
  26. async with AsyncHttPClient(default_headers=self.headers) as client:
  27. response = await client.post(url=url, data=post_data)
  28. tenant_access_token = response["tenant_access_token"]
  29. self.token = tenant_access_token
  30. class FeishuSheetApi(Feishu):
  31. async def prepend_value(self, sheet_token, sheet_id, ranges, values):
  32. insert_value_url = "https://open.feishu.cn/open-apis/sheets/v2/spreadsheets/{}/values_prepend".format(
  33. sheet_token
  34. )
  35. headers = {
  36. "Authorization": "Bearer " + self.token,
  37. "contentType": "application/json; charset=utf-8",
  38. }
  39. body = {
  40. "valueRange": {"range": "{}!{}".format(sheet_id, ranges), "values": values}
  41. }
  42. async with AsyncHttPClient() as client:
  43. response = await client.post(
  44. url=insert_value_url, json=body, headers=headers
  45. )
  46. print(response)
  47. async def insert_value(self, sheet_token, sheet_id, ranges, values):
  48. insert_value_url = (
  49. "https://open.feishu.cn/open-apis/sheets/v2/spreadsheets/{}/values".format(
  50. sheet_token
  51. )
  52. )
  53. headers = {
  54. "Authorization": "Bearer " + self.token,
  55. "contentType": "application/json; charset=utf-8",
  56. }
  57. body = {
  58. "valueRange": {"range": "{}!{}".format(sheet_id, ranges), "values": values}
  59. }
  60. async with AsyncHttPClient() as client:
  61. response = await client.put(
  62. url=insert_value_url, json=body, headers=headers
  63. )
  64. class FeishuBotApi(Feishu):
  65. @classmethod
  66. def create_feishu_columns_sheet(
  67. cls,
  68. sheet_type,
  69. sheet_name,
  70. display_name,
  71. width="auto",
  72. vertical_align="top",
  73. horizontal_align="left",
  74. number_format=None,
  75. ):
  76. match sheet_type:
  77. case "plain_text":
  78. return {
  79. "name": sheet_name,
  80. "display_name": display_name,
  81. "width": width,
  82. "data_type": "text",
  83. "vertical_align": vertical_align,
  84. "horizontal_align": horizontal_align,
  85. }
  86. case "lark_md":
  87. return {
  88. "name": sheet_name,
  89. "display_name": display_name,
  90. "data_type": "lark_md",
  91. }
  92. case "number":
  93. return {
  94. "name": sheet_name,
  95. "display_name": display_name,
  96. "data_type": "number",
  97. "format": number_format,
  98. "width": width,
  99. }
  100. case "date":
  101. return {
  102. "name": sheet_name,
  103. "display_name": display_name,
  104. "data_type": "date",
  105. "date_format": "YYYY/MM/DD",
  106. }
  107. case "options":
  108. return {
  109. "name": sheet_name,
  110. "display_name": display_name,
  111. "data_type": "options",
  112. }
  113. case _:
  114. return {
  115. "name": sheet_name,
  116. "display_name": display_name,
  117. "width": width,
  118. "data_type": "text",
  119. "vertical_align": vertical_align,
  120. "horizontal_align": horizontal_align,
  121. }
  122. # 表格形式
  123. def create_feishu_table(self, title, columns, rows, mention):
  124. table_base = {
  125. "header": {
  126. "template": "blue",
  127. "title": {"content": title, "tag": "plain_text"},
  128. },
  129. "elements": [
  130. self.mention_all if mention else self.not_mention,
  131. {
  132. "tag": "table",
  133. "page_size": len(rows) + 1,
  134. "row_height": "low",
  135. "header_style": {
  136. "text_align": "left",
  137. "text_size": "normal",
  138. "background_style": "grey",
  139. "text_color": "default",
  140. "bold": True,
  141. "lines": 1,
  142. },
  143. "columns": columns,
  144. "rows": rows,
  145. },
  146. ],
  147. }
  148. return table_base
  149. def create_feishu_bot_obj(self, title, mention, detail):
  150. """
  151. create feishu bot object
  152. """
  153. return {
  154. "elements": [
  155. {
  156. "tag": "div",
  157. "text": self.mention_all if mention else self.not_mention,
  158. },
  159. {
  160. "tag": "div",
  161. "text": {
  162. "content": json.dumps(detail, ensure_ascii=False, indent=4),
  163. "tag": "lark_md",
  164. },
  165. },
  166. ],
  167. "header": {"title": {"content": title, "tag": "plain_text"}},
  168. }
  169. # bot
  170. async def bot(self, title, detail, mention=True, table=False, env="prod"):
  171. match env:
  172. case "dev":
  173. url = self.long_articles_bot_dev
  174. case "prod":
  175. url = self.long_articles_bot
  176. case "outside_gzh_monitor":
  177. url = self.outside_gzh_monitor_bot
  178. case "server_account_publish_monitor":
  179. url = self.server_account_publish_monitor_bot
  180. case _:
  181. url = self.long_articles_bot_dev
  182. headers = {"Content-Type": "application/json"}
  183. if table:
  184. card = self.create_feishu_table(
  185. title=title,
  186. columns=detail["columns"],
  187. rows=detail["rows"],
  188. mention=mention,
  189. )
  190. else:
  191. card = self.create_feishu_bot_obj(
  192. title=title, mention=mention, detail=detail
  193. )
  194. payload = {"msg_type": "interactive", "card": card}
  195. async with AsyncHttPClient() as client:
  196. res = await client.post(url=url, headers=headers, data=json.dumps(payload))
  197. return res