node.js - 如何在 Alexa Skill lambda 函数中正确指定 SSML?

标签 node.js aws-lambda alexa alexa-skills-kit 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',
};


编辑:

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

有人知道我哪里出错了吗?

最佳答案

根据 the alexa-sdk source code for response.js在 GitHub 上,代码中的 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); 

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

响应示例:

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

关于node.js - 如何在 Alexa Skill lambda 函数中正确指定 SSML?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41776014/

相关文章:

javascript - 使用 Sequelize 在创建或保存时获取关联

python - 如何使用 chalice 打印完整的http请求?

java - 如何调用 AWS Lambda 函数的 ssl 端点

amazon-web-services - 将 AWS CloudWatch 日志组流式传输到多个 AWS Elasticsearch 服务

node.js - 直接从 Lambda/tmp 文件夹播放音频

c# - Alexa Skill 请求反序列化失败 - json 到 SkillRequest 对象 C#

javascript - Object.create() 表示参数不是对象

node.js - 如何修复 "Unhandled rejection SequelizeDatabaseError: relation "ABC“不存在”?

node.js - 无法使用 Mongoose 连接到 mongodb 图集

python - Alexa Lambda 函数获取用户的全名