图灵聊天机器人API1.0与API2.0的使用方法

转:https://blog.csdn.net/www_rsqdz_net/article/details/79680461

 https://blog.csdn.net/qq_42292831/article/details/88677623 

思维导图:

图灵机器人: 

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

官方教程:https://www.kancloud.cn/turing/www-tuling123-com/718227

源码: 

# -*- coding: utf-8 -*-
"""
Created on Tue Mar 19 22:07:22 2019
@author: dell
"""
 
import itchat
import requests
import json
itchat.auto_login(hotReload=True)
 
friends = itchat.get_friends()
 
def getResponse(msg):
    url = "http://openapi.tuling123.com/openapi/api/v2"
    data = {
        "reqType":0,
        "perception": {
            "inputText": {
                "text": msg
            },
            "inputImage": {
                "url": "imageUrl"
            }
        },
        "userInfo": {
            "apiKey": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
            "userId": "Geclipse"
        }
    }
    data = json.dumps(data)
    r = requests.post(url,data).json()
    return r['results'][0]['values']['text']
 
 
@itchat.msg_register(itchat.content.TEXT)    #读取接受到的消息的TEXT部分内容,存储进默认的msg
 
def auto_reply(msg):
    friends_message = msg['Text']
    name = msg['FromUserName']
    reply = getResponse(friends_message)
    itchat.send(reply,toUserName=name)
 
itchat.run()

 

API1.0使用方法:

# file TurlingApi1.py
import requests
import json
import yuyinhecheng as hc

def Tuling(words):
    Tuling_API_KEY = "你的AK"

    body = {"key":Tuling_API_KEY,"info":words.encode("utf-8")}

    url = "http://www.tuling123.com/openapi/api"
    r = requests.post(url,data=body)
    if r:
        date = json.loads(r.text)
        print (list(date))
        print(date['text'])
        hc.speak(date['text'])
        try:
            for mylist in date['list']:
                str2=mylist[list(mylist)[0]]
                print(str2)
                hc.speak(str2)
                print(mylist)
        except:
            pass
        return date["text"]
    else:
        return None

if __name__=='__main__':
    Tuling('红烧肉菜谱')

API2.0使用方法:

# file TurlingApi2.py
import json
import urllib.request
import yuyinhecheng as hc

def Tuling(text_input):
    api_url = "http://openapi.tuling123.com/openapi/api/v2"

    req = {
        "perception":
    {
        "inputText":
        {
            "text": text_input
        },

        "selfInfo":
        {
            "location":
            {
                "city": "天津",
                "province": "天津",
                "street": "中央大道"
            }
        }
    },

    "userInfo": 
    {
        "apiKey": "你的AK",
        "userId": "demo"
    }
    }
    # 将字典格式的req编码为utf8
    req = json.dumps(req).encode('utf8')

    http_post = urllib.request.Request(api_url, data=req, headers={'content-type': 'application/json'})
    response = urllib.request.urlopen(http_post)
    response_str = response.read().decode('utf8')
    print(response_str)
    response_dic = json.loads(response_str)
    print(response_dic)

    intent_code = response_dic['intent']['code']
    if (int(str(intent_code)[0])<4):
        print (str(intent_code))
        listp=list(response_dic['intent']['parameters'])
        print (response_dic['intent']['parameters'][listp[0]])
        str2=response_dic['results'][0]['values']['text']
        print(str2)
        hc.speak(str2)
        if ((intent_code==10003)or (intent_code==10015)) :
            for myresults in response_dic['results'][1]['values']['news']:
                print (myresults)
                print(myresults['name'])
                hc.speak(myresults['name'])
    else:
        print('错误。错误代码:' + str(intent_code))

if __name__=='__main__':
    Tuling('水煮鱼菜谱')

语言合成:

#file yuyinhecheng.py

import win32com
import pyttsx3
def speak(str):
    engine = pyttsx3.init()
    voices = engine.getProperty('voices')
    engine.setProperty('voice',voices[3].id)
    engine.say(str)
    engine.runAndWait()
 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值