Amazon AlexaのCustom SkillのサンプルをService Simulatorで試してみる

2017.06.15

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

はじめに

Amazon Alexaを試してみるためCustom Skillのサンプルを設定し、実機の代わりにService Simulatorから呼び出してみました。以下、その時の手順となります。

アカウントの用意

Amazon.comのアカウントを持っていない場合(AWSとは別)、用意する必要があります。
Amazon.comの右上の「Your Account」から用意してください。

サンプルの取得〜デプロイ

githubより公式のサンプルを取得し、デプロイを行います。主な手順はAmazon AlexaのCustom Skillをサンプルから学ぶを参考にしたのですが、こちらで紹介されているサンプルソースはREADMEに"This Node.js Alexa Skills Kit project has been deprecated. "とありました。

なので別のサンプルソースを取得し、デプロイを行います。以下にその手順を整理します。

サンプルソースの取得、編集

alexa/skill-sample-nodejs-hello-worldよりサンプルソースを取得します。以下のコマンドを実行します。

$ git clone https://github.com/alexa/skill-sample-nodejs-hello-world.git

ソースの編集

取得したソースをそのままでも動きますが、今回はUtteranceを増やしました。Utteranceやその他の用語については以下の記事の「解説」を読んでください。
【Alexa初心者向け】Alexa Skill Kitを噛み砕いて解説してみる

「sample」「say sample」「test」「say test」の4つのUtteranceを追加します。
「sample」「say sample」のUtteranceは「SampleIntent」のIntentに、「test」「say test」のUtteranceは「TestIntent」に紐づくようにします。なのでIntentSchema、Utterances、Lambda Functionは以下のようになります。

IntentSchema.json
{
  "intents": [
    {
      "intent": "HelloWorldIntent"
    },
    {
      "intent": "SampleIntent"
    },
    {
      "intent": "TestIntent"
    }
  ]
}
Utterances.txt
HelloWorldIntent hello
HelloWorldIntent say hello
HelloWorldIntent say hello world
SampleIntent sample
SampleIntent say sample
TestIntent test
TestIntent say test
index.js
'use strict';
var Alexa = require("alexa-sdk");

exports.handler = function(event, context, callback) {
    var alexa = Alexa.handler(event, context);
    alexa.registerHandlers(handlers);
    alexa.execute();
};

var handlers = {
    'LaunchRequest': function () {
        this.emit('SayHello');
    },
    'HelloWorldIntent': function () {
        this.emit('SayHello')
    },
    'SayHello': function () {
        this.emit(':tell', 'Hello World!');
    },
    // add intents
    'SampleIntent': function () {
        console.log('Sample Intent called.')
        this.emit('SaySample')
    },
    'SaySample': function () {
        this.emit(':tell', 'Hello! This is a sample.');
    },
    'TestIntent': function () {
        console.log('Test Intent called.')
        this.emit('SayTest')
    },
    'SayTest': function () {
        this.emit(':tell', 'Hello! This is a test.');
    },
};

それぞれハイライトしてある箇所がサンプルから追加した部分です。

Lambda Functionのデプロイ

Lambda Functionにアップロードするためのzipを用意します。

srcに移動する

$ cd skill-sample-nodejs-hello-world/
$ cd src/

alexa-sdkをインストールする

$ npm install --save alexa-sdk

zipを作成する

$ zip -r helloWorld.zip index.js node_modules

zipをデプロイする手順についてはAmazon AlexaのCustom Skillをサンプルから学ぶの「Lambda Functionを作成する」を参考にしてください。

Custom Skillの作成

Amazon Developer ConsoleにログインしてCustom Skillを作成します。この手順についてもAmazon AlexaのCustom Skillをサンプルから学ぶの「Custom Skillを作成する」を参考にしてください。

「Interaction Model」の「Intent Schema」「Sample Utterances」には、それぞれ先ほど用意したIntentSchema.json、Utterances.txtの内容をコピーします。 alexa-custom-skill-simulator-1

Service Simulatorの実行

Amazon Developer ConsoleのALEXAタブにて、左側に「Test」の項目があるかと思います。その中の「Service Simulator」の「Enter Utterance」に、実機では声にて入力したいUtteranceを入力してみます。

alexa-custom-skill-simulator-2

下側の「Lambda Request」には実機から入力した時にLambdaに渡されるリクエストが、「Lambda Response」にはLambdaから返される値が、それぞれシミュレーションされて表示されます。「say sample」というUtteranceに対して、Intentは「SampleIntent」、レスポンスはindex.jsに記述した「Hello! This is a sample.」が返ってくるようです。また「Listen」ボタンを押すとAlexaの音声を再現してくれるようです。

続いてUtteranceに「test」と入力してみました。こちらも想定通りにIntent、レスポンスとなるようです。

alexa-custom-skill-simulator-3

Lambda Functionにはconsole.log()にてデバッグ用文字列を出力するようにしていました。こちらもCloudWatchで確認してみましたが、無事出力されているようです。

alexa-custom-skill-simulator-4

まとめ

Custom Skillのサンプルを元に、独自のIntentを作成して、それをService Simulatorでシミュレーションしてみました。Amazon Alexaの開発時の参考になれば幸いです。