Edited at

ask-sdkの俺的テンプレ(2)

More than 1 year has passed since last update.

俺ようです。3日に1回くらい編集するかも

npm install --save ask-sdk

npm install --save ask-auxiliary

nodeがv8.10である必要あり

index.js

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

const other = require('./other.js');
const askAuxiliary = require('ask-auxiliary');

let skill;
exports.handler = async function (event, context) {
if (!skill) {
skill = Alexa.SkillBuilders.standard()
.addRequestHandlers(
LaunchRequestHandler,
StopHandler)
.create();
}
return skill.invoke(event);
}

// スキル起動時の処理
const LaunchRequestHandler = {
canHandle(handlerInput) {
return askAuxiliary.isLaunchRequest(handlerInput);
// return handlerInput.requestEnvelope.request.type === 'LaunchRequest';
},
async handle(handlerInput) {
var msg = "";
return handlerInput.responseBuilder.speak(msg).reprompt(msg).getResponse();
}
};

const sampleIntentHandler = {
canHandle(handlerInput) {
return askAuxiliary.isIntent(handlerInput, "sampleIntent");
//return handlerInput.requestEnvelope.request.type === 'IntentRequest'
// && handlerInput.requestEnvelope.request.intent.name === 'sampleIntent';

},
async handle(handlerInput) {
var msg = "自分で定義したインテントの時に呼ばれます";
return handlerInput.responseBuilder.speak(msg).reprompt(msg).getResponse();
}
};

other.js

module.exports = {

HelpIntentHandler: {
canHandle(handlerInput) {
return askAuxiliary.isIntent(handlerInput, "getPlaceIntent");
// return handlerInput.requestEnvelope.request.type === 'IntentRequest'
// && handlerInput.requestEnvelope.request.intent.name === 'AMAZON.HelpIntent';
},
handle(handlerInput) {
var msg = 'ヘルプって言われた時に発動するよ';

return handlerInput.responseBuilder
.speak(msg)
.reprompt(msg)
.withSimpleCard('Hello World', msg)
.getResponse();
},
},

CancelStopIntentHandler: {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& (handlerInput.requestEnvelope.request.intent.name === 'AMAZON.CancelIntent'
|| handlerInput.requestEnvelope.request.intent.name === 'AMAZON.StopIntent');
},
handle(handlerInput) {
var msg = 'ストップ、キャンセル';

return handlerInput.responseBuilder
.speak(msg)
.reprompt(msg)
.withSimpleCard('Hello World', msg)
.getResponse();
},
},

SessionEndedRequestHandler: {
canHandle(handlerInput) {
return askAuxiliary.isSessionEndedRequest(handlerInput);
//return handlerInput.requestEnvelope.request.type === 'SessionEndedRequest';
},
handle(handlerInput) {
return handlerInput.responseBuilder.getResponse();
},
},

// v1のunhandledと同等。
ErrorHandler: {
canHandle() {
return true;
},
handle(handlerInput, error) {
return handlerInput.responseBuilder
.speak('もう一度おねがいします')
.getResponse();
},
},
}