def handle_group_helper(msg): """ 处理群消息 :param msg: :return: """ uuid = msg.fromUserName # 群 uid ated_uuid = msg.actualUserName # 艾特你的用户的uuid ated_name = msg.actualNickName # 艾特你的人的群里的名称 text = msg['Text'] # 发送到群里的消息。 # 自己通过手机端微信发出的消息不作处理 if ated_uuid == config.get('wechat_uuid'): return conf = config.get('group_helper_conf') if not conf.get('is_open'): return # 如果开启了 『艾特才回复』,而群用户又没有艾特你。不处理消息 if conf.get('is_at') and not msg.isAt: return is_all = conf.get('is_all', False) user_uuids = conf.get('group_black_uuids') if is_all else conf.get( 'group_white_uuids') # 开启回复所有群,而用户是黑名单,不处理消息 if is_all and uuid in user_uuids: return # 未回复所有群,而用户不是白名单,不处理消息 if not is_all and uuid not in user_uuids: return # 去掉 at 标记 text = re.sub(at_compile, '', text) # 如果是帮助设置 helps = re.findall(help_complie, text, re.I) if helps: retext = help_group_content.format(ated_name=ated_name) itchat.send(retext, uuid) return # 是否是明天,用于日历,天气,星座查询 is_tomorrow = re.findall(tomorrow_compile, text) if is_tomorrow: is_tomorrow = True htext = re.sub(tomorrow_compile, '', text) else: is_tomorrow = False htext = text htext = re.sub(punct_complie, '', htext) # 去句末的标点 # 已开启天气查询,并包括天气关键词 if conf.get('is_weather'): if re.findall(weather_compile, htext, re.I): city = re.sub(weather_clean_compile, '', text, flags=re.IGNORECASE).strip() if not city: # 如果只是输入城市名 # 从缓存数据库找最后一次查询的城市名 city = find_user_city(ated_uuid) if not city: # 缓存数据库没有保存,通过用户的资料查城市 city = get_city_by_uuid(ated_uuid) if not city: retext = weather_null_msg.format(ated_name=ated_name) itchat.send(retext, uuid) return _date = datetime.now().strftime('%Y-%m-%d') weather_info = find_weather(_date, city) if weather_info: retext = common_msg.format(ated_name=ated_name, text=weather_info) itchat.send(retext, uuid) return weather_info = get_weather_info(city) if weather_info: # print(ated_name, city, retext) retext = common_msg.format(ated_name=ated_name, text=weather_info) itchat.send(retext, uuid) data = { '_date': _date, 'city_name': city, 'weather_info': weather_info, 'userid': ated_uuid, 'last_time': datetime.now() } udpate_weather(data) # userid,city_name,last_time,group_name udpate_weather_city data2 = { 'userid': ated_uuid, 'city_name': city, 'last_time': datetime.now() } udpate_user_city(data2) return else: retext = weather_error_msg.format(ated_name=ated_name, city=city) itchat.send(retext, uuid) return return # 已开启日历,并包含日历 if conf.get('is_calendar'): if re.findall(calendar_complie, htext, flags=re.IGNORECASE): calendar_text = re.sub(calendar_complie, '', htext).strip() if calendar_text: # 日历后面填上日期了 dates = re.findall(calendar_date_compile, calendar_text) if not dates: retext = calendar_error_msg.format(ated_name=ated_name) itchat.send(retext, uuid) return _date = '{}-{:0>2}-{:0>2}'.format(*dates[0]) # 用于保存数据库 rt_date = '{}{:0>2}{:0>2}'.format(*dates[0]) # 用于查询日历 else: # 日历 后面没有日期,则默认使用今日。 _date = datetime.now().strftime('%Y-%m-%d') rt_date = datetime.now().strftime('%Y%m%d') # 从数据库缓存中记取内容 cale_info = find_perpetual_calendar(_date) if cale_info: retext = common_msg.format(ated_name=ated_name, text=cale_info) itchat.send(retext, uuid) return # 取网络数据 cale_info = get_rtcalendar(rt_date) if cale_info: retext = common_msg.format(ated_name=ated_name, text=cale_info) itchat.send(retext, uuid) update_perpetual_calendar(_date, cale_info) # 保存数据到数据库 return else: # 查询无结果 retext = calendar_no_result_msg.format(ated_name=ated_name, _date=_date) itchat.send(retext, uuid) return # 垃圾分类查询 if conf.get('is_rubbish'): if re.findall(rubbish_complie, htext, re.I): key = re.sub(rubbish_complie, '', htext, flags=re.IGNORECASE).strip() if not key: retext = rubbish_null_msg.format(ated_name=ated_name) itchat.send(retext, uuid) return _type = find_rubbish(key) if _type: retext = rubbish_normal_msg.format(ated_name=ated_name, name=key, _type=_type) itchat.send(retext, uuid) return _type, return_list, other = get_atoolbox_rubbish(key) if _type: retext = rubbish_normal_msg.format(ated_name=ated_name, name=key, _type=_type) itchat.send_msg(retext, uuid) elif other: retext = rubbish_other_msg.format(ated_name=ated_name, name=key, other=other) itchat.send_msg(retext, uuid) else: retext = rubbish_nothing_msg.format(ated_name=ated_name, name=key) itchat.send_msg(retext, uuid) if return_list: update_rubbish(return_list) # 保存数据库 return if conf.get('is_moviebox'): if re.findall(moviebox_complie, htext, re.I): moviebox_text = re.sub(moviebox_complie, '', htext).strip() if moviebox_text: # 日历后面填上日期了 dates = re.findall(calendar_date_compile, moviebox_text) if not dates: retext = calendar_error_msg.format(ated_name=ated_name) itchat.send(retext, uuid) return _date = '{}{:0>2}{:0>2}'.format(*dates[0]) else: # 日历 后面没有日期,则默认使用今日。 _date = datetime.now().strftime('%Y%m%d') # 从数据库缓存中记取内容 mb_info = find_movie_box(_date) if mb_info: retext = common_msg.format(ated_name=ated_name, text=mb_info) itchat.send(retext, uuid) return is_expired = False cur_date = datetime.now().date() query_date = datetime.strptime(_date, '%Y%m%d').date() if query_date < cur_date: is_expired = True # 取网络数据 mb_info = get_maoyan_movie_box(_date, is_expired) if mb_info: retext = common_msg.format(ated_name=ated_name, text=mb_info) itchat.send(retext, uuid) update_movie_box(_date, mb_info, is_expired) # 保存数据到数据库 return else: # 查询无结果 retext = moiebox_no_result_msg.format(ated_name=ated_name, _date=_date) itchat.send(retext, uuid) return # 处理订单号 if conf.get('is_express'): express_list = re.findall(express_complie, htext, re.I) if express_list: express_code = express_list[0] db_data = find_express(express_code, uuid) shipper_code, shipper_name = '', '' if db_data: if not db_data['is_forced_update']: info = db_data['info'] retext = common_msg.format(ated_name=ated_name, text=info) itchat.send(retext, uuid) return shipper_code = db_data['shipper_code'] shipper_name = db_data['shipper_name'] data = get_express_info(express_code, shipper_name=shipper_name, shipper_code=shipper_code) if data: info = data['info'] retext = common_msg.format(ated_name=ated_name, text=info) itchat.send(retext, uuid) update_express(data, uuid) return else: print('未查询到此订单号的快递物流轨迹。') return # 其他结果都没有匹配到,走自动回复的路 if conf.get('is_auto_reply'): reply_text = get_bot_info(text, ated_uuid) # 获取自动回复 if reply_text: # 如内容不为空,回复消息 reply_text = common_msg.format(ated_name=ated_name, text=reply_text) itchat.send(reply_text, uuid) print('回复{}:{}'.format(ated_name, reply_text)) else: print('自动回复失败\n')
def handle_group_helper(msg): """ 处理群消息 :param msg: :return: """ conf = config.get('group_helper_conf') if not conf.get('is_open'): return text = msg['Text'] # 如果开启了 『艾特才回复』,而群用户又没有艾特你。不处理消息 if conf.get('is_at') and not msg.isAt: return uuid = msg.fromUserName # 群 uid ated_uuid = msg.actualUserName # 艾特你的用户的uuid ated_name = msg.actualNickName # 艾特你的人的群里的名称 is_all = conf.get('is_all', False) user_uuids = conf.get('group_black_uuids') if is_all else conf.get( 'group_white_uuids') # 开启回复所有群,而用户是黑名单,不处理消息 if is_all and uuid in user_uuids: return # 未回复所有群,而用户不是白名单,不处理消息 if not is_all and uuid not in user_uuids: return # 去掉 at 标记 text = at_compile.sub('', text) # 如果是帮助 helps = re.findall(help_complie, text, re.I) if helps: itchat.send(help_group_content, uuid) return # 是否是明天,用于日历,天气,星座查询 is_tomorrow = re.findall(tomorrow_compile, text) if is_tomorrow: is_tomorrow = True htext = re.sub(tomorrow_compile, '', text) else: is_tomorrow = False htext = text htext = re.sub(punct_complie, '', htext) # 去句末的标点 # 已开启天气查询,并包括天气关键词 if conf.get('is_weather'): citys = re.findall(weather_compile, htext) if citys: for x in citys[0]: city = x if city: break else: city = find_user_city(ated_uuid) if not city: city = get_city_by_uuid(ated_uuid) if city: _date = datetime.now().strftime('%Y-%m-%d') weather_info = find_weather(_date, city) if weather_info: retext = common_msg.format(ated_name=ated_name, text=weather_info) itchat.send(retext, uuid) return weather_info = get_weather_info(city) if weather_info: # print(ated_name, city, retext) retext = common_msg.format(ated_name=ated_name, text=weather_info) itchat.send(retext, uuid) data = { '_date': _date, 'city_name': city, 'weather_info': weather_info, 'userid': ated_uuid, 'last_time': datetime.now() } udpate_weather(data) # userid,city_name,last_time,group_name udpate_weather_city data2 = { 'userid': ated_uuid, 'city_name': city, 'last_time': datetime.now() } udpate_user_city(data2) return else: weather_error_msg = '@{ated_name}\u2005\n未找到『{city}』城市的相关信息' retext = weather_error_msg.format(ated_name=ated_name, city=city) itchat.send(retext, uuid) return return # 已开启日历,并包含日历 if conf.get('is_calendar'): if re.findall(calendar_complie, htext): _date = datetime.now().strftime('%Y-%m-%d') cale_info = find_perpetual_calendar(_date) if cale_info: retext = common_msg.format(ated_name=ated_name, text=cale_info) itchat.send(retext, uuid) return rt_date = datetime.now().strftime('%Y%m%d') cale_info = get_rtcalendar(rt_date) if cale_info: retext = common_msg.format(ated_name=ated_name, text=cale_info) itchat.send(retext, uuid) update_perpetual_calendar(_date, cale_info) return return # 其他结果都没有匹配到,走自动回复的路 if conf.get('is_auto_reply'): reply_text = get_bot_info(text, ated_uuid) # 获取自动回复 if reply_text: # 如内容不为空,回复消息 reply_text = common_msg.format(ated_name=ated_name, text=reply_text) itchat.send(reply_text, uuid) print('回复{}:{}'.format(ated_name, reply_text)) else: print('自动回复失败\n')
def handle_group_helper(msg): """ 处理群消息 :param msg: :return: """ conf = config.get('group_helper_conf') if not conf.get('is_open'): return text = msg['Text'] # 如果开启了 『艾特才回复』,而群用户又没有艾特你。不处理消息 if conf.get('is_at') and not msg.isAt: return uuid = msg.fromUserName # 群 uid ated_uuid = msg.actualUserName # 艾特你的用户的uuid ated_name = msg.actualNickName # 艾特你的人的群里的名称 is_all = conf.get('is_all', False) user_uuids = conf.get('group_black_uuids') if is_all else conf.get('group_white_uuids') # 开启回复所有群,而用户是黑名单,不处理消息 if is_all and uuid in user_uuids: return # 未回复所有群,而用户不是白名单,不处理消息 if not is_all and uuid not in user_uuids: return # 去掉 at 标记 text = re.sub(at_compile, '', text) # 如果是帮助 helps = re.findall(help_complie, text, re.I) if helps: retext = help_group_content.format(ated_name=ated_name) itchat.send(retext, uuid) return # 是否是明天,用于日历,天气,星座查询 is_tomorrow = re.findall(tomorrow_compile, text) if is_tomorrow: is_tomorrow = True htext = re.sub(tomorrow_compile, '', text) else: is_tomorrow = False htext = text htext = re.sub(punct_complie, '', htext) # 去句末的标点 # 已开启天气查询,并包括天气关键词 if conf.get('is_weather'): if re.findall(weather_compile, htext, re.I): city = re.sub(weather_clean_compile, '', text) if not city: # 如果只是输入城市名 # 从缓存数据库找最后一次查询的城市名 city = find_user_city(ated_uuid) if not city: # 缓存数据库没有保存,通过用户的资料查城市 city = get_city_by_uuid(ated_uuid) if not city: retext = '请输入城市名' itchat.send(retext, uuid) return _date = datetime.now().strftime('%Y-%m-%d') weather_info = find_weather(_date, city) if weather_info: retext = common_msg.format(ated_name=ated_name, text=weather_info) itchat.send(retext, uuid) return weather_info = get_weather_info(city) if weather_info: # print(ated_name, city, retext) retext = common_msg.format(ated_name=ated_name, text=weather_info) itchat.send(retext, uuid) data = { '_date': _date, 'city_name': city, 'weather_info': weather_info, 'userid': ated_uuid, 'last_time': datetime.now() } udpate_weather(data) # userid,city_name,last_time,group_name udpate_weather_city data2 = { 'userid': ated_uuid, 'city_name': city, 'last_time': datetime.now() } udpate_user_city(data2) return else: retext = weather_error_msg.format(ated_name=ated_name, city=city) itchat.send(retext, uuid) return return # 已开启日历,并包含日历 if conf.get('is_calendar'): if re.findall(calendar_complie, htext, re.I): calendar_text = re.sub(calendar_complie, '', htext) if calendar_text: # 日历后面填上日期了 dates = re.findall(calendar_date_compile, calendar_text) if not dates: retext = '请输入正确的日期' itchat.send(retext, uuid) return _date = '{}-{:0>2}-{:0>2}'.format(*dates[0]) # 用于保存数据库 rt_date = '{}{:0>2}{:0>2}'.format(*dates[0]) # 用于查询日历 else: # 日历 后面没有日期,则默认使用今日。 _date = datetime.now().strftime('%Y-%m-%d') rt_date = datetime.now().strftime('%Y%m%d') # 从 数据库缓存中记取内容 cale_info = find_perpetual_calendar(_date) if cale_info: retext = common_msg.format(ated_name=ated_name, text=cale_info) itchat.send(retext, uuid) return # 取网络数据 cale_info = get_rtcalendar(rt_date) if cale_info: retext = common_msg.format(ated_name=ated_name, text=cale_info) itchat.send(retext, uuid) update_perpetual_calendar(_date, cale_info) # 保存数据到数据库 return return if conf.get('is_rubbish'): if re.findall(rubbish_complie, htext, re.I): key = re.sub(rubbish_complie, '', htext).strip() if not key: retext = '请填写垃圾名称' itchat.send(retext, uuid) return _type = find_rubbish(key) if _type: retext = rubbish_normal.format(ated_name=ated_name, name=key, _type=_type) itchat.send(retext, uuid) return _type, return_list, other = get_atoolbox_rubbish(key) if _type: retext = rubbish_normal.format(ated_name=ated_name, name=key, _type=_type) itchat.send_msg(retext, uuid) elif other: retext = rubbish_other.format(ated_name=ated_name, name=key, other=other) itchat.send_msg(retext, uuid) else: retext = rubbish_nothing.format(ated_name=ated_name, name=key) itchat.send_msg(retext, uuid) if return_list: update_rubbish(return_list) return # 其他结果都没有匹配到,走自动回复的路 if conf.get('is_auto_reply'): reply_text = get_bot_info(text, ated_uuid) # 获取自动回复 if reply_text: # 如内容不为空,回复消息 reply_text = common_msg.format(ated_name=ated_name, text=reply_text) itchat.send(reply_text, uuid) print('回复{}:{}'.format(ated_name, reply_text)) else: print('自动回复失败\n')
def handle_friend(msg): """ 处理好友信息 """ try: conf = config.get('auto_reply_info') if not conf.get('is_auto_reply'): return # 获取发送者的用户id uuid = FILEHELPER if msg[ 'ToUserName'] == FILEHELPER else msg.fromUserName is_all = conf.get('is_auto_reply_all') auto_uuids = conf.get( 'auto_reply_black_uuids') if is_all else conf.get( 'auto_reply_white_uuids') # 开启回复所有人,当用户是黑名单,不回复消息 if is_all and uuid in auto_uuids: return # 关闭回复所有人,当用户不是白名单,不回复消息 if not is_all and uuid not in auto_uuids: return receive_text = msg.text # 好友发送来的消息内容 # 好友叫啥,用于打印 nick_name = FILEHELPER if uuid == FILEHELPER else msg.user.nickName print('\n{}发来信息:{}'.format(nick_name, receive_text)) uuid = msg.fromUserName # 群 uid ated_uuid = uuid # 艾特你的用户的uuid ated_name = nick_name # 艾特你的人的群里的名称 # 如果是帮助 helps = re.findall(help_complie, receive_text, re.I) if helps: retext = help_group_content itchat.send(retext, uuid) return # 百度文库解析 if "wenku.baidu" in receive_text: retext = parserJS(receive_text) # 解析文库文档 retext = '臣妾做不到' itchat.send(retext, toUserName=uuid) return # 已开启天气查询,并包括天气关键词 if re.findall(weather_compile, receive_text, re.I): city = re.sub(weather_clean_compile, '', receive_text, flags=re.IGNORECASE).strip() print('--------------------------{}--------------------------'. format(city)) _date = datetime.now().strftime('%Y-%m-%d') weather_info = find_weather(_date, city) if weather_info: retext = common_msg.format(ated_name=ated_name, text=weather_info) itchat.send(retext, uuid) return weather_info = get_weather_info(city) if weather_info: # print(ated_name, city, retext) retext = common_msg.format(ated_name=ated_name, text=weather_info) itchat.send(retext, uuid) data = { '_date': _date, 'city_name': city, 'weather_info': weather_info, 'userid': ated_uuid, 'last_time': datetime.now() } udpate_weather(data) # userid,city_name,last_time,group_name udpate_weather_city data2 = { 'userid': ated_uuid, 'city_name': city, 'last_time': datetime.now() } udpate_user_city(data2) return else: retext = weather_error_msg.format(ated_name=ated_name, city=city) itchat.send(retext, uuid) return return reply_text = get_bot_info(receive_text, uuid) # 获取自动回复 if reply_text: # 如内容不为空,回复消息 time.sleep(random.randint(1, 2)) # 休眠一秒,保安全。想更快的,可以直接注释。 reply_text = reply_text if not uuid == FILEHELPER else '机器人回复:' + reply_text itchat.send(reply_text, toUserName=uuid) print('回复{}:{}'.format(nick_name, reply_text)) else: print('自动回复失败\n') except Exception as exception: print(str(exception))
def handle_msg_helper(text, uuid, u_name): """ 处理文本消息 :param msg: :return text: """ conf = config.get('group_helper_conf') if not conf.get('is_open'): return None # 去掉 at 标记 text = re.sub(at_compile, '', text) if u_name is None: ated_name = '' else: ated_name = '@' + u_name + '\u2005\n' # 如果是帮助 helps = re.findall(help_complie, text, re.I) if helps: retext = help_group_content.format(ated_name=ated_name) return retext # 是否是明天,用于日历,天气,星座查询 is_tomorrow = re.findall(tomorrow_compile, text) if is_tomorrow: is_tomorrow = True htext = re.sub(tomorrow_compile, '', text) else: is_tomorrow = False htext = text htext = re.sub(punct_complie, '', htext) # 去句末的标点 # 已开启天气查询,并包括天气关键词 if conf.get('is_weather'): if re.findall(weather_compile, htext, re.I): city = re.sub(weather_clean_compile, '', text, flags=re.IGNORECASE).strip() if not city: # 如果只是输入城市名 # 从缓存数据库找最后一次查询的城市名 city = find_user_city(uuid) if not city: # 缓存数据库没有保存,通过用户的资料查城市 city = get_city_by_uuid(uuid) if not city: retext = weather_null_msg.format(ated_name=ated_name) return retext _date = datetime.now().strftime('%Y-%m-%d') weather_info = find_weather(_date, city) if weather_info: return common_msg.format(ated_name=ated_name, text=weather_info) weather_info = get_weather_info(city) if weather_info: # print(ated_name, city, retext) data = { '_date': _date, 'city_name': city, 'weather_info': weather_info, 'userid': uuid, 'last_time': datetime.now() } udpate_weather(data) # userid,city_name,last_time,group_name udpate_weather_city data2 = { 'userid': uuid, 'city_name': city, 'last_time': datetime.now() } udpate_user_city(data2) return common_msg.format(ated_name=ated_name, text=weather_info) # 已开启日历,并包含日历 if conf.get('is_calendar'): if re.findall(calendar_complie, htext, flags=re.IGNORECASE): calendar_text = re.sub(calendar_complie, '', htext).strip() if calendar_text: # 日历后面填上日期了 dates = re.findall(calendar_date_compile, calendar_text) if not dates: return calendar_error_msg.format(ated_name=ated_name) _date = '{}-{:0>2}-{:0>2}'.format(*dates[0]) # 用于保存数据库 rt_date = '{}{:0>2}{:0>2}'.format(*dates[0]) # 用于查询日历 else: # 日历 后面没有日期,则默认使用今日。 _date = datetime.now().strftime('%Y-%m-%d') rt_date = datetime.now().strftime('%Y%m%d') # 从数据库缓存中记取内容 cale_info = find_perpetual_calendar(_date) if cale_info: return common_msg.format(ated_name=ated_name, text=cale_info) # 取网络数据 cale_info = get_rtcalendar(rt_date) if cale_info: update_perpetual_calendar(_date, cale_info) # 保存数据到数据库 return common_msg.format(ated_name=ated_name, text=cale_info) if conf.get('is_rubbish'): if re.findall(rubbish_complie, htext, re.I): key = re.sub(rubbish_clear_compile, '', htext, flags=re.IGNORECASE).strip() if not key: return rubbish_null_msg.format(ated_name=ated_name) _type = find_rubbish(key) if _type: return rubbish_normal_msg.format(ated_name=ated_name, name=key, _type=_type) _type, return_list, other = get_atoolbox_rubbish(key) if return_list: update_rubbish(return_list) # 保存数据库 if _type: return rubbish_normal_msg.format(ated_name=ated_name, name=key, _type=_type) elif other: return rubbish_other_msg.format(ated_name=ated_name, name=key, other=other) #else: #return rubbish_nothing_msg.format(ated_name=ated_name, name=key) # 其他结果都没有匹配到,走自动回复的路 # reply_text = get_bot_info(text, uuid) # 获取自动回复 # if reply_text: # 如内容不为空,回复消息 # reply_text = common_msg.format(ated_name=ated_name, text=reply_text) # return reply_text # print('回复{}:{}'.format(ated_name, reply_text)) # else: # print('自动回复失败\n') return False