Alexaスキルの開発を効率化するBespoken Toolsを試してみた

Amazon Alexaスキルの開発を効率化したり、AlexaスキルのモニタリングができるBeSpoken社のサービスのうち、BeSpoken Toolsについて調べました。;
2018.07.11

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

大阪の方の持田です。今回は、Alexaスキルの開発を効率化したり、その後のモニタリングやテストに使えるBespoken社のサービスのうち、CLIツールであるBespoken Toolsをざっくり試した結果をご紹介します。

Bespoken Toolsとは

Bespoken Toolsは、Bespoken社によるAlexaスキルの開発を効率化するサービスです。 Bespoken社は、会社概要によると、南米ペルーの企業です。AlexaスキルやGoogle Homeのスキルに対して、スキル開発を効率化するツールや、スキルのモニタリング、バリデーション、ロギングが可能なダッシュボード提供しています。

これらのサービスは、Alexaスキルの呼び出し先(エンドポイント)を、同社のプロキシーサーバーに設定することで実現されています。

BSTコマンドのシステム構成図 http://docs.bespoken.io/en/latest/alexa_skills_kit_configuration/ に掲載の図

今回は、このコマンドを使うことで、ローカルで開発しているlambdaの修正を、再デプロイすることなしにAlexaコンソールでテストすることができます。

前提条件

Bespoken Toolsを試すため、以下の環境を用意しました。

  • node.jsがインストールされている
  • ASK CLIがセットアップされている
  • AWS LambdaがデプロイできるAWSアカウント(IAMユーザーおよびクレデンシャル)

ASK CLIでのデプロイ

はじめに、Bespoken Toolsを使わず、ASK CLIでデプロイしてみます。 Bespoken Toolsと比較するため、デプロイの時間を計測しておきます。 引数などの説明を簡略化するため、以下ASK CLIで、バージニアリージョンに英語のスキル"Hello World"をデプロイします。 また、各コマンドをtimeコマンドを使って処理時間を計測します。

$ time ask new --skill-name "Hello World"
New project for Alexa skill created.

real	0m3.621s
user	0m2.226s
sys	0m0.409s

ローカルマシンにスキルを新規作成するのは3秒あまりでした。引き続き、このままデプロイしてみます。

$ cd Hello\ World/
$ time ask deploy
-------------------- Create Skill Project --------------------
Profile for the deployment: [default]
Skill Id: amzn1.ask.skill.xxxxxxxxxxxxxxxxxxxx
Skill deployment finished.
Model deployment finished.
Lambda deployment finished.
Lambda function(s) created:
  [Lambda ARN] arn:aws:lambda:us-east-1:xxxxxxxxxxxxxxxxxxxx:function:ask-custom-Hello-World-default

real	0m54.129s
user	0m1.572s
sys	0m0.248s

初期のスキルのデプロイに54秒あまりかかりました。 少しlambdaのコードを書き換えてみます。

   handle(handlerInput) {
     const speechText = 'Welcome to the Alexa Skills Kit, you can say hello!';

最後の "hello!" を "hello world!" に修正します。

   handle(handlerInput) {
     const speechText = 'Welcome to the Alexa Skills Kit, you can say hello world!';

以上のように書き換えました。 再度、今度はlambdaだけをデプロイしてみます。

$ time ask deploy -t lambda
Lambda deployment finished.
Lambda function(s) updated:
  [Lambda ARN] arn:aws:lambda:us-east-1:xxxxxxxxxxxxxxxxxxxx:function:ask-custom-Hello-World-default

real	0m5.774s
user	0m1.140s
sys	0m0.178s

5秒かかりました。 今後、lambdaのコードを修正して、Alexaコンソールでテストするたびに、デプロイに5秒待つことになります。 そこで、Bespoken Toolsを使ってみます。

Bespoken Toolsのセットアップ

ドキュメントの通り、npmにてインストールを行います。

$ npm install bespoken-tools -g

私はnodebrewを使っているので、ドキュメントの通り-gオプションを付加しました。

bstコマンドの出力を確認します。

$ bst
BST: v1.2.11  Node: v8.11.3


  Usage: bst [options] [command]

  Options:

    -h, --help                               output usage information

  Commands:

    proxy <lambda|function|http>             Proxies a Lambda, Google Cloud Function or HTTP service
    launch                                   Sends a launch request to your service
    intend <intent> [SlotName=SlotValue...]  Sends the specified intent to your service
    utter <utterance>                        Sends an intent with the specified utterance to your service
    sleep <location>                         Instructs bst to sleep using specified location
    deploy <lambda>                          Deploys a lambda
    speak <utterance>                        Sends your message to your virtual alexa device
    help [cmd]                               display help for [cmd]

問題なさそうです。次に進みます

Bespoken Toolsの実行とコードの修正

まず、先ほどの"Hello World"スキル内の./lambda/custom/ディレクトリに移動して、bstコマンドを実行します。

$ cd lambda/custom/
$ bst proxy lambda index.js
BST: v1.2.11  Node: v8.11.3

Your public URL for accessing your local service:
https://xxxxxxxxxx.bespoken.link

Your URL for viewing requests/responses sent to your service:
https://apps.bespoken.io/dashboard?id=xxxxxxxxxx&key=xxxxxxxxxx
Copy and paste this to your browser to view your transaction history and summary data.

INFO  2018-07-06T06:37:41.308Z Connected - proxy.bespoken.tools:5000

これでプロキシが起動しました。このコンソールはプロキシの動作ログなどが表示されるので、以後別のコンソールで作業します。

次に、"Hello World"スキル内のコードを少し修正します。 まずはskill.jsonです。

skill.json

    "apis": {
      "custom": {
        "endpoint": {
          "sourceDir": "lambda/custom"
        }
      }
    },

ここのsourceDirを、さきほどbstコマンドの最初の出力にあったプロキシのURIに書き換えます。 そのほか、ワイルドカード証明書を受け入れる設定を加えて、次のようになります。

skill.json

    "apis": {
      "custom": {
        "endpoint": {
          "uri": "https://xxxxxxxxxx.bespoken.link",
          "sslCertificateType": "Wildcard"
        }
      }
    },

この skill.jsonの構文については、スキルマニフェスト(スキル管理API)をご参照ください。

次に、.ask/configです。

.ask/config

{
  "deploy_settings": {
    "default": {
      "skill_id": "amzn1.ask.skill.xxxxxxxxxxxxxxxxxxxx",
      "was_cloned": false,
      "merge": {
        "manifest": {
          "apis": {
            "custom": {
              "endpoint": {
                "uri": "ask-custom-Hello-World-default"
              }
            }
          }
        }
      },

このmergeブロックを全削除します。

.ask/config

{
  "deploy_settings": {
    "default": {
      "skill_id": "amzn1.ask.skill.xxxxxxxxxxxxxxxxxxxx",
      "was_cloned": false

修正しましたので、スキル全体を一度デプロイします。

$ time ask deploy
-------------------- Update Skill Project --------------------
Skill Id: amzn1.ask.skill.xxxxxxxxxxxxxxxxxxxx
Skill deployment finished.
Model deployment finished.
[Info]: No lambda functions need to be deployed.

real    0m42.224s
user    0m1.133s
sys     0m0.184s

出力の通り、Lambdaのコードは修正していません。 いったん、ここでAlexaコンソールでテストをしてみます。

このサンプルスキルの呼び出し名は "greeter"です。 まずは、現状のままでテストを有効にして "open greeter" と呼び出してみます。

最初の呼び出しのスクリーンショット

"Welcome to the Alexa Skills Kit, you can say hello world!"と返ってきました。先程の修正が反映されています。

次に、Lambdaのコードを修正します。"hello world!" を "hello proxy!" に変更してみます。

   handle(handlerInput) {
     const speechText = 'Welcome to the Alexa Skills Kit, you can say hello proxy!';

通常であれば、このあと ask deploy -t lambda を実行して5秒待たなければAlexaコンソールではテストができませんが、そのままでコンソールからスキルを呼び出してみます。 ただ、今回は修正したコードが Launch Request のため、いったん "quit" とスキルを終了させています。

修正後の呼び出しのスクリーンショット

"Welcome to the Alexa Skills Kit, you can say hello proxy!"と返ってきました。デプロイせずに、修正したコードをテストできています。

おわりに

今回は、Bespoken Toolsのごく基本的な使い方をみてみました。ASK CLIと重なる機能もありますが、開発が効率化する面もありますので、もう少し調べてみようと思います。