async_feishu_api.py 7.9 KB

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