Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

103 Zeilen
5.4KB

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