Amazon AlexaAWS

Serverless FrameworkでAlexa Skills Kitのサンプルコードを試してみる(City Guide編)

AlexaでCity Guideを作るサンプルコードがGitHubに公開されています。 https://github.com/alexa/skill-sample-nodejs-city-guide これをローカルで試し […]

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

AlexaでCity Guideを作るサンプルコードがGitHubに公開されています。
https://github.com/alexa/skill-sample-nodejs-city-guide

これをローカルで試してみたかったので、Serverless Frameworkに突っ込んでみました。

準備

$ npm install -g serverless
$ serverless create \ 
  --path cityguide \
  --template aws-nodejs \
  --name serverless-alexa-city-guide 
$ cd cityguide
$ npm init -y
$ npm install alexa-sdk ical --save
$ touch index.js

Copy example lambda code

以下のコードをindex.jsに貼り付けます。
https://github.com/alexa/skill-sample-nodejs-city-guide/blob/master/src/index.js

exports.handler = function (event, context, callback) {
    alexa = Alexa.handler(event, context);
    alexa.registerHandlers(newSessionHandlers, startSearchHandlers, topFiveHandlers);
    alexa.execute();
};

上記のコードを、以下のように書き換えましょう。

module.exports.hello = function(event, context, callback) {
    alexa = Alexa.handler(event, context);
    alexa.registerHandlers(newSessionHandlers, startSearchHandlers, topFiveHandlers);
    alexa.execute();
};

Configure Serverless.yml

Serverless Frameworkでは以下のように設定します。

functions:
  hello:
    handler: index.hello
    events:
      - alexaSkill

Test local(Alexa Skill起動)

Alexa Skillを起動させた時のメッセージを確認してみましょう。

以下のようなJSONをLambaにinvokeさせることで、動作確認が簡単に行えます。

$ sls invoke local -f hello -d '{
  "session":{
    "application":{
      "applicationId":""
    },
    "user":{
      "userId":""
    }
  },
  "request":{
    "type": "LaunchRequest"
  }
}'

以下のレスポンスが返って来ればOKです。

{
    "version": "1.0",
    "response": {
        "outputSpeech": {
            "type": "SSML",
            "ssml": "<speak> Seattle Guide. You can ask me for an attraction, the local news, or  say help. What will it be? </speak>"
        },
        "shouldEndSession": false,
        "reprompt": {
            "outputSpeech": {
                "type": "SSML",
                "ssml": "<speak> You can ask me for an attraction, the local news, or  say help. What will it be? </speak>"
            }
        }
    },
    "sessionAttributes": {
        "STATE": "_SEARCHMODE"
    }
}

シアトルのオススメスポットを聞いてみる

せっかくなので'getTopFiveIntentというインテントを呼び出してみましょう。

今度は以下のようなJSONを読ませます。

$ sls invoke local -f hello -d '{
  "session":{
    "application":{
      "applicationId":""
    },
    "user":{
      "userId":""
    }
  },
  "request":{
    "type":"IntentRequest",
    "intent": {
      "slots": {},
      "name":"getTopFiveIntent"
    }
  }
}'

以下の結果が返ってくる(はず)です。

{
    "version": "1.0",
    "response": {
        "outputSpeech": {
            "type": "SSML",
            "ssml": "<speak> Here are the top five things to  do in Seattle. Number 5: Take a spin on the  Seattle Great Wheel.\n Number 4: Breathe in the culture  at the Seattle Art  Museum.\n Number 3: Earn your  wings at the Museum  of Flight.\n Number 2: Get shopping at Pike Place Market.\n Number 1: Visit the Space Needle and see Seattle from  above.\n You can tell me a number for more information. For example open number one. </speak>"
        },
        "shouldEndSession": false,
        "reprompt": {
            "outputSpeech": {
                "type": "SSML",
                "ssml": "<speak>  You can tell me a number for more information. For example open number one. </speak>"
            }
        },
        "card": {
            "type": "Simple",
            "title": "Top Five Things To See in Seattle",
            "content": "Here are the top five things to  do in Seattle. Number 5: Take a spin on the  Seattle Great Wheel.\n Number 4: Breathe in the culture  at the Seattle Art  Museum.\n Number 3: Earn your  wings at the Museum  of Flight.\n Number 2: Get shopping at Pike Place Market.\n Number 1: Visit the Space Needle and see Seattle from  above.\n You can tell me a number for more information. For example open number one."
        }
    },
    "sessionAttributes": {
        "STATE": "_TOPFIVE"
    }
}

元ソースをみると配列で定義された内容が読み上げられていることがわかります。

サンプル

https://github.com/hideokamoto/serverless-alexa-city-guide

補足

リクエスト時のrequest.intent.nameintentSchema.jsonで定義されている内容を指定すると、そのほかの会話についてもテストすることができます。

まとめ

日本にまだ来ていないにも関わらず相当な盛り上がりを見せているAmazon Alexaですが、このようにデバイスがなくとも動きを確認することが簡単にできます。

Node.jsとLambda、もしくはServerless Frmeworkが扱えれば比較的簡単にSkillの開発などもできるのではと思いますので、みんなでトライアンドエラーの情報をシェアできればいいなーと思います。

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

WP Kyotoサポーター募集中

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

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

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

Related Category posts