const Alexa = require('ask-sdk');
const constants = require('./constants');
const speechOutput = 'ウェルカム脳の音楽をシャッフル再生します';
/* audioData
------------------------------------------------------------------------------------------ */
function audioData() {
const no = 0 + Math.floor( Math.random() * constants.audioData.length );
let audioData = {};
audioData.title = constants.audioData[no].title;
audioData.url = constants.audioData[no].url;
return audioData;
}
/* 起動
------------------------------------------------------------------------------------------ */
const GetNewFactHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === 'LaunchRequest'
|| (request.type === 'IntentRequest'
&& request.intent.name === 'GetNewFactIntent');
},
handle(handlerInput) {
const audio = audioData();
return handlerInput.responseBuilder
.speak(speechOutput)
//.withSimpleCard('New', audio.title)
.addAudioPlayerPlayDirective('REPLACE_ALL', audio.url , audio.title , 0, null)
.getResponse();
},
};
/* AudioPlayerEventHandler
------------------------------------------------------------------------------------------ */
const AudioPlayerEventHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type.startsWith('AudioPlayer.');
},
async handle(handlerInput) {
const {
requestEnvelope,
attributesManager,
responseBuilder
} = handlerInput;
const audioPlayerEventName = requestEnvelope.request.type.split('.')[1];
switch (audioPlayerEventName) {
case 'PlaybackStarted':
//responseBuilder.withSimpleCard('Play', 'test');
break;
case 'PlaybackFinished':
break;
case 'PlaybackStopped':
break;
case 'PlaybackNearlyFinished':
{
const audio = audioData();
const token = handlerInput.requestEnvelope.request.token;
responseBuilder.addAudioPlayerPlayDirective('ENQUEUE', audio.url , audio.title , 0, token);
break;
}
}
return responseBuilder.getResponse();
},
};
/* PlayIntentHandler
------------------------------------------------------------------------------------------ */
const PlayIntentHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return (request.type === 'IntentRequest' && request.intent.name === 'PlayIntent');
},
async handle(handlerInput) {
const audio = audioData();
return handlerInput.responseBuilder
.speak(speechOutput)
//.withSimpleCard('Play', audio.title)
.addAudioPlayerPlayDirective('REPLACE_ALL', audio.url , audio.title , 0, null)
.getResponse();
}
};
/* PauseIntentHandler
------------------------------------------------------------------------------------------ */
const PauseIntentHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return (request.type === 'IntentRequest' && request.intent.name === 'AMAZON.PauseIntent');
},
async handle(handlerInput) {
return handlerInput.responseBuilder
.addAudioPlayerStopDirective()
.getResponse();
}
};
/* ResumeIntentHandler
------------------------------------------------------------------------------------------ */
const ResumeIntentHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return (request.type === 'IntentRequest' && request.intent.name === 'AMAZON.ResumeIntent');
},
async handle(handlerInput) {
const AudioPlayer = handlerInput.requestEnvelope.context.AudioPlayer;
const offset = AudioPlayer.offsetInMilliseconds;
const audio = audioData();
return handlerInput.responseBuilder
.speak(speechOutput)
//.withSimpleCard('Resume', audio.title)
.addAudioPlayerPlayDirective('REPLACE_ALL', audio.url , audio.title , offset, null)
.getResponse();
}
};
/* NextIntentHandler
------------------------------------------------------------------------------------------ */
const NextIntentHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return (request.type === 'IntentRequest' && request.intent.name === 'AMAZON.NextIntent');
},
async handle(handlerInput) {
const audio = audioData();
return handlerInput.responseBuilder
//.withSimpleCard('Next', audio.title)
.addAudioPlayerPlayDirective('REPLACE_ALL', audio.url , audio.title , 0, null)
.getResponse();
}
};
const HelpHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === 'IntentRequest'
&& request.intent.name === 'AMAZON.HelpIntent';
},
handle(handlerInput) {
return handlerInput.responseBuilder
.speak(HELP_MESSAGE)
.reprompt(HELP_REPROMPT)
.getResponse();
},
};
const ExitHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === 'IntentRequest'
&& (request.intent.name === 'AMAZON.CancelIntent'
|| request.intent.name === 'AMAZON.StopIntent');
},
handle(handlerInput) {
return handlerInput.responseBuilder
.addAudioPlayerStopDirective()
.speak(STOP_MESSAGE)
.getResponse();
},
};
const SessionEndedRequestHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === 'SessionEndedRequest';
},
handle(handlerInput) {
console.log(`Session ended with reason: ${handlerInput.requestEnvelope.request.reason}`);
return handlerInput.responseBuilder.getResponse();
},
};
const ErrorHandler = {
canHandle() {
return true;
},
handle(handlerInput, error) {
console.log(`Error handled: ${error.message}`);
return handlerInput.responseBuilder
.speak('その機能はありません、ごめんね')
.getResponse();
},
};
//const SKILL_NAME = 'Space Facts';
//const GET_FACT_MESSAGE = 'Here\'s your fact: ';
const HELP_MESSAGE = 'You can say tell me a space fact, or, you can say exit... What can I help you with?';
const HELP_REPROMPT = 'What can I help you with?';
const STOP_MESSAGE = 'またね!';
const skillBuilder = Alexa.SkillBuilders.standard();
exports.handler = skillBuilder
.addRequestHandlers(
GetNewFactHandler,
PlayIntentHandler,
PauseIntentHandler,
ResumeIntentHandler,
NextIntentHandler,
HelpHandler,
ExitHandler,
AudioPlayerEventHandler,
SessionEndedRequestHandler
)
.addErrorHandlers(ErrorHandler)
.lambda();