Search code examples
amazon-web-servicesalexa-skills-kitalexa-voice-service

How to make Alexa countdown in seconds


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!!!


Solution

  • 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" />`;
    }