Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

53 lignes
2.1KB

  1. from wechatpy.enterprise import WeChatClient
  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 WechatComAppMessage(ChatMessage):
  7. def __init__(self, msg, client: WeChatClient, is_group=False):
  8. super().__init__(msg)
  9. self.msg_id = msg.id
  10. self.create_time = msg.time
  11. self.is_group = is_group
  12. if msg.type == "text":
  13. self.ctype = ContextType.TEXT
  14. self.content = msg.content
  15. elif msg.type == "voice":
  16. self.ctype = ContextType.VOICE
  17. self.content = TmpDir().path() + msg.media_id + "." + msg.format # content直接存临时目录路径
  18. def download_voice():
  19. # 如果响应状态码是200,则将响应内容写入本地文件
  20. response = client.media.download(msg.media_id)
  21. if response.status_code == 200:
  22. with open(self.content, "wb") as f:
  23. f.write(response.content)
  24. else:
  25. logger.info(f"[wechatcom] Failed to download voice file, {response.content}")
  26. self._prepare_fn = download_voice
  27. elif msg.type == "image":
  28. self.ctype = ContextType.IMAGE
  29. self.content = TmpDir().path() + msg.media_id + ".png" # content直接存临时目录路径
  30. def download_image():
  31. # 如果响应状态码是200,则将响应内容写入本地文件
  32. response = client.media.download(msg.media_id)
  33. if response.status_code == 200:
  34. with open(self.content, "wb") as f:
  35. f.write(response.content)
  36. else:
  37. logger.info(f"[wechatcom] Failed to download image file, {response.content}")
  38. self._prepare_fn = download_image
  39. else:
  40. raise NotImplementedError("Unsupported message type: Type:{} ".format(msg.type))
  41. self.from_user_id = msg.source
  42. self.to_user_id = msg.target
  43. self.other_user_id = msg.source