見出し画像

【EP1-3】Youtube先生に聞く

公式のYoutubeなども出ているAlexaスキルの動画
公式は段階的になっていたので、とりあえず私は公式を見ずに
一般の方が登校しているHow toから学ぶことにしました。
C言語のHello Worldもとりあえず見様見真似から始めるもんですからね。多くのソースコード全体を確認できる動画を探しました。

【アレクサスキル開発】超簡単やまびこスキルを作るよ!

こちらのやまびこ君をまねしてやったのが最初だと思います。

私が始めたころはPythonのコーディング動画はなく、node.js(言語)で書かれているものでした。最初は雰囲気をつかむためこちらの言ったことを返すやまびこ君がいいと思い真似をしてやってみました。
やまびこ君は本当に言った内容返すだけのシンプルなスキルです。

「おおー、出来た出来た」

右も左もまだまだ分かりませんが、自分のAmazon Echoで反応が返ってくるのはうれしいものです。

ここからAlexaスキル作成が始まりました。
ほぼ当時作ったままのものを公開します。(node.jsで作成しています)

まずは動画をみて自分で作ってみて、うまく出来なかったら下記のコードを参考にしてみてください。

index.js

// This sample demonstrates handling intents from an Alexa skill using the Alexa Skills Kit SDK (v2).
// Please visit https://alexa.design/cookbook for additional examples on implementing slots, dialog management,
// session persistence, api calls, and more.
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 YamabikoIntentHandler = {
   canHandle(handlerInput) {
       return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
           && Alexa.getIntentName(handlerInput.requestEnvelope) === 'YamabikoIntent';
   },
   handle(handlerInput) {
       //const speakOutput = 'Hello World!';
       // ユーザの言った言葉
       const sentence = handlerInput.requestEnvelope.request.intent.slots.SENTENCE_SLOT.value
       return handlerInput.responseBuilder
           .speak(sentence + 'ナリ')
           //何回も繰り返すためここに再度繰り返す言葉をいれる
           .reprompt('何か言ってください')
           .getResponse();
   }
};
const HelpIntentHandler = {
   canHandle(handlerInput) {
       return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
           && Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.HelpIntent';
   },
   handle(handlerInput) {
       const speakOutput = 'You can say hello to me! How can I help?';

       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 = 'Goodbye!';
       return handlerInput.responseBuilder
           .speak(speakOutput)
           .getResponse();
   }
};
const SessionEndedRequestHandler = {
   canHandle(handlerInput) {
       return Alexa.getRequestType(handlerInput.requestEnvelope) === 'SessionEndedRequest';
   },
   handle(handlerInput) {
       // Any cleanup logic goes here.
       return handlerInput.responseBuilder.getResponse();
   }
};

// The intent reflector is used for interaction model testing and debugging.
// It will simply repeat the intent the user said. You can create custom handlers
// for your intents by defining them above, then also adding them to the request
// handler chain below.
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)
           //.reprompt('add a reprompt if you want to keep the session open for the user to respond')
           .getResponse();
   }
};

// Generic error handling to capture any syntax or routing errors. If you receive an error
// stating the request handler chain is not found, you have not implemented a handler for
// the intent being invoked or included it in the skill builder below.
const ErrorHandler = {
   canHandle() {
       return true;
   },
   handle(handlerInput, error) {
       console.log(`~~~~ Error handled: ${error.stack}`);
       const speakOutput = `Sorry, I had trouble doing what you asked. Please try again.`;

       return handlerInput.responseBuilder
           .speak(speakOutput)
           .reprompt(speakOutput)
           .getResponse();
   }
};

// The SkillBuilder acts as the entry point for your skill, routing all request and response
// payloads to the handlers above. Make sure any new handlers or interceptors you've
// defined are included below. The order matters - they're processed top to bottom.
exports.handler = Alexa.SkillBuilders.custom()
   .addRequestHandlers(
       LaunchRequestHandler,
       YamabikoIntentHandler,
       HelpIntentHandler,
       CancelAndStopIntentHandler,
       SessionEndedRequestHandler,
       IntentReflectorHandler, // make sure IntentReflectorHandler is last so it doesn't override your custom intent handlers
   )
   .addErrorHandlers(
       ErrorHandler,
   )
   .lambda();

この記事が気に入ったらサポートをしてみませんか?