You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1 年之前
1 年之前
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # -*- coding: utf-8 -*-#
  2. from bridge.context import ContextType
  3. from channel.chat_message import ChatMessage
  4. from common.log import logger
  5. from common.tmp_dir import TmpDir
  6. class WeChatMPMessage(ChatMessage):
  7. def __init__(self, msg, client=None):
  8. super().__init__(msg)
  9. self.msg_id = msg.id
  10. self.create_time = msg.time
  11. self.is_group = False
  12. if msg.type == "text":
  13. self.ctype = ContextType.TEXT
  14. self.content = msg.content
  15. elif msg.type == "voice":
  16. if msg.recognition == None:
  17. self.ctype = ContextType.VOICE
  18. self.content = TmpDir().path() + msg.media_id + "." + msg.format # content直接存临时目录路径
  19. def download_voice():
  20. # 如果响应状态码是200,则将响应内容写入本地文件
  21. response = client.media.download(msg.media_id)
  22. if response.status_code == 200:
  23. with open(self.content, "wb") as f:
  24. f.write(response.content)
  25. else:
  26. logger.info(f"[wechatmp] Failed to download voice file, {response.content}")
  27. self._prepare_fn = download_voice
  28. else:
  29. self.ctype = ContextType.TEXT
  30. self.content = msg.recognition
  31. elif msg.type == "image":
  32. self.ctype = ContextType.IMAGE
  33. self.content = TmpDir().path() + msg.media_id + ".png" # content直接存临时目录路径
  34. def download_image():
  35. # 如果响应状态码是200,则将响应内容写入本地文件
  36. response = client.media.download(msg.media_id)
  37. if response.status_code == 200:
  38. with open(self.content, "wb") as f:
  39. f.write(response.content)
  40. else:
  41. logger.info(f"[wechatmp] Failed to download image file, {response.content}")
  42. self._prepare_fn = download_image
  43. else:
  44. raise NotImplementedError("Unsupported message type: Type:{} ".format(msg.type))
  45. self.from_user_id = msg.source
  46. self.to_user_id = msg.target
  47. self.other_user_id = msg.source