55"""
66
77import os
8+ import re
89import requests
910import io
1011import time
12+ from common .singleton import singleton
1113from lib import itchat
1214import json
1315from lib .itchat .content import *
@@ -68,11 +70,11 @@ def wrapper(self, msg):
6870 return func (self , msg )
6971 return wrapper
7072
71-
73+ @ singleton
7274class WechatChannel (Channel ):
7375 def __init__ (self ):
74- self .userName = None
75- self .nickName = None
76+ self .user_id = None
77+ self .name = None
7678 self .receivedMsgs = ExpiredDict (60 * 60 * 24 )
7779
7880 def startup (self ):
@@ -90,9 +92,9 @@ def startup(self):
9092 itchat .auto_login (enableCmdQR = 2 , hotReload = hotReload )
9193 else :
9294 raise e
93- self .userName = itchat .instance .storageClass .userName
94- self .nickName = itchat .instance .storageClass .nickName
95- logger .info ("Wechat login success, username : {}, nickname: {}" .format (self .userName , self .nickName ))
95+ self .user_id = itchat .instance .storageClass .userName
96+ self .name = itchat .instance .storageClass .nickName
97+ logger .info ("Wechat login success, user_id : {}, nickname: {}" .format (self .user_id , self .name ))
9698 # start message listener
9799 itchat .run ()
98100
@@ -160,17 +162,12 @@ def handle_group(self, msg):
160162 group_id = msg ['User' ].get ('UserName' , None )
161163 if not group_name :
162164 return ""
163- origin_content = msg ['Content' ]
164- content = msg ['Content' ]
165- content_list = content .split (' ' , 1 )
166- context_special_list = content .split ('\u2005 ' , 1 )
167- if len (context_special_list ) == 2 :
168- content = context_special_list [1 ]
169- elif len (content_list ) == 2 :
170- content = content_list [1 ]
165+ content = msg .content
171166 if "」\n - - - - - - - - - - - - - - -" in content :
172167 logger .debug ("[WX]reference query skipped" )
173168 return ""
169+ pattern = f'@{ self .name } (\u2005 |\u0020 )'
170+ content = re .sub (pattern , r'' , content )
174171
175172 config = conf ()
176173 group_name_white_list = config .get ('group_name_white_list' , [])
0 commit comments