How to get time passed from Alexa response to user reply?

338 Views Asked by At

Is it possible to get the time users take to answer a command from Alexa using the alexa-sdk? To make it more visual, I'm trying to measure the following:

User says: "Alexa open my app"
Alexa says: "Welcome to my app, say next to go to the next section"
-- A few seconds pass here, this is what I need to know --
User says: "Next"

I couldn't find anything in the docs, I'd normally try to use something like process.hrtime to start a timer before and after the response is made, however my handler looks something like this:

let timer;

const StartIntentHandler = {
    canHandle(handlerInput) {
        return (
            handlerInput.requestEnvelope.request.type === 'IntentRequest' &&
            handlerInput.requestEnvelope.request.intent.name === 'StartIntent'
        );
    },
    handle(handlerInput) {
        timer = process.hrtime();
        const speechText = 'Welcome to my app, say next to go to the next section';

        return handlerInput.responseBuilder
            .speak(speechText)
            .reprompt(speechText)
            .getResponse();
    }
};

const NextIntentHandler = {
    canHandle(handlerInput) {
        return (
            handlerInput.requestEnvelope.request.type === 'IntentRequest' &&
            handlerInput.requestEnvelope.request.intent.name ===
                'NextIntent'
        );
    },
    handle(handlerInput) {
        const diff = process.hrtime(timer);
        const timeInNanoseconds = diff[0] * 1e9 + diff[1];

        return handlerInput.responseBuilder
            .speak(`${timeInNanoseconds} nanoseconds`)
            .getResponse();
    }
};

However this starts counting just before Alexa starts the command so the time I get is the time it takes Alexa to speak the command + the user delay to reply.

Right now I measured Alexa's response time using a stopwatch and I'm subtracting that from the total time but it's less than ideal.

2

There are 2 best solutions below

1
On BEST ANSWER

The information of the time when Alexa stopped speaking the command the time user starts to respond is user private information and its not exposed to developers.

Note: Your current method of using stopwatch and subtracting doesn't give the correct measure of time because you are not accounting for the time the user device takes to send the information to the AVS (Alexa voice service) cloud and the time AVS takes to invoke your skill lambda.

0
On

Simply you can add <break time = '300ms'/> SSML tag

Example:-

const speakOutput = "Hello, here is your test <break time = '300ms'/>" ;

handlerInput.responseBuilder
                .speak(speakOutput)
                .getResponse();