Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

wechatcomapp_message.py 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import re
  2. import requests
  3. from wechatpy.enterprise import WeChatClient
  4. from bridge.context import ContextType
  5. from channel.chat_message import ChatMessage
  6. from common.log import logger
  7. from common.tmp_dir import TmpDir
  8. from lib import itchat
  9. from lib.itchat.content import *
  10. class WechatComAppMessage(ChatMessage):
  11. def __init__(self, msg, client: WeChatClient, is_group=False):
  12. super().__init__(msg)
  13. self.msg_id = msg.id
  14. self.create_time = msg.time
  15. self.is_group = is_group
  16. if msg.type == "text":
  17. self.ctype = ContextType.TEXT
  18. self.content = msg.content
  19. elif msg.type == "voice":
  20. self.ctype = ContextType.VOICE
  21. self.content = (
  22. TmpDir().path() + msg.media_id + "." + msg.format
  23. ) # content直接存临时目录路径
  24. def download_voice():
  25. # 如果响应状态码是200,则将响应内容写入本地文件
  26. response = client.media.download(msg.media_id)
  27. if response.status_code == 200:
  28. with open(self.content, "wb") as f:
  29. f.write(response.content)
  30. else:
  31. logger.info(
  32. f"[wechatcom] Failed to download voice file, {response.content}"
  33. )
  34. self._prepare_fn = download_voice
  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"[wechatcom] 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