更新内容:
新增内置JSON-java库(JSON.org)
增加手机位置权限和编程接口
开发示例:
通过JSON调用图灵机器人云服务,进行手机语音交互(全部代码不超过50行哟),能运行于iPhone和安卓。
手机会回答会以下问题(请不要问位置相关问题,出于隐私,该权限未启用):
“鱼香肉丝”、
“讲个笑话”,
“什么是月季”,
“今天从北京到上海的飞机”,
“今天北京到上海的火车”,
“明天农历”,
“天为什么是蓝色的”
……[code]#encoding:utf-8
import Java::hc.server.util.Assistant
import Java::hc.server.ui.ProjectContext
import Java::hc.server.util.json.JSONObject
myAssistant = Class.new(Assistant) {
def onVoice(cmd)
json = JSONObject.new()
#注意:请改为你的图灵机器人key,为了方便他人演示,请勿滥用此公用Key。
json.put("key", "76cf37784c884670921b628856440770")
json.put("info", cmd.getText())
#为获取手机GPS位置,需开启工程权限[Location of mobile],详见文档API。将鼠标移至方法上即可获得文档。
#cmd.getLocationLatitude()#纬度
#cmd.getLocationLongitude()#经度
ctx = ProjectContext::getProjectContext()
#json.put("userid", ctx.getLoginID())#可能多个家庭成员共用
json.put("userid", ctx.getMobileSoftUID())#手机重装客户端后,会重新分配SoftUID,但不会出现家庭成员共用
json = json.request("http://www.tuling123.com/openapi/api")
if json.nil?
ctx.sendMovingMsg("网络故障:" + cmd.getText())
ctx.error("网络故障:http://www.tuling123.com/openapi/api")
else
ctx.log("语音命令:[" + cmd.getText() + "],回应:" + json.toString())
#正常回应示例 :{"text":"亲,已帮你找到菜谱信息","code":200000,"url":"http://homecenter.mobi/abc"}
if json.has("text")
ctx.sendVoice(json.getString("text"))
end
if json.has("code") && (json.getInt("code") == 100000 || json.getInt("code") == 200000)
if json.has("url")
ctx.goExternalURLWhenInSession(json.getString("url"))
end
return true
end
end
return false
end
}.new
ctx = ProjectContext::getProjectContext()
ctx.registerAssistant(myAssistant)[/code]软件详情:https://gitee.com/javalovercn/homecenter/
来自:开源中国社区

