如何在 Alexa Skill lambda 函数中正确指定 SSML?

How to correctly specify SSML in an Alexa Skill lambda function?

我正在尝试制作一项 Alexa 技能,其中 Alexa 会说一些已用 SSML 标记的内容。我试图模仿此 repo 中的示例,但我总是收到

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

Alexa 字面意思是 "object object"。


这是我输入到我的 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

'use strict';

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

此示例发出询问响应,其中语音输出和重新提示语音都设置为相同的值。 SSML Builder 将正确解析与符号(这是 SSML 中的无效字符)并在两个 say 语句中插入 100 毫秒的暂停 in-between。

示例响应:

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>"
    }
  }
}

这是一个老问题,但我最近遇到了类似的问题,想贡献一个不需要额外依赖项的答案。

如前所述,speechOutput 假设是一个字符串,所以 alexa 说 "object object" 的原因是因为它是 json.

按如下方式尝试您的处理程序

'Speaketh': function () {
    var speechOutput = 'This <break time="0.3s" /> should work';

    this.emit(':tellWithCard', speechOutput, SKILL_NAME, "some text here")
}

returns 这个回复

{ 
  ...
  "response": {
    "outputSpeech": {
    "ssml": "<speak> This <break time=\"0.3s\" /> should work </speak>",
    "type": "SSML"
  },
  ...
}

您可以这样编写代码:

'BreakIntent':function(){
        var speechOutput = 'She is better known as <break time="3s" /> Champion';
        var reprompt = "How can I help?";
        this.response.speak(speechOutput);
        this.response.listen(reprompt);
        this.emit(":responseReady");
    },

我遇到了同样的问题,可以通过这种方式编写代码来解决。