热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

转:https:blog.csdn.netwww_rsqdz_netarticledetails79680461https:blog.csdn.netqq_42292

转: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&#61;list(response_dic[&#39;intent&#39;][&#39;parameters&#39;])
        print (response_dic[&#39;intent&#39;][&#39;parameters&#39;][listp[0]])
        str2&#61;response_dic[&#39;results&#39;][0][&#39;values&#39;][&#39;text&#39;]
        print(str2)
        hc.speak(str2)
        if ((intent_code&#61;&#61;10003)or (intent_code&#61;&#61;10015)) :
            for myresults in response_dic[&#39;results&#39;][1][&#39;values&#39;][&#39;news&#39;]:
                print (myresults)
                print(myresults[&#39;name&#39;])
                hc.speak(myresults[&#39;name&#39;])
    else:
        print(&#39;错误。错误代码&#xff1a;&#39; &#43; str(intent_code))

if __name__&#61;&#61;&#39;__main__&#39;:
    Tuling(&#39;水煮鱼菜谱&#39;)

语言合成&#xff1a;

#file yuyinhecheng.py

import win32com
import pyttsx3
def speak(str):
    engine &#61; pyttsx3.init()
    voices &#61; engine.getProperty(&#39;voices&#39;)
    engine.setProperty(&#39;voice&#39;,voices[3].id)
    engine.say(str)
    engine.runAndWait()
 


推荐阅读
author-avatar
手机用户2502909293
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有