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

3

7

I am trying to make an Alexa skill where Alexa says something that has been marked up with SSML. I have tried to mimic the example in this repo, but I am always receiving a lambda response of

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

and Alexa literally says "object object".


This is what I input to my lambda function (using 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.")

Setting speechOutput like this also isn't working:

var speechOutput = {
    type: "SSML",
    ssml: 'This <break time=\"0.3s\" /> is not working',
};


EDIT:

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

Anyone have any idea where I'm going wrong?

Helladic answered 21/1, 2017 at 5:20 Comment(4)
Are you using any helpers to build your response?Lacielacing
Not entirely sure what you mean, but I think I am not.Helladic
Are you working off a template or some example Alexa skill?Lacielacing
Oh yeah, I was following the official tutorial, which now seems to have changed... I've updated my question to include my source code.Helladic
R
5

Per the alexa-sdk source code for response.js on GitHub, the speechOutput object in your code is expected to be a string. Response.js is responsible for building the response object you're trying to build in your code:

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

Digging deeper, buildSpeechletResponse() invokes createSpeechObject(), which is directly responsible for creating the outputSpeech object in the Alexa Skills Kit response.

So for simple responses with no advanced SSML functionality, just send a string as that first parameter on :tell and let alexa-sdk handle it from there.


For advanced ssml functionality, like pauses, give the ssml-builder npm package a look. It allows you to wrap your response content in SSML without having to implement or hardcode an SSML parser yourself.

Example usage:

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

This example emits an ask response where both the speech output and the reprompt speech are set to the same value. SSML Builder will correctly parse the ampersand (which is an invalid character in SSML) and inject a pause 100ms pause in-between the two say statements.

Example response:

Alexa Skills Kit will emit the following response object for the code above:

{
  "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>"
    }
  }
}
Rosemonde answered 23/1, 2017 at 1:36 Comment(0)
V
5

It is an old question but I recently had a similar problem and wanted to contribute with an answer which doesn't need extra dependencies.

As mentioned, speechOutput suppose to be a string so the reason alexa says "object object" is because instead it is a json.

Trying your handler as follows

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

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

returns this response

{ 
  ...
  "response": {
    "outputSpeech": {
    "ssml": "<speak> This <break time=\"0.3s\" /> should work </speak>",
    "type": "SSML"
  },
  ...
}
Vivle answered 28/12, 2017 at 11:10 Comment(1)
Thanks, that worked perfectly and without additional dependenciesLetsou
H
0

You can write your code like this :

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

I was facing the same issue and could solve by writing the code this way.

Hermie answered 22/8, 2019 at 10:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.