Alexa Handlerの移動をできるようにしたい
趣味でAlexa Developer Console(Node.js)でスキル開発をしようとしています。
JavaScript
1const Alexa = require('ask-sdk-core'); 2const Util = require('util.js'); 3 4const LaunchRequestHandler = { 5 canHandle(handlerInput) { 6 return Alexa.getRequestType(handlerInput.requestEnvelope) === 'LaunchRequest'; 7 }, 8 handle(handlerInput) { 9 const speakOutput = `なぞなぞクイズへようこそ。このスキルではなぞなぞを出しますよ?準備はいいですか?`; 10 const reprompt = `始めますか?`; 11 return handlerInput.responseBuilder 12 .speak(speakOutput) 13 .reprompt(reprompt) 14 .getResponse(); 15 } 16}; 17const QuizIntentHandler = { 18 canHandle(handlerInput) { 19 return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest' 20 && (Alexa.getIntentName(handlerInput.requestEnvelope) === 'QuizIntent' 21 || Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.YesIntent'); 22 }, 23 handle(handlerInput) { 24 let speakOutput = '<voice name="Takumi">問題です。'; 25 // Alexa サウンドライブラリを使う場合 26 speakOutput += '<audio src="soundbank://soundlibrary/ui/gameshow/amzn_ui_sfx_gameshow_bridge_02"/>'; 27 // 独自のMP3オーディオファイルを使う場合 (事前にS3のMediaフォルダにファイルをアップロードしておきます) 28 // const url = Util.getS3PreSignedUrl('Media/question_48k.mp3'); 29 // speakOutput += `<audio src="${Alexa.escapeXmlCharacters(url)}"/>`; 30 speakOutput += `かけたり、たったり、つぶしたりするものって、なーんだ?「アレクサ」と言ってから答えを言ってください。</voice><audio src="soundbank://soundlibrary/ui/gameshow/amzn_ui_sfx_gameshow_countdown_loop_32s_full_01"/>`; 31 const reprompt = `まだわからないかな?もう一度。`; 32 return handlerInput.responseBuilder 33 .speak(speakOutput) 34 .reprompt(reprompt + speakOutput) 35 .getResponse(); 36 } 37}; 38 39const AnswerIntentHandler = { 40 canHandle(handlerInput) { 41 return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest' 42 && Alexa.getIntentName(handlerInput.requestEnvelope) === 'AnswerIntent'; 43 }, 44 handle(handlerInput) { 45 const answer = Alexa.getSlotValue(handlerInput.requestEnvelope, 'answer'); 46 47 let speakOutput; 48 let url; 49 if (answer === '時間') { 50 // Alexa サウンドライブラリを使う場合 51 speakOutput = '<audio src="soundbank://soundlibrary/ui/gameshow/amzn_ui_sfx_gameshow_positive_response_01"/>'; 52 // 独自のMP3オーディオファイルを使う場合 (事前にS3のMediaフォルダにファイルをアップロードしておきます) 53 // url = Util.getS3PreSignedUrl('Media/correct_48k.mp3'); 54 // speakOutput = `<audio src="${Alexa.escapeXmlCharacters(url)}"/>`; 55 speakOutput += '正解です。<say-as interpret-as="interjection">やった</say-as>'; 56 } 57 else { 58 // Alexa サウンドライブラリを使う場合 59 speakOutput = '<audio src="soundbank://soundlibrary/ui/gameshow/amzn_ui_sfx_gameshow_negative_response_01"/>'; 60 // 独自のMP3オーディオファイルを使う場合 (事前にS3のMediaフォルダにファイルをアップロードしておきます) 61 // url = Util.getS3PreSignedUrl('Media/incorrect_48k.mp3'); 62 // speakOutput = `<audio src="${Alexa.escapeXmlCharacters(url)}"/>`; 63 speakOutput += '残念。答えは<break time="1s"/>時間です'; 64 } 65 return handlerInput.responseBuilder 66 .speak(speakOutput) 67 .getResponse(); 68 } 69}; 70ー以下略ー
上記のサンプルスキルの「なぞなぞスキル」のように並べたHandlerどおりに順に処理するプログラムを組もう以下のようなプログラムを作成しました。(ハンドラーの名前や読み上げテキストなど変更しているところがあります。)
JavaScript
1const Alexa = require('ask-sdk-core'); 2 3const LaunchRequestHandler = { 4 canHandle(handlerInput) { 5 return Alexa.getRequestType(handlerInput.requestEnvelope) === 'LaunchRequest'; 6 }, 7 handle(handlerInput) { 8 const speakOutput = ‘-へようこそ。今日はどうしますか?'; 9 const reprompt = '今日はどうしますか?' 10 return handlerInput.responseBuilder 11 .speak(speakOutput) 12 .reprompt(reprompt) 13 .getResponse(); 14 } 15}; 16const FirstIntentHandler = { 17 canHandle(handlerInput) { 18 return handlerInput.requestEnvelope.request.type === 'IntentRequest' 19 && handlerInput.requestEnvelope.request.intent.name === 'FirstIntent'; 20 }, 21 handle(handlerInput) { 22 let slots = handlerInput.requestEnvelope.request.intent.slots; 23 let name = slots.name.value; 24 const speakOutput = `${name}は、-です。-しますか?`; 25 .speak(speakOutput) 26 .getResponse(); 27 } 28}; 29const SecondIntentHandler = { 30 canHandle(handlerInput) { 31 return handlerInput.requestEnvelope.request.type === 'IntentRequest' 32 && handlerInput.requestEnvelope.request.intent.name === 'AMAZON.YesIntent'; 33 }, 34 handle(handlerInput) { 35 let speakOutput = '承知しました。-します。'; 36 return handlerInput.responseBuilder 37 .speak(speakOutput) 38 .getResponse(); 39 } 40}; 41const ThirdIntentHandler = { 42 canHandle(handlerInput) { 43 return handlerInput.requestEnvelope.request.type === 'IntentRequest' 44 && handlerInput.requestEnvelope.request.intent.name === 'ThirdIntent'; 45 }, 46 handle(handlerInput) { 47 const speakOutput = '-します。'; 48 return handlerInput.responseBuilder 49 .speak(speakOutput) 50 .getResponse(); 51 } 52}; 53 54const HelpIntentHandler = { 55 canHandle(handlerInput) { 56 return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest' 57 && Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.HelpIntent'; 58 }, 59 handle(handlerInput) { 60 const speakOutput = 'このスキルは-するものです。-ができます。今日はどうしますか?'; 61 const reprompt = '今日はどうしますか?' 62 return handlerInput.responseBuilder 63 .speak(speakOutput) 64 .reprompt(speakOutput) 65 .getResponse(); 66 } 67}; 68const CancelAndStopIntentHandler = { 69 canHandle(handlerInput) { 70 return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest' 71 && (Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.CancelIntent' 72 || Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.StopIntent'); 73 }, 74 handle(handlerInput) { 75 const speakOutput = 'ありがとうございました。またお待ちしております。'; 76 return handlerInput.responseBuilder 77 .speak(speakOutput) 78 .getResponse(); 79 } 80}; 81const SessionEndedRequestHandler = { 82 canHandle(handlerInput) { 83 return Alexa.getRequestType(handlerInput.requestEnvelope) === 'SessionEndedRequest'; 84 }, 85 handle(handlerInput) { 86 // Any cleanup logic goes here. 87 return handlerInput.responseBuilder.getResponse(); 88 } 89}; 90const IntentReflectorHandler = { 91 canHandle(handlerInput) { 92 return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'; 93 }, 94 handle(handlerInput) { 95 const intentName = Alexa.getIntentName(handlerInput.requestEnvelope); 96 const speakOutput = ` ${intentName}というインテントが呼ばれました。`; 97 98 return handlerInput.responseBuilder 99 .speak(speakOutput) 100 //.reprompt('add a reprompt if you want to keep the session open for the user to respond') 101 .getResponse(); 102 } 103}; 104const ErrorHandler = { 105 canHandle() { 106 return true; 107 }, 108 handle(handlerInput, error) { 109 console.log(`~~~~ Error handled: ${error.stack}`); 110 const speakOutput = `すみません。なんだかうまくいかないみたいです。もう一度お試しください。`; 111 112 return handlerInput.responseBuilder 113 .speak(speakOutput) 114 .reprompt(speakOutput) 115 .getResponse(); 116 } 117}; 118exports.handler = Alexa.SkillBuilders.custom() 119 .addRequestHandlers( 120 LaunchRequestHandler, 121 FirstIntentHandler, 122 SecondIntentHandler, 123 ThirdIntentHandler, 124 HelpIntentHandler, 125 CancelAndStopIntentHandler, 126 SessionEndedRequestHandler, 127 IntentReflectorHandler, // make sure IntentReflectorHandler is last so it doesn't override your custom intent handlers 128 ) 129 .addErrorHandlers( 130 ErrorHandler, 131 ) 132 .lambda(); 133
コードエディタで保存しデプロイし、エラーはないにもかかわらず、FirstIntentHandlerで質問するまでの処理できるのですが、SecondIntentHandlerの時に承諾の意味の返答をしても、「ちょっとよくわからなかったです。ごめんなさい。」「ごめんなさい、今はわかりません。」と返されます。
ThirdIntentHandlerは別の処理のものでこれが原因かと思い削除してみても同じ結果でした。
プログラミングは趣味でやっていて、Alexaは触り始めたばかりで、どこがどう違うのかがわからず困っています。
また、もしあるハンドラーの処理の次の処理をこのハンドラーにさせるというような指定ができるような処理があればぜひ教えていただきたいです。
あなたの回答
tips
プレビュー