Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

wechat_message.py 3.9KB

il y a 1 an
il y a 1 an
il y a 1 an
il y a 1 an
il y a 1 an
il y a 1 an
il y a 1 an
il y a 1 an
il y a 1 an
il y a 1 an
il y a 1 an
il y a 1 an
il y a 1 an
il y a 1 an
il y a 1 an
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 (
  27. "加入群聊" in itchat_msg["Content"] or "加入了群聊" in itchat_msg["Content"]
  28. ):
  29. self.ctype = ContextType.JOIN_GROUP
  30. logger.debug("[WX]join group message: " + itchat_msg["Content"])
  31. self.content = itchat_msg["Content"]
  32. # 这里只能得到nickname, actual_user_id还是机器人的id
  33. if "加入了群聊" in itchat_msg["Content"]:
  34. self.actual_user_nickname = re.findall(
  35. r"\"(.*?)\"", itchat_msg["Content"]
  36. )[-1]
  37. elif "加入群聊" in itchat_msg["Content"]:
  38. self.actual_user_nickname = re.findall(
  39. r"\"(.*?)\"", itchat_msg["Content"]
  40. )[0]
  41. else:
  42. raise NotImplementedError(
  43. "Unsupported note message: " + itchat_msg["Content"]
  44. )
  45. else:
  46. raise NotImplementedError(
  47. "Unsupported message type: Type:{} MsgType:{}".format(
  48. itchat_msg["Type"], itchat_msg["MsgType"]
  49. )
  50. )
  51. self.from_user_id = itchat_msg["FromUserName"]
  52. self.to_user_id = itchat_msg["ToUserName"]
  53. user_id = itchat.instance.storageClass.userName
  54. nickname = itchat.instance.storageClass.nickName
  55. # 虽然from_user_id和to_user_id用的少,但是为了保持一致性,还是要填充一下
  56. # 以下很繁琐,一句话总结:能填的都填了。
  57. if self.from_user_id == user_id:
  58. self.from_user_nickname = nickname
  59. if self.to_user_id == user_id:
  60. self.to_user_nickname = nickname
  61. try: # 陌生人时候, 'User'字段可能不存在
  62. self.other_user_id = itchat_msg["User"]["UserName"]
  63. self.other_user_nickname = itchat_msg["User"]["NickName"]
  64. if self.other_user_id == self.from_user_id:
  65. self.from_user_nickname = self.other_user_nickname
  66. if self.other_user_id == self.to_user_id:
  67. self.to_user_nickname = self.other_user_nickname
  68. except KeyError as e: # 处理偶尔没有对方信息的情况
  69. logger.warn("[WX]get other_user_id failed: " + str(e))
  70. if self.from_user_id == user_id:
  71. self.other_user_id = self.to_user_id
  72. else:
  73. self.other_user_id = self.from_user_id
  74. if self.is_group:
  75. self.is_at = itchat_msg["IsAt"]
  76. self.actual_user_id = itchat_msg["ActualUserName"]
  77. if self.ctype != ContextType.JOIN_GROUP:
  78. self.actual_user_nickname = itchat_msg["ActualNickName"]