はじめに
今更ですが、Alexaスキル開発の手順をまとめました
これで自宅にてFireTV操作用として活躍していたAmazon Echo Dot君が更に活躍してくれることでしょう
この記事はASK CLIでの導入編です
手順
1. ASK CLIをインストール
npm install -g ask-cli
2. 初期設定
ask configure
WSL2を使用している人は以下コマンド
ask configure --no-browser
後は対話形式で進めていく
WSL2の人は、URLが表示されるのでそれにアクセス
WSL2以外の人は、途中でブラウザが開くので、ログインしてIAMのアクセス許可を付与しましょう
3. スキルの作成
ask new
こちらも対話形式で進めて行きます
テンプレートは、「Hello World」
モデルは、「Interaction Model」
バックエンドリソースは、「AWS Lambda」
4. 実装
テンプレートの階層は以下のようになっていると思います
.
├── LICENSE.txt
├── ask-resources.json
├── infrastructure
│ └── lambda-deployer
├── lambda
│ ├── index.js
│ ├── package.json
│ └── util.js
└── skill-package
├── assets
│ ├── en-US_largeIcon.png
│ └── en-US_smallIcon.png
├── interactionModels
│ └── custom
│ └── en-US.json
└── skill.json
skill-package/skill.jsonの中身を見てみましょう
ざっくり説明ですが、ここはスキルを公開した際にユーザーから見られる概要の部分になります
{
"manifest": {
"publishingInformation": {
"locales": {
"en-US": {
"summary": "Sample Short Description",
"examplePhrases": [
"Alexa open hello world",
"hello",
"help"
],
"name": "Hello-World",
"description": "Sample Full Description"
},
"isAvailableWorldwide": true,
"testingInstructions": "Sample Testing Instructions.",
"category": "KNOWLEDGE_AND_TRIVIA",
"distributionCountries": []
},
"apis": {
"custom": {
"endpoint": {
"uri": "lambdaのパス"
}
}
},
"manifestVersion": "1.0"
}
}
skill-package/interactionModels/custom/en-US.jsonの中身を見てみましょう
ここはスキルを呼び出す入口ですね
Alexaに「open "hello world"」と呼びかけた後に、「hello」や「how are you」と呼びかけると、、、
{
"interactionModel": {
"languageModel": {
"invocationName": "hello world",
"intents": [
{
"name": "AMAZON.CancelIntent",
"samples": []
},
{
"name": "AMAZON.HelpIntent",
"samples": []
},
{
"name": "AMAZON.StopIntent",
"samples": []
},
{
"name": "HelloWorldIntent",
"slots": [],
"samples": [
"hello",
"how are you",
"say hi world",
"say hi",
"hi",
"say hello world",
"say hello"
]
},
{
"name": "AMAZON.NavigateHomeIntent",
"samples": []
},
{
"name": "AMAZON.FallbackIntent",
"samples": []
}
],
"types": []
}
}
}
lambda/index.jsのHelloWorldIntentが発火して、「Hello World!」と返してくれる訳ですね
const HelloWorldIntentHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'HelloWorldIntent';
},
handle(handlerInput) {
const speakOutput = 'Hello World!';
return handlerInput.responseBuilder
.speak(speakOutput)
//.reprompt('add a reprompt if you want to keep the session open for the user to respond')
.getResponse();
}
};
5. デプロイ
サンプルのままデプロイしてみましょう
ここでデプロイしても公開される訳ではないのでご安心を
ask deploy
6. テスト
以下のコマンドを打つと、CLIでテスト可能です
ask dialog --locale en-US
テストが起動したら、以下のように受け答えを確認しましょう
User > open "hello world"
Alexa > Welcome, you can say Hello or Help. Which would you like to try?
User > hello
Alexa > Hello World!
以下の開発者コンソールにて、ブラウザ上でテキストや音声入力でスキルの動作確認もできます
7. 実機でテスト
今回は、英語のスキルなので、そのまま実機テスト可能ですが、
日本語スキルの場合、どうやらベータ版で公開しないと、実機で確認出来ないそうです...
以下を参考にさせていただきました
日本語スキルの開発方法は次回の記事で説明します
8. 最後に
これにて導入は完了です
次回の記事では、実用的なスキルを作成してみます