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.

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