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.

66 lines
2.4KB

  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 = (
  19. TmpDir().path() + msg.media_id + "." + msg.format
  20. ) # content直接存临时目录路径
  21. def download_voice():
  22. # 如果响应状态码是200,则将响应内容写入本地文件
  23. response = client.media.download(msg.media_id)
  24. if response.status_code == 200:
  25. with open(self.content, "wb") as f:
  26. f.write(response.content)
  27. else:
  28. logger.info(
  29. f"[wechatmp] Failed to download voice file, {response.content}"
  30. )
  31. self._prepare_fn = download_voice
  32. else:
  33. self.ctype = ContextType.TEXT
  34. self.content = msg.recognition
  35. elif msg.type == "image":
  36. self.ctype = ContextType.IMAGE
  37. self.content = TmpDir().path() + msg.media_id + ".png" # content直接存临时目录路径
  38. def download_image():
  39. # 如果响应状态码是200,则将响应内容写入本地文件
  40. response = client.media.download(msg.media_id)
  41. if response.status_code == 200:
  42. with open(self.content, "wb") as f:
  43. f.write(response.content)
  44. else:
  45. logger.info(
  46. f"[wechatmp] Failed to download image file, {response.content}"
  47. )
  48. self._prepare_fn = download_image
  49. else:
  50. raise NotImplementedError(
  51. "Unsupported message type: Type:{} ".format(msg.type)
  52. )
  53. self.from_user_id = msg.source
  54. self.to_user_id = msg.target
  55. self.other_user_id = msg.source