7
2

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 v2でのstateがない

Last updated at Posted at 2018-04-23

v2でのstateはこうやる?

v2にstateという概念がない。
this.handler.state = ...;とかってのがない
てか、もともとAlexaから送られてくるjsonにstateとかって項目はなくてsessionAttributesの中にSTATEという値を入れてあげてそれで管理していた。

ここで、再確認しておくのがv2でのsessionAttributesの保存方法は

// 保存の方法は
attributes = {
  "key": "value"
}
handlerInput.attributesManager.setSessionAttributes(attributes);

この容量で

attribute = {
  STATE: "stateKey"
}
handlerInput.attributesManager.setSessionAttributes(attributes);

って、設定してあげてcanHandleの中で

return handlerInput.requestEnvelope.session.attributes.STATE === "stateKey"

とかってしてあげればいいのではないか
つまり、こんな感じ?

const sampleIntentHandler = {
  canHandle(handlerInput) {
    return handlerInput.requestEnvelope.request.type === 'IntentRequest'
      && handlerInput.requestEnvelope.request.intent.name === 'levelIntent'
      && handlerInput.requestEnvelope.session.attributes.STATE === "stateKey"; // ここ
  },
  handle(handlerInput) {
    ...
  }
};

handlerInput.attributesManager.getSessionAttributes().STATEstateKeyがはいっていなかったらcanHandlefalseを返されるので、呼ばれない
他にいい方法ってか、ちゃんとした方法がある?

7
2
0

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
7
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?