Amazon Alexaask-sdk

[ASK SDK] Create Request Interceptor by TypeScript

When using ASK SDK, we can intercept the request / response by Interceptor. Requirement If you want to create […]

広告ここから
広告ここまで

When using ASK SDK, we can intercept the request / response by Interceptor.

Requirement

If you want to create a Interceptor, we have to instal the following packages.

  • ask-sdk-core: Import the HandlerInput type
  • ask-sdk-runtime: Import the RequestIntereptor interface

$ yarn -D ask-sdk-core ask-sdk-runtime

How to make it

We can make a Interceptor object by the following code.

import { HandlerInput } from 'ask-sdk-core';
import { RequestInterceptor } from 'ask-sdk-runtime'

const Interceptor: RequestInterceptor<HandlerInput> = {
  async process(input: HandlerInput) {
    console.log(`Input ${JSON.stringify(input)}`)
  }
}

If you want to do something, we can do it.

Example: Set a Invokation count

import { HandlerInput } from 'ask-sdk-core';
import { RequestInterceptor } from 'ask-sdk-runtime'

const Interceptor: RequestInterceptor<HandlerInput> = {
  async process(input: HandlerInput) {
    const { attributesManager } = input
    const data = await attributesManager.getPersistentAttributes()
    console.log('Before: %j', data)
    if (input.context.session && input.context.session.isNewSession) return
    console.log('is new session')
    const newData = {
      ...data,
      count: data.count || 0 + 1
    }
    console.log('Before: %j', newData)

    attributesManager.setPersistentAttributes(newData);
    await attributesManager.savePersistentAttributes();
  }
}

ブックマークや限定記事(予定)など

WP Kyotoサポーター募集中

WordPressやフロントエンドアプリのホスティング、Algolia・AWSなどのサービス利用料を支援する「WP Kyotoサポーター」を募集しています。
月額または年額の有料プランを契約すると、ブックマーク機能などのサポーター限定機能がご利用いただけます。

14日間のトライアルも用意しておりますので、「このサイトよく見るな」という方はぜひご検討ください。

広告ここから
広告ここまで

Related Category posts