78 - Saving user attributes in DynamoDB with the ASK SDK V2


The code is available on SkillTempaltes.com ask new —url https://skilltemplates.com/templates.json and choose the attributes starter template - https://youtu.be/G7U6vcI4jew?t=63

Steps

  1. Use the full SDK const Alexa = require('ask-sdk') - https://youtu.be/G7U6vcI4jew?t=143

  2. Code example - https://youtu.be/G7U6vcI4jew?t=210

    const MyNameIsIntentHandler = {
      canHandle(handlerInput) {
        return handlerInput.requestEnvelope.request.type === 'IntentRequest'
          && handlerInput.requestEnvelope.request.intent.name === 'MyNameIsIntent';
      },
      async handle(handlerInput) {
       
        const nameValue = handlerInput.requestEnvelope.request.intent.slots.name.value;
        let speechText = `Hello ${nameValue}. It's nice to meet you.`;
       
        const attributesManager = handlerInput.attributesManager;
        const responseBuilder = handlerInput.responseBuilder;
       
        const attributes = await attributesManager.getPersistentAttributes() || {};
       
        if (Object.keys(attributes).length === 0) {
          attributes.nameValue = nameValue;
          attributesManager.setPersistentAttributes(attributes);
          await attributesManager.savePersistentAttributes();
        } else {
          speechText = `I remember you... you told me your name was ${attributes.nameValue.toString()}.`;
        }
           
        return responseBuilder
          .speak(speechText)
          .getResponse();
      },
    };
    
  3. Use the ‘standard()’ skill builder const skillBuilder = Alexa.SkillBuilders.standard();

  4. Add code to use and create the table

    const skillBuilder = Alexa.SkillBuilders.standard();
       
    exports.handler = skillBuilder
      .addRequestHandlers(
        LaunchRequestHandler,
        MyNameIsIntentHandler,
        HelpIntentHandler,
        CancelAndStopIntentHandler,
        SessionEndedRequestHandler
      )
      .addErrorHandlers(ErrorHandler)
      .withTableName('alexa-data')
      .withAutoCreateTable(true)
      .lambda();
    
  5. Set the IAM permissions


Leave A Comment