Amazon Alexaask-sdkask-utilsProductstools

ask-sdkをもっと手軽に使いたかったので、ask-utilsというライブラリを作りました

いろいろ触っていると、汎用的に使いたい処理が増えてきましたので、パッケージ化してみました。 やりたかったこと ask-sdkの場合、以下の例のようにhandlerを実行するか否かをcanHandle()で判定します。 こ […]

広告ここから
広告ここまで

いろいろ触っていると、汎用的に使いたい処理が増えてきましたので、パッケージ化してみました。

やりたかったこと

ask-sdkの場合、以下の例のようにhandlerを実行するか否かをcanHandle()で判定します。

const HelpIntentHandler = {
  canHandle (handlerInput) {
    return handlerInput.requestEnvelope.request.type === 'IntentRequest' &&
      handlerInput.requestEnvelope.request.intent.name === 'AMAZON.HelpIntent'
  },
  handle (handlerInput) {
    const speechText = 'This is example skill'

    return handlerInput.responseBuilder
      .speak(speechText)
      .reprompt(speechText)
      .withSimpleCard(SKILL_NAME, speechText)
      .getResponse()
  }
}

これはわかりやすくていいなと思うのですが、どうもreturn handlerInput.requestEnvelope.request.type === 'IntentRequest' && handlerInput.requestEnvelope.request.intent.name === 'AMAZON.HelpIntent'という書き方が冗長でやだなぁと思っていました。

どのHandlerも似たような分岐をかけるので、「じゃあラッパー的な関数作って処理しちゃえばよくね?」ということで作ってみました。

作ったもの

ask-utilsという割と直球な名前です。自分がスキルを作るときに必要になったものをガンガンここに追加していきたいなと思います。

インストール方法

npmで公開済みなので、npm i -S ask-utilsでインストールできます。

GitHubに開発版を置いていますので、そちらも使えます。が、さすがに開発版の動作保証まではできないので、自己責任でお願いします。

ask-utilsでできること

さっきのコードサンプルがこう変わります。

const { intentHandlers } = require('ask-utils')

const HelpIntentHandler = {
  canHandle (handlerInput) {
    return intentHandlers.canHandle(handlerInput, 'IntentRequest', 'AMAZON.HelpIntent')
  },
  handle (handlerInput) {
    const speechText = 'This is example skill'

    return handlerInput.responseBuilder
      .speak(speechText)
      .reprompt(speechText)
      .withSimpleCard(SKILL_NAME, speechText)
      .getResponse()
  }
}

canHandle()がだいぶスッキリしました。intentHandlers.canHandle(handlerInput, REQUEST_TYPE, INTENT_NAME)と書くことで、リクエストタイプとインテント名が一致しているかどうかを判定してくれます。

LaunchRequestSessionEndedRequestなどIntentRequest以外のリクエストタイプの場合は第三引数を省略すればOKです。

const SessionEndedRequestHandler = {
  canHandle (handlerInput) {
    return intentHandlers.canHandle(handlerInput, 'SessionEndedRequest')
  },
  handle (handlerInput) {
    console.log(`Session ended with reason: ${handlerInput.requestEnvelope.request.reason}`)

    return handlerInput.responseBuilder.getResponse()
  }
}

そのほか

「Alexaからのレスポンスは、ランダムにしてやったほうが会話っぽくていいよ(意訳)」な記事がAmazonに出ていましたので、これもラッパー化しておきました。

One way to add variety to your skill is have your skill choose a random word or phrase from a list of welcome greetings, re-prompts, confirmations, etc. For example, think of the many different ways Alexa can say “Hello” (think: “howdy”, “hi”, “good day”), or the many ways Alexa can say “OK” (think: “Got it,” “Thanks,” “Sounds good,” “Great,” and so on). You can use these opportunities to inject variety, color, and humor to your skill. You can even prepare clever responses to customers’ requests for features your skill doesn’t yet support.

Alexa Skill Recipe: Randomize Your Responses to Add Variety to Your Skill

コードサンプル

const { randomResponse } = require('ask-utils')
const errorMessages = [
  'すみません、よく聞き取れませんでした。もう一度お願いします。',
  'すみません、よく聞き取れませんでした。',
  '上手に聞き取れなくてすみません。もう一度言ってもらえますか?',
  'ごめんなさい。もう一度言ってもらえると助かります。'
]

const ErrorHandler = {
  canHandle () {
    return true
  },
  handle (handlerInput, error) {
    console.log(`Error handled: ${error.message}`)
    const message = randomResponse.getRandomMessage(errorMessages)
    return handlerInput.responseBuilder
      .speak(message)
      .reprompt(message)
      .getResponse()
  }
}

バグ・不具合報告など

GitHubのIssueを立ててもらえると助かります。Pull Requestでもらえるとめちゃ喜びますので、「この機能もほしい!」などもくださいませm(_ _)m

ブックマークや限定記事(予定)など

WP Kyotoサポーター募集中

WordPressやフロントエンドアプリのホスティング、Algolia・AWSなどのサービス利用料を支援する「WP Kyotoサポーター」を募集しています。
月額または年額の有料プランを契約すると、ブックマーク機能などのサポーター限定機能がご利用いただけます。

14日間のトライアルも用意しておりますので、「このサイトよく見るな」という方はぜひご検討ください。

広告ここから
広告ここまで

Related Category posts