def init_alarm(): """ 初始化定时提醒 """ alarm_info = get_yaml().get('alarm_info', None) if not alarm_info: return is_alarm = alarm_info.get('is_alarm', False) if not is_alarm: return alarm_timed = alarm_info.get('alarm_timed', None) if not alarm_timed: return hour, minute = [int(x) for x in alarm_timed.split(':')] # 检查数据的有效性 for info in get_yaml().get('girlfriend_infos'): if not info: break # 解决无数据时会出现的 bug。 wechat_name = info.get('wechat_name') if (wechat_name and wechat_name.lower() not in FILEHELPER_MARK and not get_friend(wechat_name)): print('定时任务中的好友名称『{}』有误。'.format(wechat_name)) # 更新信息 group_name = info.get('group_name') if group_name and not get_group(group_name): print('定时任务中的群聊名称『{}』有误。' '(注意:必须要把需要的群聊保存到通讯录)'.format(group_name)) # 定时任务 scheduler = BlockingScheduler() # 每天9:30左右给女朋友发送每日一句 scheduler.add_job(send_alarm_msg, 'cron', hour=hour, minute=minute, misfire_grace_time=15 * 60) # 每隔 30 秒发送一条数据用于测试。 # scheduler.add_job(send_alarm_msg, 'interval', seconds=30) print('已开启定时发送提醒功能...') scheduler.start()
def send_alarm_msg(): """ 发送定时提醒 """ print('\n启动定时自动提醒...') conf = get_yaml() for gf in conf.get('girlfriend_infos'): dictum = get_dictum_info(gf.get('dictum_channel')) weather = get_weather_info(gf.get('city_name')) diff_time = get_diff_time(gf.get('start_date')) sweet_words = gf.get('sweet_words') send_msg = '\n'.join(x for x in [weather, dictum, diff_time, sweet_words] if x) print(send_msg) if not send_msg or not is_online(): continue # 给微信好友发信息 wechat_name = gf.get('wechat_name') if wechat_name: if wechat_name.lower() in FILEHELPER_MARK: itchat.send(send_msg, toUserName=FILEHELPER) print('定时给『{}』发送的内容是:\n{}\n发送成功...\n\n'.format(wechat_name, send_msg)) else: wechat_users = itchat.search_friends(name=wechat_name) if not wechat_users: continue wechat_users[0].send(send_msg) print('定时给『{}』发送的内容是:\n{}\n发送成功...\n\n'.format(wechat_name, send_msg)) # 给群聊里发信息 group_name = gf.get('group_name') if group_name: group = get_group(group_name) if group: group.send(send_msg) print('定时给群聊『{}』发送的内容是:\n{}\n发送成功...\n\n'.format(group_name, send_msg)) print('自动提醒消息发送完成...\n')
def run(): """ 主运行入口 """ if not is_online(auto_login=True): return conf = get_yaml() if conf.get('is_auto_relay'): print('已开启图灵自动回复...') init_alarm()
def run(): """ 主运行入口 """ conf = get_yaml() if not conf: # 如果 conf,表示配置文件出错。 print('程序中止...') return # 判断是否登录,如果没有登录则自动登录,返回 False 表示登录失败 if not is_online(auto_login=True): return if conf.get('is_auto_relay'): print('已开启图灵自动回复...') init_alarm() # 初始化定时任务
def init_alarm(): """ 初始化定时提醒 """ # 先勾搭一遍 # hook_up_girls(False) alarm_info = get_yaml().get('alarm_info', None) if not alarm_info: return is_alarm = alarm_info.get('is_alarm', False) if not is_alarm: return alarm_timed = alarm_info.get('alarm_timed', None) if not alarm_timed: return hour, minute = [int(x) for x in alarm_timed.split(':')] # 检查数据的有效性 for info in get_yaml().get('girlfriend_infos'): if not info: break # 解决无数据时会出现的 bug。 wechat_name = info.get('wechat_name', None) if (wechat_name and wechat_name.lower() not in FILEHELPER_MARK and not itchat.search_friends(name=wechat_name)): print_logging('定时任务中的好友名称『{}』有误。'.format(wechat_name)) group_name = info.get('group_name') if group_name and not itchat.search_chatrooms(name=group_name): print_logging('定时任务中的群聊名称『{}』有误。' '(注意:必须要把需要的群聊保存到通讯录)'.format(group_name)) # 定时任务 scheduler = BlockingScheduler() # 每天9:30左右给爱妃们发送每日一句 scheduler.add_job(send_alarm_msg, 'cron', hour=hour, minute=minute, misfire_grace_time=15 * 60) # 每隔 3600 秒给爱妃们发送一条土味情话 scheduler.add_job(hook_up_girls, 'interval', seconds=3600) print_logging('已开启定时发送提醒功能...') scheduler.start()
def init_reply(): """ 初始化自动回复相关数据。 :return: """ conf = get_yaml() for name in conf.get('auto_reply_names', None): friends = itchat.search_friends(name=name) if not friends: # 如果用户列表为空,表示用户昵称填写有误。 print('昵称『{}』有误。'.format(name)) break name_uuid = friends[0].get('UserName') # 取第一个用户的 uuid。 if name_uuid not in reply_name_uuid_list: reply_name_uuid_list.append(name_uuid)
def get_dictum_info(): """ 获取格言信息 :return: """ conf = get_yaml() channel = conf.get('dictum_channel') source = DICTUM_NAME_DICT.get(channel, '') if source: addon = importlib.import_module('onewords.' + source, __package__) dictum = addon.get_one_words() # print(dictum) return dictum return None
def init_alarm(): """ 初始化定时提醒 """ alarm_info = get_yaml().get('alarm_info', None) if not alarm_info: return is_alarm = alarm_info.get('is_alarm', False) if not is_alarm: return alarm_timed = alarm_info.get('alarm_timed', None) if not alarm_timed: return hour, minute = [int(x) for x in alarm_timed.split(':')] # 检查数据的有效性 for info in get_yaml().get('girlfriend_infos'): wechat_name = info.get('wechat_name') if (wechat_name and wechat_name not in ['filehelper', '文件传输助手'] and not itchat.search_friends(name=wechat_name)): print('定时任务中的好友名称『{}』有误。'.format(wechat_name)) group_name = info.get('group_name') if group_name and not itchat.search_chatrooms(name=group_name): print('定时任务中的群聊名称『{}』有误。(注意:必须要把需要的群聊保存到通讯录)'.format(group_name)) # 定时任务 scheduler = BlockingScheduler() # 每天9:30左右给女朋友发送每日一句 scheduler.add_job(send_alarm_msg, 'cron', hour=hour, minute=minute, misfire_grace_time=15 * 60) # # 每隔 2 分钟发送一条数据用于测试。 # scheduler.add_job(send_alarm_msg, 'interval', seconds=30) print('已开启定时发送提醒功能...') scheduler.start()
def init_wechat(): """ 初始化微信所需数据 """ conf = get_yaml() itchat.get_friends(update=True) # 更新好友数据。 itchat.get_chatrooms(update=True) # 更新群聊数据。 for name in conf.get('auto_reply_names'): if name.lower() in FILEHELPER_MARK: # 判断是否文件传输助手 if FILEHELPER not in reply_userNames: reply_userNames.append(FILEHELPER) continue friend = get_friend(name) if friend: reply_userNames.append(friend['UserName']) else: print('自动回复中的好友昵称『{}』有误。'.format(name))
def get_alarm_msg(): conf = get_yaml() for gf in conf['girlfriend_infos']: dictum = get_dictum_info() weather = get_weather_info(gf['city_name']) diff_time = get_diff_time(gf['start_date']) sweet_words = gf['sweet_words'] send_msg = '\n'.join( x for x in [dictum, weather, diff_time, sweet_words] if x) # print(send_msg) if send_msg and is_online(): authors = itchat.search_friends(nickName=gf['wechat_name']) if authors: authors[0].send(send_msg) print('\n定时给『{}』发送的内容是:\n{}\n发送成功...\n'.format( gf['wechat_name'], send_msg))
def get_tuling123(text, userId): """ 接口地址:(https://www.kancloud.cn/turing/www-tuling123-com/718227) 获取图灵机器人对话 :param text: 发送的话 :param userId: 用户唯一标识(最好用微信好友uuid) :return: 对白 """ info = get_yaml()['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 } } try: # 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 else: error_text = re_data['results'][0]['values']['text'] print('图灵机器人错误信息:{}'.format(error_text)) print('图灵机器人获取数据失败') return None except Exception as e: print(e) print('图灵机器人获取数据失败') return None return None
def init_wechat(): """ 初始化微信所需数据 """ conf = get_yaml() itchat.get_chatrooms(update=True) # 更新群信息。 for name in conf.get('auto_reply_names'): if name.lower() in FILEHELPER_MARK: # 判断是否文件传输助手 if FILEHELPER not in reply_user_name_uuid_list: reply_user_name_uuid_list.append(FILEHELPER) friends = itchat.search_friends(name=name) if not friends: # 如果用户列表为空,表示用户昵称填写有误。 print_logging('自动回复中的昵称『{}』有误。'.format(name)) continue else: name_uuid = friends[0].get('UserName') # 取第一个用户的 uuid。 if name_uuid not in reply_user_name_uuid_list: reply_user_name_uuid_list.append(name_uuid)
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 get_yaml().get('is_forced_switch', False) # 切换微信号,重新扫码。 loginCallback = init_wechat exitCallback = exit_msg for _ in range(2): # 尝试登录 2 次。 if os.environ.get('MODE') == 'server': # 命令行显示登录二维码。 itchat.auto_login(enableCmdQR=2, hotReload=hotReload, loginCallback=loginCallback, exitCallback=exitCallback) itchat.run(blockThread=False) else: itchat.auto_login(hotReload=hotReload, loginCallback=loginCallback, exitCallback=exitCallback) itchat.run(blockThread=False) if _online(): print('登录成功') return True print('登录失败。') return False
def run(): if not is_online(auto_login=True): print('登录失败') return conf = get_yaml() if conf.get('is_auto_relay', False): def _itchatRun(): itchat.run() init_reply() thread = threading.Thread(target=_itchatRun, name='LoopThread') thread.start() print('已开启图灵自动回复...') init_alarm() thread.join() else: init_alarm()
def get_alarm_msg(): """ 定时提醒内容 """ conf = get_yaml() for gf in conf.get('girlfriend_infos'): dictum = get_dictum_info(gf.get('dictum_channel')) weather = get_weather_info(gf.get('city_name')) diff_time = get_diff_time(gf.get('start_date')) sweet_words = gf.get('sweet_words') send_msg = '\n'.join(x for x in [dictum, weather, diff_time, sweet_words] if x) print(send_msg) if send_msg and is_online(): wechat_name = gf.get('wechat_name') authors = itchat.search_friends(nickName=wechat_name) if authors: authors[0].send(send_msg) print('\n定时给『{}』发送的内容是:\n{}\n发送成功...\n'.format(wechat_name, send_msg)) else: print('定时提醒发送失败,微信名 {} 失效。'.format(wechat_name))
def get_bot_info(message, userId=''): """ 获取自动回复的话。 # 优先获取图灵机器人API的回复,但失效时,会使用青云客智能聊天机器人API(过时) :param message:str, 发送的话 :return:str, 回复的话 """ channel = get_yaml().get('bot_channel', 3) source = BOT_NAME_DICT.get(channel, 'qingyunke') if source: addon = importlib.import_module('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 init_alarm(): """ 初始化定时提醒 """ alarm_info = get_yaml().get('alarm_info', None) if not alarm_info: return is_alarm = alarm_info.get('is_alarm', False) if not is_alarm: return alarm_timed = alarm_info.get('alarm_timed', None) if not alarm_timed: return hour, minute = [int(x) for x in alarm_timed.split(':')] # 定时任务 scheduler = BlockingScheduler() # 每天9:30左右给女朋友发送每日一句 scheduler.add_job(get_alarm_msg, 'cron', hour=hour, minute=minute, misfire_grace_time=GRACE_PERIOD) # 每隔 2 分钟发送一条数据用于测试。 # scheduler.add_job(get_alarm_msg, 'interval', seconds=120) print('已开启定时发送提醒功能...') scheduler.start()
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 """ conf = get_yaml() token = conf['yigeai_conf']['client_token'] if not token: print('错误 .一个「AI」token 为空') return None session_id = md5_encode(userid) try: # print('发出消息:{}'.format(text)) resp = requests.post('http://www.yige.ai/v1/query', data={ 'token': token, 'query': text, 'session_id': session_id }) 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 else: error_text = re_data['status']['error_type'] print('『一个AI』机器人错误信息:{}'.format(error_text)) print('『一个AI』机器人获取数据失败') return None except Exception as e: print(e) print('『一个AI』机器人获取数据失败') return None return None
def hook_up_girls(is_random=True): """ 自动勾搭完成开始 """ print_logging('\n自动勾搭开始...') conf = get_yaml() for gf in conf.get('hookup_girls'): # dictum = get_dictum_info(gf.get('dictum_channel')) if is_random: dictum = get_dictum_info(gf.get('dictum_channel')) else: dictum = get_dictum_info(random.randint(2, 5)) sweet_words = gf.get('sweet_words') send_msg = '\n'.join(x for x in [dictum, sweet_words] if x) # print_logging(send_msg) if not send_msg or not is_online(): continue # 给微信好友发信息 wechat_name = gf.get('wechat_name') if wechat_name: if wechat_name.lower() in FILEHELPER_MARK: itchat.send(send_msg, toUserName=FILEHELPER) print_logging('定时给『{}』发送的内容是:\n{}\n发送成功...\n\n'.format( wechat_name, send_msg)) else: wechat_users = itchat.search_friends(name=wechat_name) if not wechat_users: continue wechat_users[0].send(send_msg) print_logging('定时给『{}』发送的内容是:\n{}\n发送成功...\n\n'.format( wechat_name, send_msg)) # 给群聊里发信息 group_name = gf.get('group_name') if not group_name: continue groups = itchat.search_chatrooms(name=group_name) if not groups: continue groups[0].send(send_msg) print_logging('定时给群聊『{}』发送的内容是:\n{}\n发送成功...\n\n'.format( group_name, send_msg)) print_logging('自动勾搭完成.\n')
def get_tuling123(text): """ 获取 :param text: :return: """ info = get_yaml()['turing_conf'] apiKey = info['apiKey'] userId = info['userId'] if not apiKey or not userId: return None content = { 'perception': { 'inputText': { 'text': text } }, 'userInfo': { 'apiKey': apiKey, 'userId': userId } } try: # 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 else: error_text = re_data['results'][0]['values']['text'] print('图灵机器人错误信息:{}'.format(error_text)) print('图灵机器人发送失败') return None except Exception as e: print(e) return None return None
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 return False # 切换微信号,重新扫码。 is_forced_switch = get_yaml().get('is_forced_switch', False) for _ in range(2): # 登陆,尝试 2 次。 # 如果需要切换微信,删除 hotReload=True if os.environ.get('MODE') == 'server': # 命令行显示登录二维码。 itchat.auto_login(enableCmdQR=2, hotReload=(not is_forced_switch)) else: itchat.auto_login(hotReload=(not is_forced_switch)) if _online(): print('登录成功') return True print('登录失败。') return False
def get_yigeai(text): """ 『一个AI』自动回复 (http://www.yige.ai/) :param text: 需要发送的话 :return:str """ conf = get_yaml() token = conf['yigeai_conf']['client_token'] if not token: # print('一个AI token 为空') return None # 一个字符串token,最多36个字符,用来识别客户端和服务端每个会话参数 session_id = md5_encode(''.join(conf.get('auto_reply_names'))) try: # print('发出消息:{}'.format(text)) resp = requests.post('http://www.yige.ai/v1/query', data={ 'token': token, 'query': text, 'session_id': session_id }) if resp.status_code == 200 and is_json(resp): # print(resp.text) re_data = resp.json() if re_data['status']['code'] not in TULING_ERROR_CODE_LIST: return_text = re_data['answer'] return return_text else: error_text = re_data['status']['error_msg'] print('『一个AI』机器人错误信息:{}'.format(error_text)) print('『一个AI』机器人发送失败') return None except Exception as e: print(e) return None return None