14
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ask-sdkでdynamodb

Last updated at Posted at 2018-05-18

参考

前提条件

nodeのバージョンがv8.10であること

まるっと、コピペでLaunchRequestをリクエストしてあげたら、「sampleTableName」というテーブルが作成され以下の画像のように保存されます。

スクリーンショット 2018-05-19 0.05.31.png
npm install --save ask-sdk
const Alexa = require('ask-sdk');


let skill;
exports.handler = async function (event, context) {
    if (!skill) {
      skill = Alexa.SkillBuilders.standard() // <= standard()
        .addRequestHandlers(
            LaunchRequestHandler)
        .withTableName("sampleTableName") // これを追加(テーブル名)
        .withAutoCreateTable(true) //テーブル作成もスキルから行う場合は、これも追加
        .create();
    }
    return skill.invoke(event);
}

const LaunchRequestHandler = {
    canHandle(handlerInput) {
        return handlerInput.requestEnvelope.request.type === 'LaunchRequest';
    },
    async handle(handlerInput) {
        // 取得。awaitで取得するまで待つ
        var dynamos = await handlerInput.attributesManager.getPersistentAttributes()
        
        // 保存。awaitで保存できるまで待つ
        dynamos = {
          "key": "value"
        }
        handlerInput.attributesManager.setPersistentAttributes(dynamos); // セット
        await handlerInput.attributesManager.savePersistentAttributes(); // 保存
        var msg = "お返事";
        return handlerInput.responseBuilder.speak(msg).reprompt(msg).getResponse();
    }
};

取得

var attributes = await handlerInput.attributesManager.getPersistentAttributes()

保存

attributes = {
  "key": "value"
}
handlerInput.attributesManager.setPersistentAttributes(attributes); // セット
await handlerInput.attributesManager.savePersistentAttributes(); // 保存

lambdaのテストで使用したJSON

{
  "session": {
    "new": true,
    "sessionId": "amzn1.echo-api.session.[unique-value-here]",
    "attributes": {},
    "user": {
      "userId": "amzn1.ask.account.[unique-value-here]"
    },
    "application": {
      "applicationId": "amzn1.ask.skill.[unique-value-here]"
    }
  },
  "version": "1.0",
  "request": {
    "locale": "en-US",
    "timestamp": "2016-10-27T18:21:44Z",
    "type": "LaunchRequest",
    "requestId": "amzn1.echo-api.request.[unique-value-here]"
  },
  "context": {
    "AudioPlayer": {
      "playerActivity": "IDLE"
    },
    "System": {
      "device": {
        "supportedInterfaces": {
          "AudioPlayer": {}
        }
      },
      "application": {
        "applicationId": "amzn1.ask.skill.[unique-value-here]"
      },
      "user": {
        "userId": "amzn1.ask.account.[unique-value-here]"
      }
    }
  }
}
14
6
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?