Last active
April 13, 2018 08:00
-
-
Save hideokamoto/fd2a25b84bf0ae91746e750f29634839 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
// Alexa-sdkを取得します | |
const Alexa = require('alexa-sdk'); | |
// Alexa SkillのIDを指定します。(省略可) | |
const APP_ID = ''; | |
// リクエスト内容から、nameスロットの値を取り出す処理 | |
const getName = (event) => { | |
if (!event.request.intent) return ''; | |
if (!event.request.intent.slots.name.value) return ''; | |
return `${event.request.intent.slots.name.value} さん`; | |
} | |
// インテント毎のレスポンスをオブジェクト形式で定義 | |
// キーがインテント名となります | |
const handlers = { | |
'LaunchRequest': function () { | |
// LaunchRequestでは、HelloIntentを同じ内容を話すようにします | |
this.emit('HelloIntent'); | |
}, | |
'HelloIntent': function () { | |
const name = getName(this.event); | |
// ユーザーからの返答が必要ない場合は、`:tell`を使います。 | |
// 第二引数にアレクサに喋らせたい内容を記述します。 | |
this.emit(':tell', 'アレクサスキルへようこそ。' + name); | |
}, | |
'Unhandled': function () { | |
// Unhandledはユーザーの発話内容がどのインテントにもマッチしなかった場合に呼び出されます | |
// ユーザーからの返答を求めたい場合は、`:ask`を使います。 | |
// 第二引数にアレクサに喋らせたい内容を記述します。 | |
// 第三引数にユーザーがしばらく返答しなかった場合に再度よびかける内容を記述します。 | |
this.emit(':ask', 'すみません。よく聞き取れませんでした。なにか試したいことはありますか?', 'なにか試したいことはありますか?'); | |
}, | |
'AMAZON.HelpIntent': function () { | |
// AMAZON.XXXIntentはAmazonのビルトインインテントです | |
// 詳細:https://developer.amazon.com/ja/docs/custom-skills/implement-the-built-in-intents.html | |
this.emit(':ask', 'これはアレクサのサンプルスキルです', 'なにか試したいことはありますか?'); | |
}, | |
'AMAZON.CancelIntent': function () { | |
this.emit(':tell', 'ごきげんよう。'); | |
}, | |
'AMAZON.StopIntent': function () { | |
this.emit(':tell', 'ごきげんよう。'); | |
}, | |
}; | |
// Alexaからのリクエストはeventに含まれます | |
exports.handler = function (event, context, callback) { | |
// alexa-sdkをセットアップします | |
const alexa = Alexa.handler(event, context, callback); | |
// Alexa Skill IDをここで定義します | |
alexa.appId = APP_ID; | |
// 作成したレスポンスを登録します | |
alexa.registerHandlers(handlers); | |
// execute()を実行することで、リクエストに対応したレスポンスを返します | |
alexa.execute(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment