Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

58 lines
2.5KB

  1. from bridge.context import ContextType
  2. from channel.chat_message import ChatMessage
  3. from common.tmp_dir import TmpDir
  4. from common.log import logger
  5. from lib.itchat.content import *
  6. from lib import itchat
  7. class WeChatMessage(ChatMessage):
  8. def __init__(self, itchat_msg, is_group=False):
  9. super().__init__( itchat_msg)
  10. self.msg_id = itchat_msg['MsgId']
  11. self.create_time = itchat_msg['CreateTime']
  12. self.is_group = is_group
  13. if itchat_msg['Type'] == TEXT:
  14. self.ctype = ContextType.TEXT
  15. self.content = itchat_msg['Text']
  16. elif itchat_msg['Type'] == VOICE:
  17. self.ctype = ContextType.VOICE
  18. self.content = TmpDir().path() + itchat_msg['FileName'] # content直接存临时目录路径
  19. self._prepare_fn = lambda: itchat_msg.download(self.content)
  20. else:
  21. raise NotImplementedError("Unsupported message type: {}".format(itchat_msg['Type']))
  22. self.from_user_id = itchat_msg['FromUserName']
  23. self.to_user_id = itchat_msg['ToUserName']
  24. user_id = itchat.instance.storageClass.userName
  25. nickname = itchat.instance.storageClass.nickName
  26. # 虽然from_user_id和to_user_id用的少,但是为了保持一致性,还是要填充一下
  27. # 以下很繁琐,一句话总结:能填的都填了。
  28. if self.from_user_id == user_id:
  29. self.from_user_nickname = nickname
  30. if self.to_user_id == user_id:
  31. self.to_user_nickname = nickname
  32. try: # 陌生人时候, 'User'字段可能不存在
  33. self.other_user_id = itchat_msg['User']['UserName']
  34. self.other_user_nickname = itchat_msg['User']['NickName']
  35. if self.other_user_id == self.from_user_id:
  36. self.from_user_nickname = self.other_user_nickname
  37. if self.other_user_id == self.to_user_id:
  38. self.to_user_nickname = self.other_user_nickname
  39. except KeyError as e: # 处理偶尔没有对方信息的情况
  40. logger.warn("[WX]get other_user_id failed: " + str(e))
  41. if self.from_user_id == user_id:
  42. self.other_user_id = self.to_user_id
  43. else:
  44. self.other_user_id = self.from_user_id
  45. if self.is_group:
  46. self.is_at = itchat_msg['IsAt']
  47. self.actual_user_id = itchat_msg['ActualUserName']
  48. self.actual_user_nickname = itchat_msg['ActualNickName']