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 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 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_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 init_wechat(): """ 初始化微信所需数据 """ set_system_notice('登录成功') # conf = get_yaml() itchat.get_friends(update=True) # 更新好友数据。 itchat.get_chatrooms(update=True) # 更新群聊数据。 # 从config copy ,用于保存新的接口内容。 myset = config.copy() # start---------------------------处理自动回复好友---------------------------start relay = myset.get('auto_relay_info') if relay.get('is_auto_relay'): auto_reply_uuids = [] for name in relay.get('auto_reply_names'): if name.lower() in FILEHELPER_MARK: # 判断是否文件传输助手 if FILEHELPER not in reply_userNames: auto_reply_uuids.append(FILEHELPER) continue friend = get_friend(name) if friend: auto_reply_uuids.append(friend['UserName']) else: print('自动回复中的好友昵称『{}』有误。'.format(name)) relay['auto_reply_uuids'] = set(auto_reply_uuids) print('已开启图灵自动回复...') # end---------------------------处理自动回复好友---------------------------end alarm = myset.get('alarm_info') alarm_dict = {} if alarm.get('is_alarm'): for gi in alarm.get('girlfriend_infos'): ats = gi.get('alarm_timed') if not ats: continue uuid_list = [] # start---------------------------处理好友---------------------------start friends = gi.get('wechat_name') if isinstance(friends, str): friends = [friends] if isinstance(friends, list): for name in friends: if name.lower() in FILEHELPER_MARK: # 判断是否文件传输助手 uuid_list.append(FILEHELPER) continue name_info = get_friend(name) if not name_info: print('用户昵称{}无效'.format(name)) else: uuid_list.append(name_info['UserName']) # end---------------------------处理好友---------------------------end # start---------------------------群组处理---------------------------start group_names = gi.get('group_name') if isinstance(group_names, str): group_names = [group_names] if isinstance(group_names, list): for name in group_names: name_info = get_group(name) if not name_info: print('定时任务中的群聊名称『{}』有误。' '(注意:必须要把需要的群聊保存到通讯录)'.format(name)) else: uuid_list.append(name_info['UserName']) # end---------------------------群组处理---------------------------end # start---------------------------定时处理---------------------------start if isinstance(ats, str): ats = [ats] if isinstance(ats, list): for at in ats: times = timeCompile.findall(at) if not times: print('时间{}格式出错'.format(at)) continue hour, minute = int(times[0][0]), int(times[0][1]) temp_dict = { 'hour': hour, 'minute': minute, 'uuid_list': uuid_list } temp_dict.update(gi) alarm_dict[md5_encode(str(temp_dict))] = temp_dict # end---------------------------定时处理---------------------------end alarm['alarm_dict'] = alarm_dict # 将解析的数据保存于config中。 config.update(myset) # print(json.dumps(alarm_dict, ensure_ascii=False)) # 提醒内容不为空时,启动定时任务 if alarm_dict: init_alarm(alarm_dict) # 初始化定时任务
def init_wechat_config(): """ 初始化微信所需数据 """ # print('初始化微信所需数据开始..') # 从config copy ,用于保存新的接口内容。 myset = config.copy() print('=' * 80) # start---------------------------处理自动回复好友---------------------------start reply = myset.get('auto_reply_info') if reply.get('is_auto_reply'): if reply.get('is_auto_reply_all'): auto_reply_list_key = 'auto_reply_black_list' auto_reply_list_uuid_name = 'auto_reply_black_uuids' else: auto_reply_list_key = 'auto_reply_white_list' auto_reply_list_uuid_name = 'auto_reply_white_uuids' auto_reply_uuids_list = [] for name in reply.get(auto_reply_list_key): if not name.strip(): continue if name.lower() in FILEHELPER_MARK: # 判断是否文件传输助手 auto_reply_uuids_list.append(FILEHELPER) continue friend = get_friend(name) if friend: auto_reply_uuids_list.append(friend['UserName']) else: print('自动回复中的好友昵称『{}』有误。'.format(name)) reply[auto_reply_list_uuid_name] = set(auto_reply_uuids_list) # print('已开启图灵自动回复...') # end---------------------------处理自动回复好友---------------------------end # start ----------------------------------- 群功能初始化 ----------------------------------- start helper = myset.get('group_helper_conf') if helper.get('is_open'): if helper.get('is_all', False): group_list_key = 'group_name_black_list' group_list_uuid_name = 'group_black_uuids' else: group_list_key = 'group_name_white_list' group_list_uuid_name = 'group_white_uuids' group_uuid_list = [] for name in helper.get(group_list_key): if not name.strip(): continue group = get_group(name) if group: group_uuid_list.append(group['UserName']) else: print('群助手中的群聊名称『{}』有误。' '(注意:必须要把需要的群聊保存到通讯录)'.format(name)) helper[group_list_uuid_name] = set(group_uuid_list) # end ----------------------------------- 群功能初始化 ----------------------------------- end alarm = myset.get('alarm_info') alarm_dict = {} if alarm.get('is_alarm'): for gi in alarm.get('girlfriend_infos'): ats = gi.get('alarm_timed') if not ats: continue uuid_list = [] nickname_list = [] # start---------------------------处理好友---------------------------start friends = gi.get('wechat_name') if isinstance(friends, str): friends = [friends] if isinstance(friends, list): for name in friends: if name.lower() in FILEHELPER_MARK: # 判断是否文件传输助手 uuid_list.append(FILEHELPER) nickname_list.append(name) continue name_info = get_friend(name) if not name_info: print('定时提醒中的好友昵称『{}』无效'.format(name)) else: uuid_list.append(name_info['UserName']) nickname_list.append(name) # end---------------------------处理好友---------------------------end # start---------------------------群组处理---------------------------start group_names = gi.get('group_name') if isinstance(group_names, str): group_names = [group_names] if isinstance(group_names, list): for name in group_names: name_info = get_group(name) if not name_info: print('定时任务中的群聊名称『{}』有误。' '(注意:必须要把需要的群聊保存到通讯录)'.format(name)) else: uuid_list.append(name_info['UserName']) nickname_list.append(name) # end---------------------------群组处理---------------------------end # start---------------------------定时处理---------------------------start if isinstance(ats, str): ats = [ats] if isinstance(ats, list): for at in ats: times = TIME_COMPILE.findall(at) if not times: print('时间{}格式出错'.format(at)) continue hour, minute = int(times[0][0]), int(times[0][1]) temp_dict = {'hour': hour, 'minute': minute, 'uuid_list': uuid_list, 'nickname_list': nickname_list} temp_dict.update(gi) alarm_dict[md5_encode(str(temp_dict))] = temp_dict # end---------------------------定时处理---------------------------end alarm['alarm_dict'] = alarm_dict # 将解析的数据保存于 config 中。 config.update(myset) # print(json.dumps(alarm_dict, ensure_ascii=False)) # print('初始化微信所需数据结束..') log_all_config()