如何在Alexa Skill lambda函数中正确指定SSML?
我正在尝试制作一个Alexa技能,其中Alexa说的是用SSML标记的东西。我试图模仿这个repo的例子,但我总是收到一个lambda响应
{
...
"response": {
"outputSpeech": {
"type": "SSML",
"ssml": "<speak> [object Object] </speak>"
},
...
}
和Alexa字面上说“对象对象”。
这是我输入到我的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',
};
EDIT:
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上的the alexa-sdk source code for response.js,代码中的speechOutput
对象应该是一个字符串。 Response.js负责构建您尝试在代码中构建的响应对象:
this.handler.response = buildSpeechletResponse({
sessionAttributes: this.attributes,
output: getSSMLResponse(speechOutput),
shouldEndSession: true
});
深入挖掘,buildSpeechletResponse()调用createSpeechObject(),它直接负责在Alexa Skills Kit响应中创建outputSpeech
对象。
因此,对于没有高级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将针对以上代码发出以下response object:
{
"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>"
}
}
}