Search code examples
javascriptamazon-web-servicesalexa-skills-kit

Programmatically insert custom content into Intent Confirmation message?


I'm using the AWS ASK SDK for Node.js V2 to build an Alexa skill, and I'm wondering if it's possible to programmatically generate or update 'Alexa Prompt' for the 'Intent confirmation'.

The challenge is that we are running a search for a price, and the goal is to inject the price into the 'Intent confirmation' message, before asking for it.

I was thinking of trying to 'reprompt' the user, and force the reprompt after I have a price, but this feels dirty:

module.exports = {
    canHandle(handlerInput) {
        return (
            handlerInput.requestEnvelope.request.type === 'IntentRequest' &&
            handlerInput.requestEnvelope.request.intent.name ===
                'HelloWorldIntent'
        );
    },
    async handle(handlerInput) {
        let speechText;
        let repromptText;
        
        //perform web request to get price
        //then dynamically update the intent confirmation response prompt to include price, 
        //before asking intent confirmation prompt?
        
        return handlerInput.responseBuilder
            .speak(speechText)
            .getResponse();
    }
}

The documentation is lacking to say the least.


Solution

  • You can use Dialog.ConfirmIntent directive to send Alexa a command to confirm all the information the user has provided for the intent. You can also provide a prompt to ask the user for confirmation in an OutputSpeech object in the response.

    In ask-nodejs-sdk v2, ConfirmIntent directive can be send through addConfirmIntentDirective().

    Ex:

    response = handlerInput.responseBuilder
      .speak('The price is 10 dollars, shall I confirm?')
      .reprompt('shall I confirm?')
      .addConfirmIntentDirective()
      .getResponse();
    

    Check this answer for more info.
    More on Dialog directives here.
    Check out the documentation of ResponseBuilder.