Created
August 21, 2018 21:34
-
-
Save msato0731/63477f768c962f74019e1f5083d88a29 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'; | |
var Alexa = require('alexa-sdk'); | |
var APP_ID = process.env.ALEXA_APP_ID; | |
var SKILL_NAME = "ベンチプレス計算"; | |
var HELP_MESSAGE = "重量と回数からベンチプレスの最大重量を計算します。"; | |
var HELP_REPROMPT = "重量を教えてください"; | |
var START_MESSAGE = "重量と回数からベンチプレスの最大重量を計算します。重量をkgで教えてください。"; | |
var STOP_MESSAGE = "さようなら"; | |
const UNHANDLED = "聞き取れませんでした。もう一度言ってください。"; | |
// ステートの定義 | |
const states = { | |
START: '_STARTMODE', | |
KG: '_KGMODE', | |
REPS: '_REPSMODE', | |
HELP: '_HELPMODE' | |
}; | |
// Lambda関数のメイン処理 | |
exports.handler = function(event, context, callback) { | |
var alexa = Alexa.handler(event, context);// Alexaオブジェクトのインスタンス生成 | |
alexa.appId = APP_ID; | |
alexa.registerHandlers(newSessionHandlers,StartHandlers,KgHandlers,RepsHandlers,HelpHandlers);// ハンドラの登録 | |
alexa.execute();// インスタンスの実行 | |
}; | |
const newSessionHandlers = { | |
"LaunchRequest": function () { | |
this.handler.state = states.START; | |
this.emitWithState("StartSkill"); | |
}, | |
"AMAZON.HelpIntent": function () { | |
this.handler.state = states.HELP; | |
this.emitWithState("helpTheUser"); | |
}, | |
"Unhandled": function () { | |
const speechOutput = "スタート"; | |
this.emit(":ask", speechOutput); | |
} | |
}; | |
// スタート時のステートハンドラ | |
const StartHandlers = Alexa.CreateStateHandler(states.START,{ | |
"StartSkill": function () { | |
this.handler.state = states.KG; | |
const speechOutput = START_MESSAGE; | |
this.emit(":ask", speechOutput); | |
}, | |
"Unhandled": function () { | |
const speechOutput = UNHANDLED; | |
this.emit(":ask", speechOutput); | |
}, | |
}); | |
//重量のハンドラ | |
const KgHandlers = Alexa.CreateStateHandler(states.KG,{ | |
"BenchCalc": function () { | |
const weight = this.event.request.intent.slots.Number.value; // 重量を取得 | |
this.handler.state = states.REPS; | |
this.attributes['weight'] = weight; // 重量をセッションアトリビュートにセット | |
const speechOutput = "重量は" + weight + "kgですね。回数を教えてください。"; | |
if(weight > 0){ | |
this.emit(':ask', speechOutput); // レスポンスの生成 | |
} | |
else{ | |
this.emit('AMAZON.StopIntent'); | |
} | |
this.emit(":ask", speechOutput); | |
}, | |
"AMAZON.HelpIntent": function () { | |
this.handler.state = states.HELP; | |
this.emitWithState("helpTheUser"); | |
}, | |
'AMAZON.CancelIntent': function () { | |
this.emit(':tell', STOP_MESSAGE); | |
}, | |
'AMAZON.StopIntent': function () { | |
this.emit(':tell', STOP_MESSAGE); | |
}, | |
"Unhandled": function () { | |
const speechOutput = UNHANDLED; | |
this.emit(":ask", speechOutput); | |
}, | |
}); | |
//回数と計算のハンドラ | |
const RepsHandlers = Alexa.CreateStateHandler(states.REPS,{ | |
"BenchCalc": function () { | |
this.handler.state = ''; | |
this.attributes['STATE'] = undefined; | |
const reps = this.event.request.intent.slots.Number.value; // 回数を取得 | |
const weight = this.attributes['weight']; | |
//重量と回数から計算 | |
var percent_table = [1, 0.97, 0.94, 0.92, 0.89, 0.86, 0.83, 0.81, 0.78, 0.75, | |
0.73, 0.71, 0.70, 0.68, 0.67, 0.65, 0.64, 0.63, 0.61, 0.60, | |
0.59, 0.58, 0.57, 0.56, 0.55, 0.54, 0.53, 0.52, 0.51, 0.50]; | |
const maxbench = weight / percent_table[reps - 1]; //max重量 = 重さ / 係数 | |
const result = Math.round(maxbench); | |
const speechOutput = ""; | |
if(isNaN(result)){ //回数に正しい値(1~30)が入っているかを判定 | |
speechOutput = "0以上30以下の回数で初めから、やり直してください"; | |
} | |
else{ | |
speechOutput = "あなたの最大重量は" + result + "kgです"; | |
} | |
this.emit(':tell', speechOutput); | |
}, | |
"AMAZON.HelpIntent": function () { | |
this.handler.state = states.HELP; | |
this.emitWithState("helpTheUser"); | |
}, | |
'AMAZON.CancelIntent': function () { | |
this.emit(':tell', STOP_MESSAGE); | |
}, | |
'AMAZON.StopIntent': function () { | |
this.emit(':tell', STOP_MESSAGE); | |
}, | |
"Unhandled": function () { | |
const speechOutput = UNHANDLED; | |
this.emit(":ask", speechOutput); | |
}, | |
}); | |
const HelpHandlers = Alexa.CreateStateHandler(states.HELP,{ | |
"helpTheUser": function () { | |
const speechOutput = HELP_MESSAGE; | |
this.emit(":ask", speechOutput); | |
}, | |
"Unhandled": function () { | |
const speechOutput = "ヘルプ"; | |
this.emit(":ask", speechOutput); | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment