Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@furuya02
Created October 26, 2018 16:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save furuya02/7553721b79ee6e54864878270af22cc3 to your computer and use it in GitHub Desktop.
Save furuya02/7553721b79ee6e54864878270af22cc3 to your computer and use it in GitHub Desktop.
const Alexa = require('ask-sdk');
const mp3 = "https://exsample.com/audio001.mp3";
let skill;
exports.handler = async function (event, context) {
if (!skill) {
skill = Alexa.SkillBuilders.standard()
.addRequestInterceptors(RequestInterceptor)
.addRequestHandlers(
PlayIntentHandler,
PauseIntentHandler,
ResumeIntentHandler,
PlaybackNearlyFinishedHandler,
PlaybackHandler,
StopCancelIntentHandler)
.create();
}
return skill.invoke(event);
}
// ログ取得のため
const RequestInterceptor = {
process(h) {
console.log(JSON.stringify(h.requestEnvelope))
}
};
// 再生
const PlayIntentHandler = {
canHandle(h) {
return isMatch(h, 'LaunchRequest','PlayIntent');
},
async handle(h) {
const url = mp3;
const token = mp3;
return h.responseBuilder
.addAudioPlayerPlayDirective('REPLACE_ALL', url, token, 0, null)
.speak('オーディオ・サンプルを開始します')
.getResponse();
}
};
// 中断
const PauseIntentHandler = {
canHandle(h) {
return isMatch(h, 'AMAZON.PauseIntent');
},
async handle(h) {
return h.responseBuilder
.addAudioPlayerStopDirective()
.speak('オーディオ・サンプルを中断します')
.getResponse();
}
};
// 再開
const ResumeIntentHandler = {
canHandle(h) {
return isMatch(h, 'AMAZON.ResumeIntent');
},
async handle(h) {
const url = mp3;
const AudioPlayer = h.requestEnvelope.context.AudioPlayer;
const token = AudioPlayer.token;
const offset = AudioPlayer.offsetInMilliseconds;
return h.responseBuilder
.addAudioPlayerPlayDirective('REPLACE_ALL', url, token, offset, null)
.speak('オーディオ・サンプルを再開します')
.getResponse();
}
};
// 連続再生
const PlaybackNearlyFinishedHandler = {
canHandle(h) {
return isMatch(h, 'AudioPlayer.PlaybackNearlyFinished');
},
async handle(h) {
const url = mp3;
const token = mp3;
const AudioPlayer = h.requestEnvelope.context.AudioPlayer;
const expectedPreviousToken = AudioPlayer.token;
return h.responseBuilder
.addAudioPlayerPlayDirective('ENQUEUE', url, token, 0, expectedPreviousToken)
.getResponse();
}
};
// 終了
const StopCancelIntentHandler = {
canHandle(h) {
return isMatch(h ,'AMAZON.CancelIntent', 'AMAZON.StopIntent');
},
handle(h) {
return h.responseBuilder
.addAudioPlayerStopDirective()
.speak('オーディオ・サンプルを終了します')
.getResponse();
}
};
// 以下のイベントは処理しない
const PlaybackHandler = {
canHandle(h) {
return isMatch(h,'AudioPlayer.PlaybackStarted',
'AudioPlayer.PlaybackFinished',
'AudioPlayer.PlaybackStopped',
'AudioPlayer.PlaybackFailed');
},
async handle(h) {
return h.responseBuilder.getResponse();
}
};
// インテント名・タイプのマッチング用のヘルパー
isMatch = function(h, ...intents) {
return intents.some( intent => {
if (intent == 'SessionEndedRequest' || intent == 'LaunchRequest') {
return h.requestEnvelope.request.type == intent;
}
if (h.requestEnvelope.request.type == 'IntentRequest') {
return h.requestEnvelope.request.intent.name === intent
}
if (h.requestEnvelope.request.type.indexOf('AudioPlayer.') === 0) {
return h.requestEnvelope.request.type === intent
}
return false;
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment