3

I want to be able to have alexa (audibly) countdown 15 seconds in my skill. I know I can just <break time="15s" /> in SSML. But that isn't audible. I also know I can just do:

15<break time="1s" />
14<break time="1s" /> 

or better yet (to account for the time it takes to say the number)

15<break time="0.85s" />
14<break time="0.85s" />

But that's going to be a ton of repeated code if I do this many times over. So I'm probably going to write a function that takes in a number and a number of seconds, and produces an SSML countdown in that interval.

Before I do that, however, I was wondering if there's a proper, built-in way of doing this? Or if someone has a function they've already built for this? Thanks!!!

ProGrammar
  • 298
  • 4
  • 17

2 Answers2

3
function buildCountdown(seconds, break) {
    var countdown = "";

    for (var i = seconds; i > 0; i--) {
        var count = i.toString + "<break time='" + break.toString() + "s' />\n";
        countdown.concat(count);
    }

    return countdown;
}

And then just provide the outputSpeech property:

"outputSpeech": {
    "type": "SSML",
    "ssml": buildCountdown(15, 0.85)
}

I'm not sure about any ASK built-ins for building SSML, but writing functions that generate markup is pretty common when working with Javascript frameworks, so it seems appropriate here.

mrshl
  • 531
  • 3
  • 12
1

I ended up with the following function (with the help of someone on the Alexa slack):

function countDown(numSeconds, breakTime) {
    return Array.apply(null, {length: numSeconds})
        .map((n, i) => {return `<say-as interpret-as="cardinal">${numSeconds-i}</say-as>` })
        .join(`<break time="${breakTime ? breakTime : 0.85}s" />`) + `<break time="${breakTime ? breakTime : 0.85}s" />`;
}
ProGrammar
  • 298
  • 4
  • 17