Skip to content

Instantly share code, notes, and snippets.

@kencharos
Last active August 15, 2017 11:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kencharos/0701ab164c36066247459dfa53af4f6a to your computer and use it in GitHub Desktop.
Save kencharos/0701ab164c36066247459dfa53af4f6a to your computer and use it in GitHub Desktop.
'use strict';
/**
* Hook.io にて、 Alexa Skill を実行するサンプルです
*/
/**
* スピーチ内容を作成する関数です。
* 通常、各イベントの処理の最後にbuildResponseと組み合わせて、次のように実行します。
*
* buildResponse({}, buildSpeechResponse("text", true))
*
* @output 読み上げる内容
* @shouldEndSession 会話を終了する場合は true
*
*/
function buildSpeechResponse(output, shouldEndSession) {
return {
outputSpeech: {
type: 'PlainText',
text: output,
},
shouldEndSession,
};
}
// 最終結果を生成する関数です
function buildResponse(sessionAttributes, speechletResponse) {
return {
version: '1.0',
sessionAttributes:sessionAttributes,
response: speechletResponse,
};
}
// --------------- 各イベントに対する処理です -----------------------
// 起動イベント
function onLaunch(request, session) {
return buildResponse({}, buildSpeechResponse("Hi, You say start birthday year I am 35 years old.", true))
}
// 汎用イベント
function onDefault(request, session) {
return buildResponse({}, buildSpeechResponse("good bye", true))
}
function onCalcYear(intent, session) {
// 発言中の年の数を取得します
let age = Number(intent.slots.age.value);
// 現在を取得します。
let now = new Date().getFullYear();
// 生まれ年を計算します
let birthYear = now - age;
console.log("onCalcYear end by:" + birthYear);
return buildResponse({},
buildSpeechResponse(`You are born in ${birthYear}. Right?`, true));
}
// Hook.ioから呼び出されるメイン関数です
module['exports'] = function myService (hook) {
let event = hook.params;
console.log(event);
//
var result = null;
if (event.request.type === 'LaunchRequest') {
// スキル名のみで起動した場合に発生します
result = onLaunch(event.request, event.session);
} else if (event.request.type === 'SessionEndedRequest') {
// 時間経過や、shouldEndSessionをtrue で呼び出した場合に発生します。
// 今回のサンプルでは発生しません
result = onDefault(event.request, event.session)
} else if (event.request.type === 'IntentRequest') {
// Intent が発声された場合に発生します。intnet名に応じて、どの処理を行うかを呼び分けます。
let intent = event.request.intent;
let intentName = intent.name;
console.log(intent);
if (intentName === 'calcYear') { // calcYear Intentの音声だと判断した場合ここに来ます
console.log("run calcYear");
// calcYear Intent に対する処理を onCalcYear で行います
result = onCalcYear(intent, event.session);
} else if (intentName === 'AMAZON.StopIntent' || intentName === 'AMAZON.CancelIntent' || intentName === 'AMAZON.HelpIntent') {
// この3つのIntentは必ずデフォルトで定義するIntentです。何もしない場合は、 resultに{}を設定すればOKです・
result = onDefault(intent, event.session)
} else {
// Intentを追加する場合、この else より上にelse if 節を足していきます。
// 上記以外のIntentが発生した場合は異常終了です
console.log("Invalid intent");
throw new Error('Invalid intent');
}
}
// イベント処理の結果を、Alexaに返却します。
console.log("end by:" + result);
hook.res.json(result);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment