feishu.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. """
  2. feishu python方法
  3. """
  4. import json
  5. import requests
  6. import datetime
  7. def get_app_token():
  8. """
  9. 获取飞书api token
  10. :return:
  11. """
  12. url = "https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal/"
  13. post_data = {
  14. "app_id": "cli_a51114cf8bf8d00c", # 这里账号密码是发布应用的后台账号及密码
  15. "app_secret": "cNoTAqMpsAm7mPBcpCAXFfvOzCNL27fe",
  16. }
  17. response = requests.request("POST", url=url, data=post_data)
  18. tenant_access_token = response.json()["tenant_access_token"]
  19. return tenant_access_token
  20. class Feishu(object):
  21. """
  22. feishu Python Object
  23. """
  24. def __init__(self):
  25. self.document_url = "https://w42nne6hzg.feishu.cn/sheets/C1Qrsa4HWh6bzEtv7aocrFlAnad"
  26. self.robot_url = "https://open.feishu.cn/open-apis/bot/v2/hook/34e2fdbc-0649-44d3-b5ce-b28b38cca1db"
  27. self.headers = {"Content-Type": "application/json"}
  28. self.document_token = "C1Qrsa4HWh6bzEtv7aocrFlAnad"
  29. def insert_columns_rows(self, sheet_id, major_dimension, start_index, end_index):
  30. """
  31. 写入飞书表格
  32. :param sheet_id: 表的id
  33. :param major_dimension:行或者列, ROWS, COLUMNS
  34. :param start_index:开始位置
  35. :param end_index:结束位置
  36. """
  37. insert_columns_url = "https://open.feishu.cn/open-apis/sheets/v2/spreadsheets/" \
  38. + self.document_token + "/insert_dimension_range"
  39. headers = {
  40. "Authorization": "Bearer " + get_app_token(),
  41. "Content-Type": "application/json; charset=utf-8",
  42. }
  43. body = {
  44. "dimension": {
  45. "sheetId": sheet_id,
  46. "majorDimension": major_dimension, # 默认 ROWS ,可选 ROWS、COLUMNS
  47. "startIndex": start_index, # 开始的位置
  48. "endIndex": end_index # 结束的位置
  49. },
  50. "inheritStyle": "AFTER" # BEFORE 或 AFTER,不填为不继承 style
  51. }
  52. response = requests.post(url=insert_columns_url, headers=headers, json=body, verify=False)
  53. print(response.json()['msg'])
  54. def insert_value(self, sheet_id, ranges, values):
  55. """
  56. update
  57. :param sheet_id:
  58. :param ranges:
  59. :param values:
  60. """
  61. update_values_url = "https://open.feishu.cn/open-apis/sheets/v2/spreadsheets/" \
  62. + self.document_token + "/values_batch_update"
  63. headers = {
  64. "Authorization": "Bearer " + get_app_token(),
  65. "Content-Type": "application/json; charset=utf-8"
  66. }
  67. body = {
  68. "valueRanges": [
  69. {
  70. "range": sheet_id + "!" + ranges,
  71. "values": values
  72. },
  73. ],
  74. }
  75. response = requests.request("POST", url=update_values_url, headers=headers, json=body)
  76. print(response.json())
  77. def bot(self, platform_name, flag=1):
  78. """
  79. 飞书机器人,在群里报警用
  80. :param platform_name: 小程序的名称
  81. :param flag: 通知 or 报警, 默认为1, 通知
  82. :return:
  83. """
  84. if flag == 1:
  85. payload = {
  86. "msg_type": "interactive",
  87. "card": {
  88. "elements": [
  89. {
  90. "tag": "div",
  91. "text": {
  92. "content": "**{}**\n数据自动导出完成;\n完成时间是:{}。".format(
  93. platform_name, datetime.datetime.now().__str__()
  94. ),
  95. "tag": "lark_md",
  96. },
  97. },
  98. ],
  99. "header": {"title": {"content": "We分析: 通知 ✅", "tag": "plain_text"}},
  100. },
  101. }
  102. else:
  103. payload = {
  104. "msg_type": "interactive",
  105. "card": {
  106. "elements": [
  107. {
  108. "tag": "div",
  109. "text": {
  110. "content": "**{}**\n自动导出失败 !!!".format(platform_name),
  111. "tag": "lark_md",
  112. },
  113. },
  114. ],
  115. "header": {
  116. "title": {"content": "We分析: 报警 ❌", "tag": "plain_text"}
  117. },
  118. },
  119. }
  120. w = requests.request(
  121. "POST", url=self.robot_url, headers=self.headers, data=json.dumps(payload)
  122. )
  123. print(w.json())
  124. def remind(self):
  125. """
  126. 飞书提醒机器人
  127. :return: None
  128. """
  129. payload = {
  130. "msg_type": "interactive",
  131. "card": {
  132. "elements": [
  133. {
  134. "tag": "div",
  135. "text": {
  136. "content": "扫码通知:记得要扫码导出we分析, ",
  137. "tag": "lark_md",
  138. },
  139. },
  140. ],
  141. "header": {"title": {"content": "We分析: 提醒 ⏰", "tag": "plain_text"}},
  142. },
  143. }
  144. w = requests.request(
  145. "POST", url=self.robot_url, headers=self.headers, data=json.dumps(payload)
  146. )
  147. print(w.json())
  148. if __name__ == "__main__":
  149. F = Feishu()
  150. value = [[0]]
  151. F.insert_value(sheet_id="gwzBOM", values=value, ranges="D1:F1")