概要
node.jsでalexaスキル、やってみた。
練習問題やってみた。
練習問題
Alexa-hostedで、暗算、インテントを追加せよ。
方針
- attributesManagerを使う。
LaunchRequest
「暗算ゲームをはじめます。それでは問題です。20足す40は?」
AnswerIntent
「60だぜ」
「すごい正解です。20足す40は60です。よくわかりましたね!」
「70だぜ」
「残念20足す40の答えは60です。」
写真
アンサー、インテントを追加
-
対話モデル>インテント>インテントを追加>カスタムインテントを作成
AnswerIntent -
対話モデル>インテント>インテントを追加>サンプル発話
" {ans} だぜ" -
対話モデル>インテント>インテントを追加>インテントスロット
ans AMAZON.Number -
対話モデル>インテント>インテントを追加>保存>スキルをビルド
サンプルコード
const LaunchRequestHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'LaunchRequest';
},
async handle(handlerInput) {
let attributes = handlerInput.attributesManager.getSessionAttributes();
const min = Math.ceil(1);
const max = Math.floor(30);
const a = Math.floor(Math.random() * (max - min) + min);
const b = Math.floor(Math.random() * (max - min) + min);
attributes.a = a;
attributes.b = b;
handlerInput.attributesManager.setSessionAttributes(attributes);
let speechText = '暗算ゲームを始めます。それでは問題です。' + attributes.a + '足す' + attributes.b + 'は?';
return handlerInput.responseBuilder.speak(speechText).reprompt(speechText).getResponse();
}
};
const AnswerIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest' && handlerInput.requestEnvelope.request.intent.name === 'AnswerIntent';
},
handle(handlerInput) {
let attributes = handlerInput.attributesManager.getSessionAttributes();
const a = attributes.a;
const b = attributes.b;
const ans = handlerInput.requestEnvelope.request.intent.slots.ans.value;
let speechText = '';
const v = a + b;
if (v === parseInt(ans))
{
speechText = 'すごい!正解です!' + a + ' 足す ' + b + 'は ' + v + 'です。よくわかりましたね!'
}
else
{
speechText = '残念!' + a + ' 足す ' + b + 'は ' + ans + 'ではありません。正解は' + v + 'でした。'
}
return handlerInput.responseBuilder.speak(speechText).reprompt(speechText).getResponse();
}
};
以上。