LoginSignup
0
0

node.jsでalexaスキル その8

Last updated at Posted at 2024-04-19

概要

node.jsでalexaスキル、やってみた。
練習問題やってみた。

練習問題

Alexa-hostedで、暗算、インテントを追加せよ。

方針

  • attributesManagerを使う。

LaunchRequest
「暗算ゲームをはじめます。それでは問題です。20足す40は?」

AnswerIntent
「60だぜ」

「すごい正解です。20足す40は60です。よくわかりましたね!」

「70だぜ」

「残念20足す40の答えは60です。」

写真

スクリーンショット 2024-04-20 065343.png

アンサー、インテントを追加

  • 対話モデル>インテント>インテントを追加>カスタムインテントを作成
    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();
	}
};

以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0