Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

93 Zeilen
3.9KB

  1. from bridge.context import ContextType
  2. from channel.chat_message import ChatMessage
  3. import json
  4. import requests
  5. from common.log import logger
  6. from common.tmp_dir import TmpDir
  7. from common import utils
  8. class FeishuMessage(ChatMessage):
  9. def __init__(self, event: dict, is_group=False, access_token=None):
  10. super().__init__(event)
  11. msg = event.get("message")
  12. sender = event.get("sender")
  13. self.access_token = access_token
  14. self.msg_id = msg.get("message_id")
  15. self.create_time = msg.get("create_time")
  16. self.is_group = is_group
  17. msg_type = msg.get("message_type")
  18. if msg_type == "text":
  19. self.ctype = ContextType.TEXT
  20. content = json.loads(msg.get('content'))
  21. self.content = content.get("text").strip()
  22. elif msg_type == "file":
  23. self.ctype = ContextType.FILE
  24. content = json.loads(msg.get("content"))
  25. file_key = content.get("file_key")
  26. file_name = content.get("file_name")
  27. self.content = TmpDir().path() + file_key + "." + utils.get_path_suffix(file_name)
  28. def _download_file():
  29. # 如果响应状态码是200,则将响应内容写入本地文件
  30. url = f"https://open.feishu.cn/open-apis/im/v1/messages/{self.msg_id}/resources/{file_key}"
  31. headers = {
  32. "Authorization": "Bearer " + access_token,
  33. }
  34. params = {
  35. "type": "file"
  36. }
  37. response = requests.get(url=url, headers=headers, params=params)
  38. if response.status_code == 200:
  39. with open(self.content, "wb") as f:
  40. f.write(response.content)
  41. else:
  42. logger.info(f"[FeiShu] Failed to download file, key={file_key}, res={response.text}")
  43. self._prepare_fn = _download_file
  44. # elif msg.type == "voice":
  45. # self.ctype = ContextType.VOICE
  46. # self.content = TmpDir().path() + msg.media_id + "." + msg.format # content直接存临时目录路径
  47. #
  48. # def download_voice():
  49. # # 如果响应状态码是200,则将响应内容写入本地文件
  50. # response = client.media.download(msg.media_id)
  51. # if response.status_code == 200:
  52. # with open(self.content, "wb") as f:
  53. # f.write(response.content)
  54. # else:
  55. # logger.info(f"[wechatcom] Failed to download voice file, {response.content}")
  56. #
  57. # self._prepare_fn = download_voice
  58. # elif msg.type == "image":
  59. # self.ctype = ContextType.IMAGE
  60. # self.content = TmpDir().path() + msg.media_id + ".png" # content直接存临时目录路径
  61. #
  62. # def download_image():
  63. # # 如果响应状态码是200,则将响应内容写入本地文件
  64. # response = client.media.download(msg.media_id)
  65. # if response.status_code == 200:
  66. # with open(self.content, "wb") as f:
  67. # f.write(response.content)
  68. # else:
  69. # logger.info(f"[wechatcom] Failed to download image file, {response.content}")
  70. #
  71. # self._prepare_fn = download_image
  72. else:
  73. raise NotImplementedError("Unsupported message type: Type:{} ".format(msg_type))
  74. self.from_user_id = sender.get("sender_id").get("open_id")
  75. self.to_user_id = event.get("app_id")
  76. if is_group:
  77. # 群聊
  78. self.other_user_id = msg.get("chat_id")
  79. self.actual_user_id = self.from_user_id
  80. self.content = self.content.replace("@_user_1", "").strip()
  81. self.actual_user_nickname = ""
  82. else:
  83. # 私聊
  84. self.other_user_id = self.from_user_id
  85. self.actual_user_id = self.from_user_id