Kramer
Kramer

Reputation: 399

AWS lambda function to speak number as digit in alexa

I have tried to use say-as interpret-as to make Alexa speak number in digits

Example - 9822 must not read in words instead '9,8,2,2'

One of the two ways I have tried is as follows:

this.emit(':tell',"Hi "+clientname+" your "+theIntentConfirmationStatus+" ticket is sent to "+ "<say-as interpret-as='digits'>" + clientno + "</say-as>",'backup');  

The other one is this:

this.response.speak("Hi "+clientname+" your "+theIntentConfirmationStatus+" ticket is sent to "+ "<say-as interpret-as='digits'>" + clientno + "</say-as>");

Both are not working but working on a separate fresh function.

Upvotes: 1

Views: 1334

Answers (3)

Ron Lisle
Ron Lisle

Reputation: 1164

Edited: Yep, nightflash's answer is great. You could also break the numbers up yourself if you need other formatting, such as emphasizing particular digits, add pauses, etc. You would need to use your Lambda code to convert the numeric string to multiple digits separated by spaces and any other formatting you need.

Here's an example based on the answers in this post:

var inputNumber = 12354987;
var output = '';
var sNumber = inputNumber.toString();

for (var i = 0, len = sNumber.length; i < len; i += 1) {
    // just adding spaces here, but could be SSML attributes, etc.
    output = output + sNumber.charAt(i) + ' ';
}

console.log(output);

This code could be refactored and done many other ways, but I think this is about the easiest to understand.

Upvotes: 0

Amod Gokhale
Amod Gokhale

Reputation: 2448

You can split number into individual digits using sample function ( please test it for your possible inputs-its not tested for all input). You can search for similar function on stackoverflow

function getNumber(tablenumber) {
    var number = (""+tablenumber).split("");
    var arrayLength = number.length;
    var tmp =" ";
    for (var i = 0; i < arrayLength; i++) {

        var tmp = tmp + myStringArray[i] + ", <break time=\"0.4s\"/> ";
    }
    return tmp;
}

In your main function... call this

var finalresult = getNumber(clientno);
this.emit(':tell',"Hi "+clientname+" your "+theIntentConfirmationStatus+" ticket is sent to "+  finalresult ,'backup');

Upvotes: 0

Nightflash
Nightflash

Reputation: 118

Actually your code SHOULD work. Maybe you can try in test simulator and send us the code your script produces? Or the logs?

I've tried the following:

   <speak>
        1. The numbers are: <say-as interpret-as="digits">5498</say-as>.
        2. The numbers are: <say-as interpret-as="spell-out">5498</say-as>.
        3. The numbers are: <say-as interpret-as="characters">5498</say-as>.
        4. The numbers are: <prosody rate="x-slow"><say-as interpret-as="digits">5498</say-as></prosody>.
        5. The number is: 5498.
    </speak>

Digits, Spell-out and Characters all have the effect you want. If you want to Alexa to say it extra slow, use the prosody in #4.

Try using examples #2 or #3, maybe this works out? Otherwise the example from Amod will work too.

Upvotes: 5

Related Questions