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.

feishu_message.py 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. else:
  45. raise NotImplementedError("Unsupported message type: Type:{} ".format(msg_type))
  46. self.from_user_id = sender.get("sender_id").get("open_id")
  47. self.to_user_id = event.get("app_id")
  48. if is_group:
  49. # 群聊
  50. self.other_user_id = msg.get("chat_id")
  51. self.actual_user_id = self.from_user_id
  52. self.content = self.content.replace("@_user_1", "").strip()
  53. self.actual_user_nickname = ""
  54. else:
  55. # 私聊
  56. self.other_user_id = self.from_user_id
  57. self.actual_user_id = self.from_user_id