Esempio n. 1
0
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
Esempio n. 2
0
def get_acib_info():
    """
    从词霸中获取每日一句,带英文。
    :return:str ,返回每日一句(双语)
    """
    print('获取格言信息(双语)...')
    try:
        resp = requests.get('http://open.iciba.com/dsapi')
        if resp.status_code == 200 and is_json(resp):
            content_dict = resp.json()
            content = content_dict.get('content')
            note = content_dict.get('note')
            return '{}{}'.format(content, note)

        print('没有获取到格言数据。')
    except requests.exceptions.RequestException as exception:
        print(exception)
Esempio n. 3
0
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
Esempio n. 4
0
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
Esempio n. 5
0
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