def test_send_email(self): ac = config.get('other_alarm_conf', None) # if ac: email_config = ac.get('email_config', None) user = email_config.get('user', None) password = email_config.get('password', None) host = email_config.get('host', None) email_notice = EmailNotice(user=user, password=password, host=host) # yy = email_notice.send('title', 'hello world', receivers='*****@*****.**') # print(yy) girlfriend_infos = config.get('alarm_info').get('girlfriend_infos') for gf in girlfriend_infos[:1]: is_tomorrow = gf.get('is_tomorrow', False) calendar_info = get_calendar_info(gf.get('calendar'), is_tomorrow) weather = get_weather_info(gf.get('city_name'), is_tomorrow) horoscope = get_constellation_info(gf.get("horescope"), is_tomorrow) dictum = get_dictum_info(gf.get('dictum_channel')) diff_time = get_diff_time(gf.get('start_date'), gf.get('start_date_msg')) sweet_words = gf.get('sweet_words') send_msg = '\n'.join(x for x in [ calendar_info, weather, horoscope, dictum, diff_time, sweet_words ] if x) print(send_msg) print('\n' + '-' * 50 + '\n')
def handle_friend(msg): """ 处理好友信息 """ try: # 自己通过手机微信发送给别人的消息(文件传输助手除外)不作处理。 if msg['FromUserName'] == config.get( 'wechat_uuid') and msg['ToUserName'] != FILEHELPER: return 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)) reply_text = get_bot_info(receive_text, uuid) # 获取自动回复 if reply_text: # 如内容不为空,回复消息 time.sleep(random.randint(1, 2)) # 休眠一秒,保安全。想更快的,可以直接注释。 prefix = conf.get('auto_reply_prefix', '') # 前缀 if prefix: reply_text = '{}{}'.format(prefix, reply_text) suffix = conf.get('auto_reply_suffix', '') # 后缀 if suffix: reply_text = '{}{}'.format(reply_text, suffix) itchat.send(reply_text, toUserName=uuid) print('回复{}:{}'.format(nick_name, reply_text)) else: print('自动回复失败\n') except Exception as exception: print(str(exception))
def send_alarm_msg(key): """ 发送定时提醒 """ print('\n启动定时自动提醒...') conf = config.get('alarm_info').get('alarm_dict') gf = conf.get(key) # print(gf) is_tomorrow = gf.get('is_tomorrow', False) calendar_info = get_calendar_info(gf.get('calendar'), is_tomorrow) weather = get_weather_info(gf.get('city_name'), is_tomorrow) horoscope = get_constellation_info(gf.get("horescope"), is_tomorrow) dictum = get_dictum_info(gf.get('dictum_channel')) diff_time = get_diff_time(gf.get('start_date'), gf.get('start_date_msg')) sweet_words = gf.get('sweet_words') send_msg = '\n'.join( x for x in [calendar_info, weather, horoscope, dictum, diff_time, sweet_words] if x) # print('\n' + send_msg + '\n') if not send_msg or not is_online(): return uuid_list = gf.get('uuid_list') for uuid in uuid_list: time.sleep(1) itchat.send(send_msg, toUserName=uuid) print('\n定时内容:\n{}\n发送成功...\n\n'.format(send_msg)) 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)) 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 get_yigeai(text, userid): """ 『一个AI』自动回复 (http://www.yige.ai/) 接口说明:http://docs.yige.ai/Query%E6%8E%A5%E5%8F%A3.html :param text:str, 需要发送的话 :userid:str,机器唯一标识 :return:str """ try: # config.init() info = config.get('auto_reply_info')['yigeai_conf'] token = info['client_token'] if not token: print('一个「AI」token 为空,请求出错') return None session_id = md5_encode(userid if userid else '250') # print('发出的消息:{}'.format(text)) data = {'token': token, 'query': text, 'session_id': session_id} resp = requests.post('http://www.yige.ai/v1/query', data=data) if resp.status_code == 200 and is_json(resp): # print(resp.text) re_data = resp.json() code = re_data['status']['code'] # 错误码返回有时是数字,有点是str。一起做处理 if code and str(code) not in TULING_ERROR_CODE_LIST: return_text = re_data['answer'] return return_text error_text = re_data['status']['error_type'] print('『一个AI』机器人错误信息:{}'.format(error_text)) return None print('『一个AI』机器人获取数据失败') except Exception as e: print(e) print('『一个AI』机器人获取数据失败')
def text_reply(msg): """ 监听用户消息,用于自动回复 """ try: # if not get_yaml().get('is_auto_relay'): # return conf = config.get('auto_relay_info') if not conf.get('is_auto_relay'): return # print(json.dumps(msg, ensure_ascii=False)) # 获取发送者的用户id uuid = FILEHELPER if msg[ 'ToUserName'] == FILEHELPER else msg.fromUserName # 如果用户id是自动回复列表的人员 if uuid in conf.get('auto_reply_uuids'): receive_text = msg.text # 好友发送来的消息内容 # 好友叫啥,用于打印 nickName = FILEHELPER if uuid == FILEHELPER else msg.user.nickName print('\n{}发来信息:{}'.format(nickName, receive_text)) 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(nickName, reply_text)) else: print('自动回复失败\n') except Exception as e: print(str(e))
def test_all_info(self): """ 测试获取提醒的所有信息。 :return: """ girlfriend_infos = config.get('alarm_info').get('girlfriend_infos') for gf in girlfriend_infos: is_tomorrow = gf.get('is_tomorrow', False) calendar_info = get_calendar_info(gf.get('calendar'), is_tomorrow) weather = get_weather_info(gf.get('city_name'), is_tomorrow) horoscope = get_constellation_info(gf.get("horescope"), is_tomorrow) dictum = get_dictum_info(gf.get('dictum_channel')) diff_time = get_diff_time(gf.get('start_date'), gf.get('start_date_msg')) sweet_words = gf.get('sweet_words') air_quality = get_air_quality(gf.get('air_quality_city')) send_msg = '\n'.join(x for x in [ calendar_info, weather, horoscope, air_quality, dictum, diff_time, sweet_words ] if x) print(send_msg) print('\n' + '-' * 50 + '\n')
def send_alarm_msg(): print('\n获取消息...') gf = config.get('alarm_info').get('girlfriend_infos')[0] is_tomorrow = gf.get('is_tomorrow', False) # calendar_info = get_calendar_info(gf.get('calendar'), gf.get('app_token'), is_tomorrow) weather = get_weather_info(gf.get('city_name'), gf.get('app_token'), is_tomorrow) horoscope = get_constellation_info(gf.get("horescope"), is_tomorrow) dictum = get_dictum_info(gf.get('dictum_channel')) diff_time = get_diff_time(gf.get('start_date'), gf.get('start_date_msg')) air_quality = get_air_quality(gf.get('air_quality_city')) sweet_words = gf.get('sweet_words') list_data = [] for x in [weather, air_quality, horoscope, dictum, diff_time, sweet_words]: if x: list_data.append(x) send_msg = ' \n'.join(list_data) # 必须添加两个空格加换行 print('\n' + send_msg + '\n') # pyperclip.copy(send_msg) with open('./result.txt', 'w', encoding='utf-8') as f: f.write(send_msg) form = { 'title': '每日一句', 'desp': send_msg } send_key = os.environ.get('SEND_KEY') # print(send_key) resp = requests.post('https://sctapi.ftqq.com/{}.send'.format(send_key), form) print(resp) if resp.status_code == 200: print('发送成功!')
def get_ruyiai_bot(text, userId): """ 海知智能 文档说明:<http://docs.ruyi.ai/502931> :param text: str 需要发送的话 :param userId: str 用户标识 :return: str 机器人回复 """ try: # config.init() info = config.get('auto_reply_info')['ruyi_conf'] app_key = info['app_key'] if not app_key: print('海知智能 api_key 为空,请求失败') return params = {'q': text, 'user_id': md5_encode(userId), 'app_key': app_key} headers = {'Content-Type': 'application/json'} resp = requests.get(URL, headers=headers, params=params) if resp.status_code == 200: # print(resp.text) content_dict = resp.json() if content_dict['code'] in (0, 200): outputs = content_dict['result']['intents'][0]['outputs'] reply_text = outputs[0]['property']['text'] # print(reply_text) return reply_text else: print('海知智能 获取数据失败:{}'.format(content_dict['msg'])) return print('海知智能 获取数据失败') return None except Exception as exception: print(str(exception))
def is_online(auto_login=False): """ 判断是否还在线。 :param auto_login: bool,当为 Ture 则自动重连(默认为 False)。 :return: bool,当返回为 True 时,在线;False 已断开连接。 """ def _online(): """ 通过获取好友信息,判断用户是否还在线。 :return: bool,当返回为 True 时,在线;False 已断开连接。 """ try: if itchat.search_friends(): return True except IndexError: return False return True if _online(): return True # 如果在线,则直接返回 True if not auto_login: # 不自动登录,则直接返回 False print('微信已离线..') return False hotReload = not config.get('is_forced_switch', False) # 切换微信号,重新扫码。 loginCallback = init_data exitCallback = exit_msg try: for _ in range(2): # 尝试登录 2 次。 if platform.system() in ('Windows', 'Darwin'): itchat.auto_login(hotReload=hotReload, loginCallback=loginCallback, exitCallback=exitCallback) itchat.run(blockThread=True) else: # 命令行显示登录二维码。 itchat.auto_login(enableCmdQR=2, hotReload=hotReload, loginCallback=loginCallback, exitCallback=exitCallback) itchat.run(blockThread=True) if _online(): print('登录成功') return True except Exception as exception: # 登录失败的错误处理。 sex = str(exception) if sex == "'User'": print('此微信号不能登录网页版微信,不能运行此项目。没有任何其它解决办法!可以换个号再试试。') else: print(sex) delete_cache() # 清理缓存数据 print('登录失败。') return False
def init_data(): """ 初始化微信所需数据 """ set_system_notice('登录成功') itchat.get_friends(update=True) # 更新好友数据。 itchat.get_chatrooms(update=True) # 更新群聊数据。 init_wechat_config() # 初始化所有配置内容 # 提醒内容不为空时,启动定时任务 alarm_dict = config.get('alarm_info').get('alarm_dict') if alarm_dict: init_alarm(alarm_dict) # 初始化定时任务
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) retext = handle_msg_helper(text, ated_uuid, ated_name) if retext: itchat.send(retext, toUserName=uuid) 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 get_express_info(express_code, shipper_code='', shipper_name=''): """ 查询快递物流信息 :param express_code: str,快递单号 :param shipper_code: str,快递公司简称代号 :param shipper_name: str,快递公司名称(用于结果显示) :return: """ express_config_info = config.get('group_helper_conf')['express_info'] app_id = express_config_info['app_id'] app_key = express_config_info['app_key'] if not shipper_code or not shipper_name: company_info = get_company_info(express_code, app_id, app_key) # print(company_info) if not company_info: return shipper_code = company_info['shipper_code'] shipper_name = company_info['shipper_name'] trace_data = get_logistic_info(express_code, shipper_code, app_id, app_key) print(trace_data) if not trace_data: return state_code = trace_data['State'] express_state = EXPRESS_STATE_DICT.get(state_code, '未知状态') info = [] express_base_info = '物流公司:{shipper_name}\n物流单号:{express_code}\n物流状态:{express_state}'.format( shipper_name=shipper_name, express_code=express_code, express_state=express_state) info.append(express_base_info) info.append('------物流详情------') traces = trace_data['Traces'] for i, item in enumerate(traces[::-1]): bb = '{index}. {time} {station}'.format( index=str(i + 1), time=item['AcceptTime'], station=item['AcceptStation']) # print(bb) info.append(bb) return_info = { 'express_code': express_code, 'shipper_code': shipper_code, 'shipper_name': shipper_name, 'info': '\n'.join(info), 'state': True if state_code == '3' else False } return return_info
def is_online(auto_login=False): """ 判断是否还在线。 :param auto_login: bool,当为 Ture 则自动重连(默认为 False)。 :return: bool,当返回为 True 时,在线;False 已断开连接。 """ # print('i am here..') def _online(): """ 通过获取好友信息,判断用户是否还在线。 :return: bool,当返回为 True 时,在线;False 已断开连接。 """ try: if itchat.search_friends(): return True except IndexError: return False return True if _online(): return True # 如果在线,则直接返回 True if not auto_login: # 不自动登录,则直接返回 False print('微信已离线..') return False hotReload = not config.get('is_forced_switch', False) # 切换微信号,重新扫码。 loginCallback = init_data exitCallback = exit_msg for _ in range(2): # 尝试登录 2 次。 if platform.system() in ('Windows', 'Darwin'): itchat.auto_login(hotReload=hotReload, loginCallback=loginCallback, exitCallback=exitCallback) itchat.run(blockThread=True) else: # 命令行显示登录二维码。 itchat.auto_login(enableCmdQR=2, hotReload=hotReload, loginCallback=loginCallback, exitCallback=exitCallback) itchat.run(blockThread=True) if _online(): print('登录成功') return True print('登录失败。') return False
def get_tuling123(text, userId): """ 接口地址:(https://www.kancloud.cn/turing/www-tuling123-com/718227) 获取图灵机器人对话 :param text: 发送的话 :param userId: 用户唯一标识(最好用微信好友uuid) :return: 对白 """ try: # config.init() info = config.get('auto_reply_info')['turing_conf'] apiKey = info['apiKey'] if not apiKey: print('图灵机器人 apikey 为空,请求出错') return None userId = md5_encode(userId if userId else '250') content = { 'perception': { 'inputText': { 'text': text } }, 'userInfo': { 'apiKey': apiKey, 'userId': userId } } # print('发出消息:{}'.format(text)) resp = requests.post(URL, json=content) if resp.status_code == 200 and is_json(resp): # print(resp.text) re_data = resp.json() if re_data['intent']['code'] not in TULING_ERROR_CODE_LIST: return_text = re_data['results'][0]['values']['text'] return return_text error_text = re_data['results'][0]['values']['text'] print('图灵机器人错误信息:{}'.format(error_text)) return None print('图灵机器人获取数据失败') except Exception as exception: print(str(exception)) print('图灵机器人获取数据失败')
def get_tianapi_robot(text, userid): """ 从天行机器人获取自动回复,接口地址:<https://www.tianapi.com/apiview/47> :param text: 发出的消息 :param userid: 收到的内容 :return: """ try: # config.init() info = config.get('auto_reply_info')['txapi_conf'] app_key = info['app_key'] if not app_key: print('天行机器人 app_key 为空,请求失败') return reply_name = info.get('reply_name', '') bot_name = info.get('bot_name', '') params = { 'key': app_key, 'question': text, 'userid': md5_encode(userid), 'limit': 10, # 机器人分析系数,取值1-10 'mode': 1, # 图文返回数量,取值1-10 'datatype': '0', # 返回类型,文本0[默认]、语音1 } resp = requests.get('https://api.tianapi.com/txapi/robot/', params=params) if resp.status_code == 200: # print(resp.text) content_dict = resp.json() if content_dict['code'] == 200: if content_dict['datatype'] == 'text': data_dict = content_dict['newslist'] reply_text = data_dict[0]['reply'] reply_text.replace('{robotname}', bot_name).replace( '{appellation}', reply_name) return reply_text else: return '我不太懂你在说什么' else: print('天行机器人获取数据失败:{}'.format(content_dict['msg'])) print('获取数据失败') return None except Exception as exception: print(str(exception))
def get_nlp_textchat(text, userId): """ 智能闲聊(腾讯)<https://ai.qq.com/product/nlpchat.shtml> 接口文档:<https://ai.qq.com/doc/nlpchat.shtml> :param text: 请求的话 :param userId: 用户标识 :return: str """ try: # config.init() info = config.get('auto_reply_info')['qqnlpchat_conf'] app_id = info['app_id'] app_key = info['app_key'] if not app_id or not app_key: print('app_id 或 app_key 为空,请求失败') return # 产生随机字符串 nonce_str = ''.join( random.sample(string.ascii_letters + string.digits, random.randint(10, 16))) time_stamp = int(time.time()) # 时间戳 params = { 'app_id': app_id, # 应用标识 'time_stamp': time_stamp, # 请求时间戳(秒级) 'nonce_str': nonce_str, # 随机字符串 'session': md5_encode(userId), # 会话标识 'question': text # 用户输入的聊天内容 } # 签名信息 params['sign'] = getReqSign(params, app_key) resp = requests.get(URL, params=params, proxies=Proxies) if resp.status_code == 200: # print(resp.text) content_dict = resp.json() if content_dict['ret'] == 0: data_dict = content_dict['data'] return data_dict['answer'] print('智能闲聊 获取数据失败:{}'.format(content_dict['msg'])) return None except Exception as exception: print(str(exception))
def get_bot_info(message, userId=''): """ 获取自动回复的话。 # 优先获取图灵机器人API的回复,但失效时,会使用青云客智能聊天机器人API(过时) :param message:str, 发送的话 :return:str, 回复的话 """ channel = config.get('bot_channel', 3) # channel = get_yaml().get('', 3) source = BOT_NAME_DICT.get(channel, 'qingyunke') if source: addon = importlib.import_module('everyday_wechat.control.bot.' + source, __package__) reply_msg = addon.get_auto_reply(message, userId) return reply_msg # reply_msg = get_tuling123(message) # if not reply_msg: # # reply_msg = get_qingyunke(message) # reply_msg = get_yigeai(message) return None
def get_ownthink_robot(text, userid): """ 思知机器人,接口地址:<https://www.ownthink.com/> https://api.ownthink.com/bot?appid=xiaosi&userid=user&spoken=姚明多高啊? :param text: 发出的消息 :param userid: 收到的内容 :return: """ try: # config.init() info = config.get('auto_reply_info')['txapi_conf'] app_key = info.get('app_key', '') if not re.findall(r'^[0-9a-z]{20,}$', app_key): # 验证 app_key 是否有效 app_key = '' params = { 'appid': app_key, 'userid': md5_encode(userid), 'spoken': text } url = 'https://api.ownthink.com/bot' resp = requests.get(url, params=params, proxies=Proxies) if resp.status_code == 200: # print(resp.text) content_dict = resp.json() if content_dict['message'] == 'success': data = content_dict['data'] if data['type'] == 5000: reply_text = data['info']['text'] return reply_text else: print('返回的数据不是文本数据!') else: print('思知机器人获取数据失败:{}'.format(content_dict['msg'])) print('获取数据失败') return None except Exception as exception: print(str(exception))
def get_bot_info(message, userId=''): """ 跟机器人互动 # 优先获取图灵机器人API的回复,但失效时,会使用青云客智能聊天机器人API(过时) :param message:str, 发送的话 :param userId: str, 好友的uid,作为请求的唯一标识。 :return:str, 机器人回复的话。 """ channel = config.get('auto_reply_info').get('bot_channel', 7) source = BOT_NAME_DICT.get(channel, 'ownthink_robot') # print(source) if source: addon = importlib.import_module('everyday_wechat.control.bot.' + source, __package__) reply_msg = addon.get_auto_reply(message, userId) return reply_msg # reply_msg = get_tuling123(message) # if not reply_msg: # # reply_msg = get_qingyunke(message) # reply_msg = get_yigeai(message) return None
def send_xiaohua_message(key): """ 发送定时消息 """ print('\n启动定时消息提醒...') conf = config.get('alarm_info').get('alarm_dict') gf = conf.get(key) dictum = get_dictum_info(5) if not dictum or not is_online(): return b_insert = find_qinghua(dictum) while (b_insert): dictum = get_dictum_info(5) b_insert = find_qinghua(dictum) insert_qinghua(dictum) uuid_list = gf.get('uuid_list') for uuid in uuid_list: time.sleep(1) itchat.send(dictum, toUserName=uuid) print('\n定时内容:\n{}\n发送成功...\n\n'.format(dictum)) print('自动提醒消息发送完成...\n')
def send_image(key): print('\n启动定时消息提醒...') result = find_imageurl() file_path = download_image(result["image"])[0] if not file_path or not is_online(): return conf = config.get('alarm_info').get('alarm_dict') gf = conf.get(key) uuid_list = gf.get('uuid_list') for uuid in uuid_list: time.sleep(1) itchat.send(result["title"], toUserName=uuid) itchat.send_image(fileDir=file_path, toUserName=uuid) result["status"] = 1 collection2.update({"_id": result["_id"]}, result) os.remove(file_path) print('\n定时内容:\n{}\n发送成功...\n\n'.format(result["image"])) 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_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 log_all_config(): """ 用于打印设置日志 :return: """ print('=' * 80) channel = config.get('auto_reply_info').get('bot_channel', 7) source = BOT_NAME_DICT.get(channel, 'ownthink_robot') addon = import_module('everyday_wechat.control.bot.' + source, __package__) bot_name = addon.BOT_NAME print('自动回复机器人渠道:{}'.format(bot_name)) # start ----------------------------------- 微信好友自动回复的功能日志 ----------------------------------- start reply = config.get('auto_reply_info') if not reply.get('is_auto_reply'): print('未开启微信好友自动回复。') else: if reply.get('is_auto_reply_all'): auto_uuids = reply.get('auto_reply_black_uuids') nicknames = [] for auid in auto_uuids: if auid == 'filehelper': nicknames.append(auid) else: friends = itchat.search_friends(userName=auid) nickname = friends.nickName nicknames.append(nickname) nns = ','.join(nicknames) print('开启对全部微信好友全部回复,除了:{}'.format(nns)) else: auto_uuids = reply.get('auto_reply_white_uuids') nicknames = [] for auid in auto_uuids: if auid == 'filehelper': nicknames.append(auid) else: friends = itchat.search_friends(userName=auid) nickname = friends.nickName nicknames.append(nickname) nns = ','.join(nicknames) print('对微信好友 {},进行自动回复'.format(nns)) print('=' * 80) # start ----------------------------------- 群功能日志说明 ----------------------------------- start helper = config.get('group_helper_conf') if not helper.get('is_open'): print('未开启群助手功能。') else: if helper.get('is_all'): auto_uuids = helper.get('group_black_uuids') nicknames = [] for auid in auto_uuids: chatrooms = itchat.search_chatrooms(userName=auid) nickname = chatrooms['NickName'] # 群聊名称 nicknames.append(nickname) nns = ','.join(nicknames) print('已开启对全部微信群的监听,除了群:{}。'.format(nns)) else: auto_uuids = helper.get('group_white_uuids') nicknames = [] for auid in auto_uuids: chatroom = itchat.search_chatrooms(userName=auid) nickname = chatroom['NickName'] # 群聊名称 nicknames.append(nickname) nns = ','.join(nicknames) print('已对微信群:{},开启了群助手功能。'.format(nns)) if helper.get('is_at'): print('只有群里用户@机器人,才会触发群助手功能。') if helper.get('is_auto_reply'): print('已开启对微信群内用户的自动回复。') if helper.get('is_weather'): print('已开启天气查询功能,具体使用方法请输入:“help” 查看。') if helper.get('is_calendar'): print('已开启日志查询功能,具体使用方法请输入:“help” 查看。') if helper.get('is_rubbish'): print('已开启垃圾分类查询功能,具体使用方法请输入:“help” 查看。') if helper.get('is_moviebox'): print('已开启票房查询功能,具体使用方法请输入:“help” 查看。') print('=' * 80) # start ----------------------------------- 提醒功能的日志说明 ----------------------------------- start alarm = config.get('alarm_info') if not alarm.get('is_alarm'): print('未开启每日提醒功能。') else: print('已开启定时发送提醒功能。') alarm_dict = alarm.get('alarm_dict') for value in alarm_dict.values(): nickname_list = value.get('nickname_list') nns = ','.join(nickname_list) # temp_dict = {'hour': hour, 'minute': minute, 'uuid_list': uuid_list, 'nickname_list': nickname_list} hour = value.get('hour') minute = value.get('minute') print('定时:{hour}:{minute},给:{nicknames},发送提醒内容。'.format(hour=hour, minute=minute, nicknames=nns)) print('=' * 80)
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
""" import pymongo from everyday_wechat.utils import config from functools import wraps from datetime import datetime __all__ = [ 'is_open_db', 'udpate_weather', 'udpate_user_city', 'find_user_city', 'find_weather', 'update_perpetual_calendar', 'find_perpetual_calendar', 'find_rubbish', 'update_rubbish', 'find_movie_box', 'update_movie_box', 'find_express', 'update_express', 'find_air_quality', 'udpate_air_quality' ] cache_valid_time = 4 * 60 * 60 # 天气缓存有效时间 db_config = config.get('db_config') if db_config and db_config.get('is_open_db') and db_config.get('mongodb_conf'): is_open_db = db_config.get('is_open_db') mongodb_conf = db_config.get('mongodb_conf') try: myclient = pymongo.MongoClient(host=mongodb_conf.get('host'), port=mongodb_conf.get('port'), serverSelectionTimeoutMS=10) myclient.server_info() # 查看数据库信息,在这里用于是否连接数据的测试 wechat_helper_db = myclient["wechat_helper"] weather_db = wechat_helper_db['weather'] user_city_db = wechat_helper_db['user_city'] perpetual_calendar_db = wechat_helper_db['perpetual_calendar'] rubbish_db = wechat_helper_db['rubbish_assort'] movie_box_db = wechat_helper_db['movie_box'] # 电影票房
""" import pymongo from everyday_wechat.utils import config from functools import wraps from datetime import datetime __all__ = [ 'is_open_db', 'udpate_weather', 'udpate_user_city', 'find_user_city', 'find_weather', 'update_perpetual_calendar', 'find_perpetual_calendar', 'find_rubbish', 'update_rubbish' ] cache_valid_time = 4 * 60 * 60 # 缓存有效时间 is_open_db = config.get('db_config')['is_open_db'] if is_open_db: mongodb_conf = config.get('db_config')['mongodb_conf'] try: myclient = pymongo.MongoClient( host=mongodb_conf['host'], port=mongodb_conf['port'], serverSelectionTimeoutMS=10) myclient.server_info() # 查看数据库信息,在这里用于是否连接数据的测试 wechat_helper_db = myclient["wechat_helper"] weather_db = wechat_helper_db['weather'] user_city_db = wechat_helper_db['user_city'] perpetual_calendar_db = wechat_helper_db['perpetual_calendar'] rubbish_db = wechat_helper_db['rubbish_assort']