您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

wechat_message.py 5.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import re
  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. from lib import itchat
  7. from lib.itchat.content import *
  8. class WechatMessage(ChatMessage):
  9. def __init__(self, itchat_msg, is_group=False):
  10. super().__init__(itchat_msg)
  11. self.msg_id = itchat_msg["MsgId"]
  12. self.create_time = itchat_msg["CreateTime"]
  13. self.is_group = is_group
  14. if itchat_msg["Type"] == TEXT:
  15. self.ctype = ContextType.TEXT
  16. self.content = itchat_msg["Text"]
  17. elif itchat_msg["Type"] == VOICE:
  18. self.ctype = ContextType.VOICE
  19. self.content = TmpDir().path() + itchat_msg["FileName"] # content直接存临时目录路径
  20. self._prepare_fn = lambda: itchat_msg.download(self.content)
  21. elif itchat_msg["Type"] == PICTURE and itchat_msg["MsgType"] == 3:
  22. self.ctype = ContextType.IMAGE
  23. self.content = TmpDir().path() + itchat_msg["FileName"] # content直接存临时目录路径
  24. self._prepare_fn = lambda: itchat_msg.download(self.content)
  25. elif itchat_msg["Type"] == NOTE and itchat_msg["MsgType"] == 10000:
  26. if is_group and ("加入群聊" in itchat_msg["Content"] or "加入了群聊" in itchat_msg["Content"]):
  27. self.ctype = ContextType.JOIN_GROUP
  28. self.content = itchat_msg["Content"]
  29. # 这里只能得到nickname, actual_user_id还是机器人的id
  30. if "加入了群聊" in itchat_msg["Content"]:
  31. self.actual_user_nickname = re.findall(r"\"(.*?)\"", itchat_msg["Content"])[-1]
  32. elif "加入群聊" in itchat_msg["Content"]:
  33. self.actual_user_nickname = re.findall(r"\"(.*?)\"", itchat_msg["Content"])[0]
  34. elif "你已添加了" in itchat_msg["Content"]: #通过好友请求
  35. self.ctype = ContextType.ACCEPT_FRIEND
  36. self.content = itchat_msg["Content"]
  37. elif "拍了拍我" in itchat_msg["Content"]:
  38. self.ctype = ContextType.PATPAT
  39. self.content = itchat_msg["Content"]
  40. if is_group:
  41. self.actual_user_nickname = re.findall(r"\"(.*?)\"", itchat_msg["Content"])[0]
  42. else:
  43. raise NotImplementedError("Unsupported note message: " + itchat_msg["Content"])
  44. elif itchat_msg["Type"] == ATTACHMENT:
  45. self.ctype = ContextType.FILE
  46. self.content = TmpDir().path() + itchat_msg["FileName"] # content直接存临时目录路径
  47. self._prepare_fn = lambda: itchat_msg.download(self.content)
  48. elif itchat_msg["Type"] == SHARING:
  49. self.ctype = ContextType.SHARING
  50. self.content = itchat_msg.get("Url")
  51. else:
  52. raise NotImplementedError("Unsupported message type: Type:{} MsgType:{}".format(itchat_msg["Type"], itchat_msg["MsgType"]))
  53. self.from_user_id = itchat_msg["FromUserName"]
  54. self.to_user_id = itchat_msg["ToUserName"]
  55. user_id = itchat.instance.storageClass.userName
  56. nickname = itchat.instance.storageClass.nickName
  57. # 虽然from_user_id和to_user_id用的少,但是为了保持一致性,还是要填充一下
  58. # 以下很繁琐,一句话总结:能填的都填了。
  59. if self.from_user_id == user_id:
  60. self.from_user_nickname = nickname
  61. if self.to_user_id == user_id:
  62. self.to_user_nickname = nickname
  63. try: # 陌生人时候, User字段可能不存在
  64. # my_msg 为True是表示是自己发送的消息
  65. self.my_msg = itchat_msg["ToUserName"] == itchat_msg["User"]["UserName"] and \
  66. itchat_msg["ToUserName"] != itchat_msg["FromUserName"]
  67. self.other_user_id = itchat_msg["User"]["UserName"]
  68. self.other_user_nickname = itchat_msg["User"]["NickName"]
  69. if self.other_user_id == self.from_user_id:
  70. self.from_user_nickname = self.other_user_nickname
  71. if self.other_user_id == self.to_user_id:
  72. self.to_user_nickname = self.other_user_nickname
  73. if itchat_msg["User"].get("Self"):
  74. # 自身的展示名,当设置了群昵称时,该字段表示群昵称
  75. self.self_display_name = itchat_msg["User"].get("Self").get("DisplayName")
  76. except KeyError as e: # 处理偶尔没有对方信息的情况
  77. logger.warn("[WX]get other_user_id failed: " + str(e))
  78. if self.from_user_id == user_id:
  79. self.other_user_id = self.to_user_id
  80. else:
  81. self.other_user_id = self.from_user_id
  82. if self.is_group:
  83. self.is_at = itchat_msg["IsAt"]
  84. self.actual_user_id = itchat_msg["ActualUserName"]
  85. if self.ctype not in [ContextType.JOIN_GROUP, ContextType.PATPAT]:
  86. self.actual_user_nickname = itchat_msg["ActualNickName"]