Python实现智能回复

我们来利用图灵机器人来实现实时回复信息

第一步 : 注册一个图灵机器人

网址 : http://www.turingapi.com/

 创建成功后我们可以得到  apiKey 保存下来,我们稍后会用到

第二步 : 编写代码

附上 官方 API 接口文档 : https://www.kancloud.cn/turing/www-tuling123-com/718227

import logging
import requests


def get_response(_info):
    api_url = "http://openapi.tuling123.com/openapi/api/v2"
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36",
        "Content-Type": "application/json;charset=UTF-8"
    }
    data = {
        "reqType": 0,
        "perception": {
            "inputText": {
                "text": _info
            },
            "inputImage": {
                "url": ""
            },
            "selfInfo": {
                "location": {
                    "city": "北京",
                    "province": "北京",
                    "street": "信息路"
                }
            }
        },
        "userInfo": {
            "apiKey": "6d6c975a54828423fbd2b6a3d7c166e98",
            "userId": "shang"
        }
    }

    r = requests.post(url=api_url, headers=headers, json=data).json()
    logging.warning(r)
    text = r.get("results", [{}])[0].get("values", {}).get("text", "暂时不能说话")
    return text


logging.warning(get_response("您好!"))

在这里提醒大家免费版的认证用户每天回复数量有限

机器人回复为秒回,更近于真人可以在程序中设置时间

原文地址:https://www.cnblogs.com/shangwei/p/13535943.html