async_feishu_api.py 7.6 KB

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