1- '''
2- 构建2个管道
3- '''
41import itchat , time
52from itchat .content import TEXT
63import zmq
118
129codelab_adapter_dir = pathlib .Path .home () / "codelab_adapter"
1310
14- class WechatGateway (Extension ):
15- '''
16- todo 作为网关
17- 使用插件来启停它
18- '''
1911
12+ class WechatGateway (Extension ):
2013 def __init__ (self ):
2114 super ().__init__ ()
22- self .EXTENSION_ID = "eim/wechat" # 放在内部
15+ self .EXTENSION_ID = "eim/wechat"
2316 self .quit_code = "quit!"
2417
2518 def extension_message_handle (self , topic , payload ):
@@ -36,8 +29,8 @@ def extension_message_handle(self, topic, payload):
3629 if type == 'user' :
3730 user2SendMessage = itchat .search_friends (nickName = username )[0 ]
3831 user2SendMessage .send (text )
39- except :
40- pass
32+ except Exception as e :
33+ self . logger . error ( str ( e ))
4134
4235 def run (self ):
4336 self .wechat_run_as_thread ()
@@ -66,34 +59,33 @@ def text_reply(self, msg):
6659 "content" : content ,
6760 }
6861 self .publish (message )
69- except :
70- pass
62+ except Exception as e :
63+ self . logger . error ( str ( e ))
7164
7265 # 群消息
7366 def group_text_reply (self , msg ):
7467 '''
7568 将群消息发往scratch
7669 '''
77-
70+
7871 content = msg .text
7972 username = msg .actualNickName
8073 try :
8174 # 群消息有时候会有问题 msg.User.NickName不存在
8275 group_name = msg .User .NickName
8376 except :
8477 return
85- # todo 主动发信息 第一条信息发两次
8678 try :
87- # 如果有groupname就是群消息
79+ # groupname: 群消息
8880 message = {}
8981 message ["payload" ] = {
9082 "username" : username ,
9183 "content" : content ,
9284 "groupname" : group_name
9385 }
9486 self .publish (message )
95- except :
96- pass
87+ except Exception as e :
88+ self . logger . error ( str ( e ))
9789
9890 @threaded
9991 def wechat_run_as_thread (self ):
@@ -106,7 +98,8 @@ def wechat_run_as_thread(self):
10698 itchat .msg_register (TEXT , isGroupChat = True )(self .group_text_reply )
10799 itchat .auto_login (
108100 True , picDir = picDir , statusStorageDir = statusStorageDir )
109- itchat .run (True )
101+ itchat .run (True ) # todo 提示有些账号无法登录
102+
110103
111104export = WechatGateway
112105
0 commit comments