Amazon Alexaask-utils

ask-utilsで始めるNode.jsでのAlexa Custom Skill作成

Alexaスキル、作ってますか?npm i -S ask-sdkすることで、Node.jsで動くAlexa Custom Skillバックエンドが比較的簡単に作れます。 が、個人的にもう少し痒いところに手が届けばいいなと […]

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

Alexaスキル、作ってますか?npm i -S ask-sdkすることで、Node.jsで動くAlexa Custom Skillバックエンドが比較的簡単に作れます。

が、個人的にもう少し痒いところに手が届けばいいなと思うところがありましたので、ask-utilsというライブラリを作って公開・メンテナンスしています。

ask-utilsをいれると変わること

現在日米合わせて15以上のスキルを公開しています。その中で比較的繰り返して利用することの多い関数をこのライブラリにまとめています。

ランダムな発話の作成

Alexaブログにて、「ランダムな発話を実装することでより人間らしい会話がつくれる」ということが書かれています。

function randomPhrase(myData) {
  // the argument is an array [] of words or phrases
  var i = 0;
  i = Math.floor(Math.random() * myData.length);
  return(myData[i]);
}
const errorMessages = randomPhrase([
  'I can not here what you say, please say again.',
  'Please say again.',
  "I'm sorry, please tell me again"
])

が、この処理を毎回Lambda内で実装するのは非効率です。ということで関数でラッピングしました。

const { getRandomMessage } = require('ask-utils')
const errorMessages = [
  'I can not here what you say, please say again.',
  'Please say again.',
  "I'm sorry, please tell me again"
]

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

新機能のfixtureを提供

先日発表のあったリマインダーAPIのように、SDK側で対応が完了していない処理・機能も時にはでてきます。ask-utilsを使うことで、自力でAPIリクエストを構築しなくとも、fixtureを使うことで比較的簡単にAPIコールが行えます。

const { RemidnerClient } = require('ask-utils')
// PUT new reminder
const client = new RemidnerClient(handlerInput)
const payload = {...}
client.setPayload(payload)
await client.fetch('POST')

// Lists reminder
const client = new RemidnerClient(handlerInput)
const lists = await client.fetch()

// Delete a reminder
const client = new RemidnerClient(handlerInput)
await client.fetch('DELETE', `/v1/alerts/reminders/${id}`)

APIドキュメントも用意しています。

作った本人ですら使い方を忘れることがあります。そのためask-utilsはAPIドキュメントを用意するようにしています。実装のコードサンプルなどもなるべく記載するようにしていますので、ぜひご覧ください。

また、サンプルも用意しています。

みんなもっと使ってくだしあ

より踏み込んだ使い方について

技術書典5で出品した本に踏み込んだユースケースなどを紹介しています。興味がある方はこちらもぜひ。

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

WP Kyotoサポーター募集中

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

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

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

Related Category posts