0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

node.jsでalexaスキル その2

Posted at

概要

node.jsでalexaスキル、やってみた。
サンプルを日本語化してみた。

サンプルコード

const Alexa = require('ask-sdk-core');

const LaunchRequestHandler = {
	canHandle(handlerInput) {
		return Alexa.getRequestType(handlerInput.requestEnvelope) === 'LaunchRequest';
	},
	handle(handlerInput) {
		const speakOutput = 'ようこそ、こんにちは、または説明と言ってください。 どれにしますか?';
		return handlerInput.responseBuilder.speak(speakOutput)
			.reprompt(speakOutput)
			.getResponse();
	}
};
const HelloWorldIntentHandler = {
	canHandle(handlerInput) {
		return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
			&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'HelloWorldIntent';
	},
	handle(handlerInput) {
		const speakOutput = 'こんにちは、アレクサスキルの世界へようこそ!';
		return handlerInput.responseBuilder.speak(speakOutput)
			.getResponse();
	}
};
const HelpIntentHandler = {
	canHandle(handlerInput) {
		return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
			&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.HelpIntent';
	},
	handle(handlerInput) {
		const speakOutput = 'これは、おひさまが作ったアレクサのスキルです。';
		return handlerInput.responseBuilder.speak(speakOutput)
			.reprompt(speakOutput)
			.getResponse();
	}
};
const CancelAndStopIntentHandler = {
	canHandle(handlerInput) {
		return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
			&& (Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.CancelIntent'
				|| Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.StopIntent');
	},
	handle(handlerInput) {
		const speakOutput = 'さようなら';
		return handlerInput.responseBuilder.speak(speakOutput)
			.getResponse();
	}
};
const FallbackIntentHandler = {
	canHandle(handlerInput) {
		return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
			&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.FallbackIntent';
	},
	handle(handlerInput) {
		const speakOutput = 'すみません、よく聞き取れませんでした。';
		return handlerInput.responseBuilder.speak(speakOutput)
			.reprompt(speakOutput)
			.getResponse();
	}
};
const SessionEndedRequestHandler = {
	canHandle(handlerInput) {
		return Alexa.getRequestType(handlerInput.requestEnvelope) === 'SessionEndedRequest';
	},
	handle(handlerInput) {
		console.log(`~~~~ Session ended: ${JSON.stringify(handlerInput.requestEnvelope)}`);
		return handlerInput.responseBuilder.getResponse();
	}
};
const IntentReflectorHandler = {
	canHandle(handlerInput) {
		return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest';
	},
	handle(handlerInput) {
		const intentName = Alexa.getIntentName(handlerInput.requestEnvelope);
		const speakOutput = `You just triggered ${intentName}`;
		return handlerInput.responseBuilder.speak(speakOutput)
			.getResponse();
	}
};
const ErrorHandler = {
	canHandle() {
		return true;
	},
	handle(handlerInput, error) {
		const speakOutput = 'すみません、よく聞き取れませんでした。';
		console.log(`~~~~ Error handled: ${JSON.stringify(error)}`);
		return handlerInput.responseBuilder.speak(speakOutput)
			.reprompt(speakOutput)
			.getResponse();
	}
};
exports.handler = Alexa.SkillBuilders.custom()
	.addRequestHandlers(
		LaunchRequestHandler,
		HelloWorldIntentHandler,
		HelpIntentHandler,
		CancelAndStopIntentHandler,
		FallbackIntentHandler,
		SessionEndedRequestHandler,
		IntentReflectorHandler)
	.addErrorHandlers(ErrorHandler)
	.withCustomUserAgent('sample/hello-world/v1.2')
	.lambda();

以上。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?