request_api.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import json
  2. import requests
  3. import os
  4. from functools import wraps
  5. from dotenv import load_dotenv
  6. from requests_toolbelt import MultipartEncoder
  7. from utils.logger import logger
  8. from model.global_model import GlobalModel
  9. load_dotenv() # 加载环境变量
  10. APP_ID = os.getenv('APP_ID')
  11. APP_SECRET = os.getenv('APP_SECRET')
  12. def get_tenant_token():
  13. url = 'https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal'
  14. data = {
  15. 'app_id': APP_ID,
  16. 'app_secret': APP_SECRET
  17. }
  18. r = requests.post(url, json=data)
  19. GlobalModel().tenant_token = r.json().get('tenant_access_token')
  20. def keep_tenant_access_token(f):
  21. @wraps(f)
  22. def decorated(*args, **kwargs):
  23. if GlobalModel().tenant_token == "":
  24. # tenant_token未初始化
  25. get_tenant_token()
  26. r = f(*args, **kwargs)
  27. else:
  28. # tenant_token已初始化
  29. try:
  30. r = f(*args, **kwargs)
  31. if r:
  32. if r.json().get('code') == 99991661 or r.json().get('code') == 99991663:
  33. # tenant_token已过期
  34. get_tenant_token()
  35. r = f(*args, **kwargs)
  36. else:
  37. # 正常返回
  38. return r
  39. else:
  40. get_tenant_token()
  41. r = f(*args, **kwargs)
  42. except:
  43. get_tenant_token()
  44. r = f(*args, **kwargs)
  45. return r
  46. return r
  47. return decorated
  48. @keep_tenant_access_token
  49. def reply_message(message_id, response_json):
  50. headers = {
  51. 'Authorization': 'Bearer ' + GlobalModel().tenant_token,
  52. 'Content-Type': 'application/json; charset=utf-8'
  53. }
  54. url = f"https://open.feishu.cn/open-apis/im/v1/messages/{message_id}/reply"
  55. res = requests.post(url, json=response_json, headers=headers)
  56. if res.status_code == 200 and 'code' in res.json() and res.json()['code'] == 0:
  57. logger.info(message_id + '回复成功')
  58. return res
  59. else:
  60. logger.error(message_id + '回复失败\n' + str(res.json()))
  61. return None
  62. @keep_tenant_access_token
  63. def get_img(message_id, img_key):
  64. headers = {
  65. 'Authorization': 'Bearer ' + GlobalModel().tenant_token
  66. }
  67. url = f"https://open.feishu.cn/open-apis/im/v1/messages/{message_id}/resources/{img_key}?type=image"
  68. response = requests.get(url, headers=headers)
  69. return response
  70. @keep_tenant_access_token
  71. def update_message(message_id, update_json):
  72. headers = {
  73. 'Authorization': 'Bearer ' + GlobalModel().tenant_token,
  74. 'Content-Type': 'application/json; charset=utf-8'
  75. }
  76. url = f"https://open.feishu.cn/open-apis/im/v1/messages/{message_id}"
  77. res = requests.patch(url, json=update_json, headers=headers)
  78. if res.status_code == 200 and 'code' in res.json() and res.json()['code'] == 0:
  79. logger.info(message_id + '更新成功')
  80. return res
  81. else:
  82. logger.error(message_id + '更新失败\n' + str(res.json()))
  83. return None
  84. @keep_tenant_access_token
  85. def upload_image(img_key, image_data, note):
  86. url = "https://open.feishu.cn/open-apis/im/v1/images"
  87. data = {
  88. "image_type": "message",
  89. 'image': image_data
  90. }
  91. multi_form = MultipartEncoder(data)
  92. headers = {
  93. 'Authorization': 'Bearer ' + GlobalModel().tenant_token,
  94. 'Content-Type': multi_form.content_type
  95. }
  96. res = requests.post(url, data=multi_form, headers=headers)
  97. if res.status_code == 200 and 'code' in res.json() and res.json()['code'] == 0:
  98. logger.info(img_key + "-" + note + '上传成功')
  99. return res
  100. else:
  101. logger.error(img_key + "-" + note + '上传失败\n' + str(res.json()))
  102. return None