1515from itchat .content import (
1616 TEXT
1717)
18- from everyday_wechat . utils . common import md5_encode
18+
1919from everyday_wechat .utils .data_collection import (
2020 get_bot_info ,
2121 get_weather_info ,
2525 get_constellation_info
2626)
2727from everyday_wechat .utils import config
28+ from everyday_wechat .utils .itchat_helper import (
29+ init_wechat_config ,
30+ set_system_notice ,
31+ )
32+ from everyday_wechat .utils .group_helper import (
33+ handle_group_helper
34+ )
35+ from everyday_wechat .utils .friend_helper import (
36+ handle_friend
37+ )
2838
29- reply_userNames = []
3039FILEHELPER_MARK = ['文件传输助手' , 'filehelper' ] # 文件传输助手标识
3140FILEHELPER = 'filehelper'
3241TIME_COMPILE = re .compile (r'^\s*([01]?[0-9]|2[0-3])\s*[::\-]\s*([0-5]?[0-9])\s*$' )
@@ -69,10 +78,10 @@ def _online():
6978 return False
7079
7180 hotReload = not config .get ('is_forced_switch' , False ) # 切换微信号,重新扫码。
72- loginCallback = init_wechat
81+ loginCallback = init_data
7382 exitCallback = exit_msg
7483 for _ in range (2 ): # 尝试登录 2 次。
75- if platform .system () == 'Windows' :
84+ if platform .system () in ( 'Windows' , 'Darwin' ) :
7685 itchat .auto_login (hotReload = hotReload , loginCallback = loginCallback , exitCallback = exitCallback )
7786 itchat .run (blockThread = True )
7887 else :
@@ -88,98 +97,25 @@ def _online():
8897 return False
8998
9099
91- def init_wechat ():
100+ def init_data ():
92101 """ 初始化微信所需数据 """
93102 set_system_notice ('登录成功' )
94-
95- # conf = get_yaml()
96103 itchat .get_friends (update = True ) # 更新好友数据。
97104 itchat .get_chatrooms (update = True ) # 更新群聊数据。
98105
99- # 从config copy ,用于保存新的接口内容。
100- myset = config .copy ()
101- # start---------------------------处理自动回复好友---------------------------start
102- relay = myset .get ('auto_relay_info' )
103- if relay .get ('is_auto_relay' ):
104- auto_reply_uuids = []
105- for name in relay .get ('auto_reply_names' ):
106- if name .lower () in FILEHELPER_MARK : # 判断是否文件传输助手
107- if FILEHELPER not in reply_userNames :
108- auto_reply_uuids .append (FILEHELPER )
109- continue
110- friend = get_friend (name )
111- if friend :
112- auto_reply_uuids .append (friend ['UserName' ])
113- else :
114- print ('自动回复中的好友昵称『{}』有误。' .format (name ))
115- relay ['auto_reply_uuids' ] = set (auto_reply_uuids )
116- print ('已开启图灵自动回复...' )
117- # end---------------------------处理自动回复好友---------------------------end
118-
119- alarm = myset .get ('alarm_info' )
120- alarm_dict = {}
121- if alarm .get ('is_alarm' ):
122- for gi in alarm .get ('girlfriend_infos' ):
123- ats = gi .get ('alarm_timed' )
124- if not ats :
125- continue
126- uuid_list = []
127- # start---------------------------处理好友---------------------------start
128- friends = gi .get ('wechat_name' )
129- if isinstance (friends , str ):
130- friends = [friends ]
131- if isinstance (friends , list ):
132- for name in friends :
133- if name .lower () in FILEHELPER_MARK : # 判断是否文件传输助手
134- uuid_list .append (FILEHELPER )
135- continue
136- name_info = get_friend (name )
137- if not name_info :
138- print ('用户昵称{}无效' .format (name ))
139- else :
140- uuid_list .append (name_info ['UserName' ])
141- # end---------------------------处理好友---------------------------end
142-
143- # start---------------------------群组处理---------------------------start
144- group_names = gi .get ('group_name' )
145- if isinstance (group_names , str ):
146- group_names = [group_names ]
147- if isinstance (group_names , list ):
148- for name in group_names :
149- name_info = get_group (name )
150- if not name_info :
151- print ('定时任务中的群聊名称『{}』有误。'
152- '(注意:必须要把需要的群聊保存到通讯录)' .format (name ))
153- else :
154- uuid_list .append (name_info ['UserName' ])
155- # end---------------------------群组处理---------------------------end
156-
157- # start---------------------------定时处理---------------------------start
158- if isinstance (ats , str ):
159- ats = [ats ]
160- if isinstance (ats , list ):
161- for at in ats :
162- times = TIME_COMPILE .findall (at )
163- if not times :
164- print ('时间{}格式出错' .format (at ))
165- continue
166- hour , minute = int (times [0 ][0 ]), int (times [0 ][1 ])
167- temp_dict = {'hour' : hour , 'minute' : minute , 'uuid_list' : uuid_list }
168- temp_dict .update (gi )
169- alarm_dict [md5_encode (str (temp_dict ))] = temp_dict
170- # end---------------------------定时处理---------------------------end
171- alarm ['alarm_dict' ] = alarm_dict
172-
173- # 将解析的数据保存于config中。
174- config .update (myset )
175- # print(json.dumps(alarm_dict, ensure_ascii=False))
106+ init_wechat_config () # 初始化所有配置内容
176107
177108 # 提醒内容不为空时,启动定时任务
109+ alarm_dict = config .get ('alarm_info' ).get ('alarm_dict' )
178110 if alarm_dict :
179111 init_alarm (alarm_dict ) # 初始化定时任务
180112
181113
182114def init_alarm (alarm_dict ):
115+ """
116+ 初始化定时任务
117+ :param alarm_dict: 定时相关内容
118+ """
183119 # 定时任务
184120 scheduler = BackgroundScheduler ()
185121 for key , value in alarm_dict .items ():
@@ -197,98 +133,40 @@ def send_alarm_msg(key):
197133
198134 gf = conf .get (key )
199135 # print(gf)
200- calendar_info = get_calendar_info (gf .get ('calendar' ))
136+ is_tomorrow = gf .get ('is_tomorrow' , False )
137+ calendar_info = get_calendar_info (gf .get ('calendar' ), is_tomorrow )
138+ weather = get_weather_info (gf .get ('city_name' ), is_tomorrow )
139+ horoscope = get_constellation_info (gf .get ("horescope" ), is_tomorrow )
201140 dictum = get_dictum_info (gf .get ('dictum_channel' ))
202- weather = get_weather_info (gf .get ('city_name' ))
203141 diff_time = get_diff_time (gf .get ('start_date' ), gf .get ('start_date_msg' ))
204142 sweet_words = gf .get ('sweet_words' )
205- horoscope = get_constellation_info (gf .get ("horescope" ))
206-
207143 send_msg = '\n ' .join (x for x in [calendar_info , weather , horoscope , dictum , diff_time , sweet_words ] if x )
208144 # print('\n' + send_msg + '\n')
209-
210145 if not send_msg or not is_online (): return
211-
212146 uuid_list = gf .get ('uuid_list' )
213147 for uuid in uuid_list :
214148 time .sleep (1 )
215149 itchat .send (send_msg , toUserName = uuid )
216150 print ('\n 定时内容:\n {}\n 发送成功...\n \n ' .format (send_msg ))
217-
218151 print ('自动提醒消息发送完成...\n ' )
219152
220153
221154@itchat .msg_register ([TEXT ])
222155def text_reply (msg ):
223156 """ 监听用户消息,用于自动回复 """
224- try :
225- # if not get_yaml().get('is_auto_relay'):
226- # return
227- conf = config .get ('auto_relay_info' )
228- if not conf .get ('is_auto_relay' ):
229- return
230- # print(json.dumps(msg, ensure_ascii=False))
231- # 获取发送者的用户id
232- uuid = FILEHELPER if msg ['ToUserName' ] == FILEHELPER else msg .fromUserName
233- # 如果用户id是自动回复列表的人员
234- if uuid in conf .get ('auto_reply_uuids' ):
235- receive_text = msg .text # 好友发送来的消息内容
236- # 好友叫啥,用于打印
237- nickName = FILEHELPER if uuid == FILEHELPER else msg .user .nickName
238- print ('\n {}发来信息:{}' .format (nickName , receive_text ))
239- reply_text = get_bot_info (receive_text , uuid ) # 获取自动回复
240- if reply_text : # 如内容不为空,回复消息
241- time .sleep (random .randint (1 , 2 )) # 休眠一秒,保安全。想更快的,可以直接注释。
242- reply_text = reply_text if not uuid == FILEHELPER else '机器人回复:' + reply_text
243- itchat .send (reply_text , toUserName = uuid )
244- print ('回复{}:{}' .format (nickName , reply_text ))
245- else :
246- print ('自动回复失败\n ' )
247- except Exception as exception :
248- print (str (exception ))
157+ handle_friend (msg )
158+ # print(json.dumps(msg, ensure_ascii=False))
249159
250160
251- def set_system_notice (text ):
252- """
253- 给文件传输助手发送系统日志。
254- :param text:str 日志内容
255- """
256- if text :
257- text = '系统通知:' + text
258- itchat .send (text , toUserName = FILEHELPER )
161+ @itchat .msg_register ([TEXT ], isGroupChat = True )
162+ def text_group (msg ):
163+ """ 监听用户消息,用于自动回复 """
164+ handle_group_helper (msg )
259165
260166
261167def exit_msg ():
262- """ 项目中止提醒 """
263- set_system_notice ('项目已断开连接' )
264-
265-
266- def get_group (gruop_name , update = False ):
267- """
268- 根据群组名获取群组数据
269- :param wechat_name:str, 群组名
270- :param update: bool 强制更新群组数据
271- :return: obj 单个群组信息
272- """
273- if update : itchat .get_chatrooms (update = True )
274- if not gruop_name : return None
275- groups = itchat .search_chatrooms (name = gruop_name )
276- if not groups : return None
277- return groups [0 ]
278-
279-
280- def get_friend (wechat_name , update = False ):
281- """
282- 根据用户名获取用户数据
283- :param wechat_name: str 用户名
284- :param update: bool 强制更新用户数据
285- :return: obj 单个好友信息
286- """
287- if update : itchat .get_friends (update = True )
288- if not wechat_name : return None
289- friends = itchat .search_friends (name = wechat_name )
290- if not friends : return None
291- return friends [0 ]
168+ """ 退出通知 """
169+ print ('程序已退出' )
292170
293171
294172if __name__ == '__main__' :
0 commit comments