33"""
44wechat channel
55"""
6+
67import itchat
78import json
89from itchat .content import *
910from channel .channel import Channel
1011from concurrent .futures import ThreadPoolExecutor
1112from common .log import logger
13+ from common .tmp_dir import TmpDir
1214from config import conf
1315import requests
1416import io
1820
1921@itchat .msg_register (TEXT )
2022def handler_single_msg (msg ):
21- WechatChannel ().handle (msg )
23+ WechatChannel ().handle_text (msg )
2224 return None
2325
2426
@@ -28,6 +30,12 @@ def handler_group_msg(msg):
2830 return None
2931
3032
33+ @itchat .msg_register (VOICE )
34+ def handler_single_voice (msg ):
35+ WechatChannel ().handle_voice (msg )
36+ return None
37+
38+
3139class WechatChannel (Channel ):
3240 def __init__ (self ):
3341 pass
@@ -39,12 +47,27 @@ def startup(self):
3947 # start message listener
4048 itchat .run ()
4149
42- def handle (self , msg ):
43- logger .debug ("[WX]receive msg: " + json .dumps (msg , ensure_ascii = False ))
50+ def handle_voice (self , msg ):
51+ if conf ().get ('speech_recognition' ) != True :
52+ return
53+ logger .debug ("[WX]receive voice msg: " + msg ['FileName' ])
54+ thread_pool .submit (self ._do_handle_voice , msg )
55+
56+ def _do_handle_voice (self , msg ):
57+ fileName = TmpDir ().path () + msg ['FileName' ]
58+ msg .download (fileName )
59+ content = super ().build_voice_to_text (fileName )
60+ self ._handle_single_msg (msg , content , conf ().get ('voice_reply_voice' ))
61+
62+ def handle_text (self , msg ):
63+ logger .debug ("[WX]receive text msg: " + json .dumps (msg , ensure_ascii = False ))
64+ content = msg ['Text' ]
65+ self ._handle_single_msg (msg , content , False )
66+
67+ def _handle_single_msg (self , msg , content , reply_voice = False ):
4468 from_user_id = msg ['FromUserName' ]
4569 to_user_id = msg ['ToUserName' ] # 接收人id
4670 other_user_id = msg ['User' ]['UserName' ] # 对手方id
47- content = msg ['Text' ]
4871 match_prefix = self .check_prefix (content , conf ().get ('single_chat_prefix' ))
4972 if "」\n - - - - - - - - - - - - - - -" in content :
5073 logger .debug ("[WX]reference query skipped" )
@@ -60,9 +83,10 @@ def handle(self, msg):
6083 if img_match_prefix :
6184 content = content .split (img_match_prefix , 1 )[1 ].strip ()
6285 thread_pool .submit (self ._do_send_img , content , from_user_id )
63- else :
64- thread_pool .submit (self ._do_send , content , from_user_id )
65-
86+ elif reply_voice :
87+ thread_pool .submit (self ._do_send_voice , content , from_user_id )
88+ else :
89+ thread_pool .submit (self ._do_send_text , content , from_user_id )
6690 elif to_user_id == other_user_id and match_prefix :
6791 # 自己给好友发送消息
6892 str_list = content .split (match_prefix , 1 )
@@ -72,8 +96,10 @@ def handle(self, msg):
7296 if img_match_prefix :
7397 content = content .split (img_match_prefix , 1 )[1 ].strip ()
7498 thread_pool .submit (self ._do_send_img , content , to_user_id )
99+ elif reply_voice :
100+ thread_pool .submit (self ._do_send_voice , content , to_user_id )
75101 else :
76- thread_pool .submit (self ._do_send , content , to_user_id )
102+ thread_pool .submit (self ._do_send_text , content , to_user_id )
77103
78104
79105 def handle_group (self , msg ):
@@ -105,10 +131,24 @@ def handle_group(self, msg):
105131 thread_pool .submit (self ._do_send_group , content , msg )
106132
107133 def send (self , msg , receiver ):
108- logger .info ('[WX] sendMsg={}, receiver={}' .format (msg , receiver ))
109134 itchat .send (msg , toUserName = receiver )
135+ logger .info ('[WX] sendMsg={}, receiver={}' .format (msg , receiver ))
110136
111- def _do_send (self , query , reply_user_id ):
137+ def _do_send_voice (self , query , reply_user_id ):
138+ try :
139+ if not query :
140+ return
141+ context = dict ()
142+ context ['from_user_id' ] = reply_user_id
143+ reply_text = super ().build_reply_content (query , context )
144+ if reply_text :
145+ replyFile = super ().build_text_to_voice (reply_text )
146+ itchat .send_file (replyFile , toUserName = reply_user_id )
147+ logger .info ('[WX] sendFile={}, receiver={}' .format (replyFile , reply_user_id ))
148+ except Exception as e :
149+ logger .exception (e )
150+
151+ def _do_send_text (self , query , reply_user_id ):
112152 try :
113153 if not query :
114154 return
@@ -138,8 +178,8 @@ def _do_send_img(self, query, reply_user_id):
138178 image_storage .seek (0 )
139179
140180 # 图片发送
141- logger .info ('[WX] sendImage, receiver={}' .format (reply_user_id ))
142181 itchat .send_image (image_storage , reply_user_id )
182+ logger .info ('[WX] sendImage, receiver={}' .format (reply_user_id ))
143183 except Exception as e :
144184 logger .exception (e )
145185
0 commit comments