python接入图灵机器人_python3 调用图灵机器人API实现语音聊天

首先需要在www.tuling123.com注册,

然后创建一个机器人,设置机器人的基本信息。

导入pyttsx3 库 实现语音功能

import json 导入json库

import urllib.request

import pyttsx3 #导入语音库

engine=pyttsx3.init() #初始化语音库

#语速

rate = engine.getProperty('rate')

engine.setProperty('rate', rate-50)

api_url = "http://openapi.tuling123.com/openapi/api/v2" #图灵机器人api网址

while 1:

text_input = input('我:')

req = {

"perception":

{

"inputText":

{

"text": text_input

},

"selfInfo":

{

"location":

{

"city": "咸阳",

"province": "咸阳",

"street": "人民路"

}

}

},

"userInfo":

{

"apiKey": "a4fbaea97---------------------980c2",#你的apiKey

"userId": "OnlyUseAlphabet" #不知道用途

}

}

# print(req)

# 将字典格式的req编码为utf8

req = json.dumps(req).encode('utf8')

# print(req)

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']

results_text = response_dic['results'][0]['values']['text']

print('Turing的回答:')

#print('code:' + str(intent_code))

print(' ' + results_text) #打印机器人的回复

engine.say(results_text)#合成语音

engine.runAndWait()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值