[日本語Alexa] Alexa-SDK Ver2 をTypeScriptで使う

通常、TypeScriptを使用する場合、別途、型情報の入手が必要であり、SDKの最新の状態に整合しているかどうかは重要な問題となります。 しかし、Alexa SDK Ver2のパッケージには、最初からバンドルされているわけですから、この辺、まったく心配なく利用できるのでは無いでしょうか。
2018.05.05

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

1 はじめに

Alexa SDK Ver2のパッケージには、TypeScriptプロジェクトで使用する定義ファイル(.d.ts)が最初からバンドルされています。このため、特に何もしなくても、TypeScriptで書くことが可能です。

今回は、TypeScriptでAlexa SDK Ver2を使用する方法を紹介させて頂きます。

2 セットアップ

TypeScriptのバージョンは、v2.xが必要です。

$ tsc --version
Version 2.8.3

ノードのTypeScript定義は、含まれていませんのでインストールする必要があります。

$ npm install --save-dev @types/node

使用したtsconfig.jsonは、以下のとおりです。

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs", 
    "esModuleInterop": true,
    "sourceMap": true
  }
}

3 型情報

TypeScriptで作業(VSCodeを利用)していて一番嬉しいのでは、型情報が簡単に手に入るので、コードを書きながらSDKの仕様確認を軽快に進められることです。

どのようなオブジェクトがあるのか、また、そのパラメータは?などは、いちいち調べなくても、.(ドット)を打つだけで分かります。

HandlerInputが持つオブジェクトの列挙(?が付いているものは、オプショナルなのでundefinedの可能性もあります)

ResponseBuiderが持つメソッドの列挙

ResponseBuider.speak()の引数 (string型の引数を1つ取って、Alexa.ResponseBuilderを返す)

VSCodeが型を理解している部分は、コマンドキーを押しながらカーソルを合わせると、ヘルプ表示となり、そのままクリックすると定義されているd.tsにジャンプできます。

4 実装

という事で、ざっくりと書いてみました。
Developing Your First Skill を参考にさせて頂いています

import * as Alexa from 'ask-sdk';
import { RequestEnvelope, ResponseEnvelope, services } from 'ask-sdk-model';

let skill: Alexa.Skill;

exports.handler = async function (event: RequestEnvelope, context: any) {
  if (!skill) {
    skill = Alexa.SkillBuilders.custom()
      .addRequestHandlers(
        LaunchRequestHandler,
        HelloWorldIntentHandler)
      .addErrorHandlers(ErrorHandler)
      .create();
  }
  return skill.invoke(event, context);
}

const LaunchRequestHandler = {
    canHandle(handlerInput: Alexa.HandlerInput) {
      return handlerInput.requestEnvelope.request.type === 'LaunchRequest';
  },
  handle(handlerInput: Alexa.HandlerInput) {
      const speechText = 'Welcome to the Alexa Skills Kit, you can say hello!';
      return handlerInput.responseBuilder
          .speak(speechText)
          .reprompt(speechText)
          .withSimpleCard('Hello World', speechText)
          .getResponse();
  }
}

const HelloWorldIntentHandler = {
  canHandle(handlerInput: Alexa.HandlerInput) {
      return handlerInput.requestEnvelope.request.type === 'IntentRequest'
          && handlerInput.requestEnvelope.request.intent.name === 'HelloWorldIntent';
  },
  handle(handlerInput: Alexa.HandlerInput) {
      const speechText = 'Hello World!';

      return handlerInput.responseBuilder
          .speak(speechText)
          .withSimpleCard('Hello World', speechText)
          .getResponse();
  }
};

const ErrorHandler = {
  canHandle(handlerInput: Alexa.HandlerInput, error: Error) {
      return true;
  },
  handle(handlerInput: Alexa.HandlerInput, error: Error) {
      return handlerInput.responseBuilder
          .speak('error')
          .getResponse();
  }
}

5 最後に

通常、TypeScriptを使用する場合、別途、型情報の入手が必要であり、SDKの最新の状態に整合しているかどうかは重要な問題となります。

しかし、Alexa SDK Ver2のパッケージには、最初からバンドルされているわけですから、この辺、まったく心配なく利用できるのでは無いでしょうか。

参考リンク


npm ask-sdk