python实现人机交互_Python实现与智能机器人交互

本文将利用API与智能机器人进行人机交互,进行简单的聊天对话;代码由Python语言实现。

这里使用的机器人为图灵机器人,其官方网站为:http://www.turingapi.com

开始前,清先注册账号,并创建一个机器人,获得 apikey,userid 这两个关键参数。

为了简化POST和编码过程,我使用了 requests 这个库;

机器人API:

http://openapi.tuling123.com/openapi/api/v2

API文档:https://www.kancloud.cn/turing/www-tuling123-com/718227

import requests

robotAPI = 'http://openapi.tuling123.com/openapi/api/v2'

apiKey=''

userId=''

message=''

def sendMessage(message,apiKey=apiKey,userId=userId):

jsondata = {'reqType': '0',

'perception': {'inputText': {'text': message},

'inputImage': {'url': 'imageUrl'},

'selfInfo': {'location': {'city': '北京', 'province': 'beijing', 'street': 'xinxilu'}}},

'userInfo': {'apiKey': apiKey, 'userId': userId}}

if not message:return 'empty message.'

r=requests.post(robotAPI,json=jsondata)

if r.status_code != 200:return 'error code'

return r.json()

while 1:

result=sendMessage(input('->'))['results'][0]['values']['text']

print(result)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值