如何正确指定Alexa技能的lambda函数中的SSML?

我正在努力使Alexa的技能在Alexa中被标记为SSML。 我试图模仿在这个回购的例子,但我总是收到一个lambda响应

{ ... "response": { "outputSpeech": { "type": "SSML", "ssml": "<speak> [object Object] </speak>" }, ... } 

和Alexa的字面意思是“对象对象”。


这是我input到我的lambda函数(使用node.js):

 var speechOutput = { type: "SSML", ssml: 'This <break time=\"0.3s\" /> is not working', }; this.emit(':tellWithCard', speechOutput, SKILL_NAME, "ya best not repeat after me.") 

像这样设置speechOutput也不起作用:

 var speechOutput = { type: "SSML", ssml: 'This <break time=\"0.3s\" /> is 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: 'This <break time=\"0.3s\" /> is not working', }; this.emit(':tellWithCard', speechOutput, SKILL_NAME, "some text here") } }; 

任何人都知道我要去哪里错了?

根据GitHub上的response.js的alexa-sdk源代码,代码中的speechOutput对象应该是一个string。 Response.js负责构build您要在代码中构build的响应对象:

 this.handler.response = buildSpeechletResponse({ sessionAttributes: this.attributes, output: getSSMLResponse(speechOutput), shouldEndSession: true }); 

深入挖掘, buildSpeechletResponse()调用createSpeechObject() ,它直接负责在Alexa技能包响应中创buildoutputSpeech对象。

因此,对于没有高级SSMLfunction的简单响应,只需发送一个string作为第一个参数:tell并让alexa-sdk从那里处理它。


对于高级的ssmlfunction,比如暂停,给ssml-builder npm包一下。 它允许你用SSML封装你的响应内容,而不必自己实现或硬编码SSMLparsing器。

用法示例:

 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); 

这个例子发出一个询问应答,其中语音输出和重新提示语音都被设置为相同的值。 SSML Builder将正确地parsing&符号(SSML中的一个无效字符),并在两个say语句之间插入一个暂停100ms的暂停。

响应示例:

Alexa Skills Kit会为上面的代码发出以下响应对象 :

 { "outputSpeech": { "type": "SSML", "ssml": "<speak> This is a test response and works great! <break time='100ms'/> How can I help you? </speak>" }, "shouldEndSession": false, "reprompt": { "outputSpeech": { "type": "SSML", "ssml": "<speak> This is a test response and works great! <break time='100ms'/> How can I help you? </speak>" } } }