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.

120 lines
7.3KB

  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. notes_join_group = ["加入群聊", "加入了群聊", "invited", "joined"] # 可通过添加对应语言的加入群聊通知中的关键词适配更多
  15. notes_bot_join_group = ["邀请你", "invited you", "You've joined", "你通过扫描"]
  16. notes_exit_group = ["移出了群聊", "removed"] # 可通过添加对应语言的踢出群聊通知中的关键词适配更多
  17. notes_patpat = ["拍了拍我", "tickled my", "tickled me"] # 可通过添加对应语言的拍一拍通知中的关键词适配更多
  18. if itchat_msg["Type"] == TEXT:
  19. self.ctype = ContextType.TEXT
  20. self.content = itchat_msg["Text"]
  21. elif itchat_msg["Type"] == VOICE:
  22. self.ctype = ContextType.VOICE
  23. self.content = TmpDir().path() + itchat_msg["FileName"] # content直接存临时目录路径
  24. self._prepare_fn = lambda: itchat_msg.download(self.content)
  25. elif itchat_msg["Type"] == PICTURE and itchat_msg["MsgType"] == 3:
  26. self.ctype = ContextType.IMAGE
  27. self.content = TmpDir().path() + itchat_msg["FileName"] # content直接存临时目录路径
  28. self._prepare_fn = lambda: itchat_msg.download(self.content)
  29. elif itchat_msg["Type"] == NOTE and itchat_msg["MsgType"] == 10000:
  30. if is_group:
  31. if any(note_bot_join_group in itchat_msg["Content"] for note_bot_join_group in notes_bot_join_group): # 邀请机器人加入群聊
  32. logger.warn("机器人加入群聊消息,不处理~")
  33. pass
  34. elif any(note_join_group in itchat_msg["Content"] for note_join_group in notes_join_group): # 若有任何在notes_join_group列表中的字符串出现在NOTE中
  35. # 这里只能得到nickname, actual_user_id还是机器人的id
  36. if "加入群聊" not in itchat_msg["Content"]:
  37. self.ctype = ContextType.JOIN_GROUP
  38. self.content = itchat_msg["Content"]
  39. if "invited" in itchat_msg["Content"]: # 匹配英文信息
  40. self.actual_user_nickname = re.findall(r'invited\s+(.+?)\s+to\s+the\s+group\s+chat', itchat_msg["Content"])[0]
  41. elif "joined" in itchat_msg["Content"]: # 匹配通过二维码加入的英文信息
  42. self.actual_user_nickname = re.findall(r'"(.*?)" joined the group chat via the QR Code shared by', itchat_msg["Content"])[0]
  43. elif "加入了群聊" in itchat_msg["Content"]:
  44. self.actual_user_nickname = re.findall(r"\"(.*?)\"", itchat_msg["Content"])[-1]
  45. elif "加入群聊" in itchat_msg["Content"]:
  46. self.ctype = ContextType.JOIN_GROUP
  47. self.content = itchat_msg["Content"]
  48. self.actual_user_nickname = re.findall(r"\"(.*?)\"", itchat_msg["Content"])[0]
  49. elif any(note_exit_group in itchat_msg["Content"] for note_exit_group in notes_exit_group): # 若有任何在notes_exit_group列表中的字符串出现在NOTE中
  50. self.ctype = ContextType.EXIT_GROUP
  51. self.content = itchat_msg["Content"]
  52. self.actual_user_nickname = re.findall(r"\"(.*?)\"", itchat_msg["Content"])[0]
  53. elif "你已添加了" in itchat_msg["Content"]: #通过好友请求
  54. self.ctype = ContextType.ACCEPT_FRIEND
  55. self.content = itchat_msg["Content"]
  56. elif any(note_patpat in itchat_msg["Content"] for note_patpat in notes_patpat): # 若有任何在notes_patpat列表中的字符串出现在NOTE中:
  57. self.ctype = ContextType.PATPAT
  58. self.content = itchat_msg["Content"]
  59. if is_group:
  60. if "拍了拍我" in itchat_msg["Content"]: # 识别中文
  61. self.actual_user_nickname = re.findall(r"\"(.*?)\"", itchat_msg["Content"])[0]
  62. elif ("tickled my" in itchat_msg["Content"] or "tickled me" in itchat_msg["Content"]):
  63. self.actual_user_nickname = re.findall(r'^(.*?)(?:tickled my|tickled me)', itchat_msg["Content"])[0]
  64. else:
  65. raise NotImplementedError("Unsupported note message: " + itchat_msg["Content"])
  66. elif itchat_msg["Type"] == ATTACHMENT:
  67. self.ctype = ContextType.FILE
  68. self.content = TmpDir().path() + itchat_msg["FileName"] # content直接存临时目录路径
  69. self._prepare_fn = lambda: itchat_msg.download(self.content)
  70. elif itchat_msg["Type"] == SHARING:
  71. self.ctype = ContextType.SHARING
  72. self.content = itchat_msg.get("Url")
  73. else:
  74. raise NotImplementedError("Unsupported message type: Type:{} MsgType:{}".format(itchat_msg["Type"], itchat_msg["MsgType"]))
  75. self.from_user_id = itchat_msg["FromUserName"]
  76. self.to_user_id = itchat_msg["ToUserName"]
  77. user_id = itchat.instance.storageClass.userName
  78. nickname = itchat.instance.storageClass.nickName
  79. # 虽然from_user_id和to_user_id用的少,但是为了保持一致性,还是要填充一下
  80. # 以下很繁琐,一句话总结:能填的都填了。
  81. if self.from_user_id == user_id:
  82. self.from_user_nickname = nickname
  83. if self.to_user_id == user_id:
  84. self.to_user_nickname = nickname
  85. try: # 陌生人时候, User字段可能不存在
  86. # my_msg 为True是表示是自己发送的消息
  87. self.my_msg = itchat_msg["ToUserName"] == itchat_msg["User"]["UserName"] and \
  88. itchat_msg["ToUserName"] != itchat_msg["FromUserName"]
  89. self.other_user_id = itchat_msg["User"]["UserName"]
  90. self.other_user_nickname = itchat_msg["User"]["NickName"]
  91. if self.other_user_id == self.from_user_id:
  92. self.from_user_nickname = self.other_user_nickname
  93. if self.other_user_id == self.to_user_id:
  94. self.to_user_nickname = self.other_user_nickname
  95. if itchat_msg["User"].get("Self"):
  96. # 自身的展示名,当设置了群昵称时,该字段表示群昵称
  97. self.self_display_name = itchat_msg["User"].get("Self").get("DisplayName")
  98. except KeyError as e: # 处理偶尔没有对方信息的情况
  99. logger.warn("[WX]get other_user_id failed: " + str(e))
  100. if self.from_user_id == user_id:
  101. self.other_user_id = self.to_user_id
  102. else:
  103. self.other_user_id = self.from_user_id
  104. if self.is_group:
  105. self.is_at = itchat_msg["IsAt"]
  106. self.actual_user_id = itchat_msg["ActualUserName"]
  107. if self.ctype not in [ContextType.JOIN_GROUP, ContextType.PATPAT, ContextType.EXIT_GROUP]:
  108. self.actual_user_nickname = itchat_msg["ActualNickName"]