我正在尝试制作一个Alexa技能,其中Alexa说的是用SSML标记的东西.我试图模仿这个回购中的例子,但我总是收到一个lambda响应
{ ... "response": { "outputSpeech": { "type": "SSML", "ssml": "[object Object] " }, ... }
和Alexa字面上说"对象对象".
这是我输入到我的lambda函数(使用node.js):
var speechOutput = { type: "SSML", ssml: 'Thisis not working', }; this.emit(':tellWithCard', speechOutput, SKILL_NAME, "ya best not repeat after me.")
像这样设置speechOutput也不起作用:
var speechOutput = { type: "SSML", ssml: 'Thisis not working', };
index.js
'使用严格';
var Alexa = require('alexa-sdk'); var APP_ID = "MY_ID_HERE"; var SKILL_NAME = "MY_SKILL_NAME"; exports.handler = function(event, context, callback) { var alexa = Alexa.handler(event, context); alexa.APP_ID = APP_ID; alexa.registerHandlers(handlers); alexa.execute(); }; var handlers = { 'LaunchRequest': function () { this.emit('Speaketh'); }, 'MyIntent': function () { this.emit('Speaketh'); }, 'Speaketh': function () { var speechOutput = { type: "SSML", ssml: 'Thisis not working', }; this.emit(':tellWithCard', speechOutput, SKILL_NAME, "some text here") } };
任何人都知道我哪里出错了?
根据GitHub上response.js的alexa-sdk源代码,代码中的speechOutput
对象应该是一个字符串.Response.js负责构建您尝试在代码中构建的响应对象:
this.handler.response = buildSpeechletResponse({ sessionAttributes: this.attributes, output: getSSMLResponse(speechOutput), shouldEndSession: true });
深入挖掘,buildSpeechletResponse()调用createSpeechObject(),它直接负责outputSpeech
在Alexa Skills Kit响应中创建对象.
因此,对于没有高级SSML功能的简单响应,只需发送一个字符串作为第一个参数,:tell
然后让alexa-sdk从那里处理它.
对于高级ssml功能(如暂停),请查看ssml-builder npm包.它允许您将响应内容包装在SSML中,而无需自己实现或硬编码SSML解析器.
用法示例:
var speech = new Speech(); speech.say('This is a test response & works great!'); speech.pause('100ms'); speech.say('How can I help you?'); var speechOutput = speech.ssml(true); this.emit(':ask', speechOutput , speechOutput);
此示例发出一个ask响应,其中语音输出和reprompt语音都设置为相同的值.SSML Builder将正确解析&符号(这是SSML中的无效字符)并在两个say语句之间暂停100ms暂停.
响应示例:
Alexa Skills Kit将为上面的代码发出以下响应对象:
{ "outputSpeech": { "type": "SSML", "ssml": "This is a test response and works great! " }, "shouldEndSession": false, "reprompt": { "outputSpeech": { "type": "SSML", "ssml": "How can I help you? This is a test response and works great! " } } }How can I help you?
这是一个老问题,但我最近有一个类似的问题,并希望贡献一个不需要额外依赖的答案.
如上所述,speechOutput
假设是一个字符串,所以alexa说"对象对象"的原因是因为它是一个json.
尝试如下处理程序
'Speaketh': function () { var speechOutput = 'Thisshould work'; this.emit(':tellWithCard', speechOutput, SKILL_NAME, "some text here") }
返回此响应
{ ... "response": { "outputSpeech": { "ssml": "This ", "type": "SSML" }, ... }should work