1. 程式人生 > 實用技巧 >Python實現智慧回覆

Python實現智慧回覆

我們來利用圖靈機器人來實現實時回覆資訊

第一步 : 註冊一個圖靈機器人

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

建立成功後我們可以得到apiKeyuserId 儲存下來,我們稍後會用到

第二步 : 編寫程式碼

附上 官方 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("您好!"))

在這裡提醒大家免費版的認證使用者每天回覆數量有限

機器人回覆為秒回,更近於真人可以在程式中設定時間