Alexa-hosted Hello World スキルにセッション永続生を利用したカウンターをつける。
package.jsonファイル
package.jsonファイルのdependenciesセクションに、npm依存関係を追加。
"ask-sdk-s3-persistence-adapter": "^2.0.0"
index.jsファイル
index.jsファイルに以下を追加します。
// 1. ask persistence adapterの読み込み
const persistenceAdapter = require('ask-sdk-s3-persistence-adapter');
// 2. persistence adapterをスキルビルダーに追加
exports.handler = Alexa.SkillBuilders.custom()
.addRequestHandlers(
LaunchRequestHandler,
...
SesssionEndedRequestHandler
)
.addErrorHandlers(ErrorHandler)
.withPersistenceAdapter(
new persistenceAdapter.S3PersistenceAdapter({bucketName:process.env.S3_PERSISTENCE_BUCKET})
);
.lambda();
// 3. アトリビュートを保存するためのサンプルハンドラー
async handle(handlerInput){
const attributesManager = handlerInput.attributesManager;
let s3Attributes = {"counter":10};
attributesManager.setPersistentAttributes(s3Attributes);
await attributesManager.savePersistentAttributes();
let speechOutput = こんにちは、ハローワールド! 保存したカウンターは${s3Attributes.counter}です
;
return handlerInput.responseBuilder
.speak(speechOutput)
.getResponse();
},
// 4. アトリビュートを読み込むためのサンプルハンドラー
async handle(handlerInput){
const attributesManager = handlerInput.attributesManager;
const s3Attributes = await attributesManager.getPersistentAttributes() || {};
console.log('s3Attributes is: ', s3Attributes);
const counter = s3Attributes.hasOwnProperty('counter')? s3Attributes.counter : 0;
let speechOutput = こんにちは、ハローワールド! カウンターは${counter}です
;
return handlerInput.responseBuilder
.speak(speechOutput)
.getResponse();
},
HelloWorldIntentを起動した回数をカウントを示すために、3,4はHelloWorldIntentのhandle(handlerInput)の中に置く必要がある?
コメントを残す