概要
AlexaをCLIから自発的にしゃべらせるツールに、下記のalexa-remote-controlがある。
上記のコマンドをMacから実行してみたところ下記のようなエラーが発生してしまい、確認したところAlexaに対する認証の箇所で失敗しているようだった。
$ ./alexa_remote_control.sh -a
cookie does not exist. logging in ...
ERROR: Amazon Login was unsuccessful. Possibly you get a captcha login screen.
Try logging in to https://alexa.amazon.co.jp with your browser. In your browser
make sure to have all Amazon related cookies deleted and Javascript disabled!
(For more information have a look at /tmp/.alexa.login)
To avoid issues with captcha, try using Multi-Factor Authentication.
To do so, first set up Two-Step Verification on your Amazon account, then
configure this script (or the environment) with your MFA secret.
Support for Multi-Factor Authentication requires 'oathtool' to be installed.
必要だと言われているouthtool
やjq
コマンドはインストール済みである。
$ oathtool
oathtool 2.6.6
Generate and validate OATH one-time passwords. KEY and OTP is the string '-'
to read from standard input, '@FILE' to read from indicated filename, or a hex
encoded value (not recommended on multi-user systems).
~~省略~~
同じ設定をしたものをラズパイや他のUbuntsu端末で実行したところ問題なく実行できたので、この現象はMacOSのみで発生する問題の可能性が高い。
そこで、alexa-remote-control
をAlphinLinuxのイメージ上で動くdockerコマンド化し、Macでもalexa-remote-controlを実行できるようにした。
実装
環境は以下の通り。
- macOS Catalina(v10.15.7)
- docker: 20.10.7
alexa-remote-controlの準備
はじめに、リポジトリからソースコードをクローンする。
git clone https://github.com/thorsten-gehrig/alexa-remote-control.git
その後alexa-remote-control.sh
の各値を、ユーザの設定に合わせて変更する。
~~省略~~
SET_EMAIL=(Amazonのメールアドレス)
SET_PASSWORD=(Amazonのパスワード)
SET_LANGUAGE=ja-JP
SET_TTS_LOCALE=ja-JP
SET_AMAZON='amazon.co.jp'
SET_ALEXA='alexa.amazon.co.jp'
~~省略~~
(対応必須)ログインページのURLの修正
下記のIssueにあるようにAlexaのログインページのURLが変更になったため、手動で修正の必要がある。
alexa-remote-control.sh
の522行目を下記のように修正の必要がある。
~~省略~~
- ${CURL} ${OPTS} -s -D "${TMP}/.alexa.header" -c ${COOKIE} -b ${COOKIE} -A "${BROWSER}" -H "Accept-Language: ${LANGUAGE}" -H "DNT: 1" -H "Connection: keep-alive" -H "Upgrade-Insecure-Requests: 1" -L https://alexa.${AMAZON} | grep "hidden" | sed 's/hidden/\n/g' | grep "value=\"" | sed -r 's/^.*name="([^"]+)".*value="([^"]+)".*/\1=\2\&/g' >"${TMP}/.alexa.postdata"
+ ${CURL} ${OPTS} -s -D "${TMP}/.alexa.header" -c ${COOKIE} -b ${COOKIE} -A "${BROWSER}" -H "Accept-Language: ${LANGUAGE}" -H "DNT: 1" -H "Connection: keep-alive" -H "Upgrade-Insecure-Requests: 1" -L https://alexa.${AMAZON}/spa/index.html | grep "hidden" | sed 's/hidden/\n/g' | grep "value=\"" | sed -r 's/^.*name="([^"]+)".*value="([^"]+)".*/\1=\2\&/g' >"${TMP}/.alexa.postdata"
~~省略~~
Dockerファイルの作成
次にDockerfileを作成し、クローンしてきたalexa-remote-control
と同じディレクトリに配置する。
FROM alpine:3.13
ENV LANG=ja_JP.UTF-8
RUN apk update \
&& apk add --no-cache curl jq oath-toolkit-oathtool
WORKDIR /app
COPY ./alexa_remote_control.sh /app/
ENTRYPOINT ["./alexa_remote_control.sh"]
その後、イメージのビルドを行う。
docker build -t arc-cmd .
下記のコマンドでalexa-remote-control
のイメージを実行する。下記の例は、アカウントに紐づく全てのデバイスを取得するコマンドである。
$ docker run --rm -t arc-cmd -a
cookie does not exist. logging in ...
device list does not exist. downloading ...
the following devices exist in your account:
リビング の Echo Plus
リラックス
机の上 の Echo Show
This Device
自身のアカウントに紐づくデバイスが全て出力されていれば、動作に問題ないことが確認できたこととなる。
Alexaにしゃべらせる
作成したイメージを使って、Alexaにしゃべらせてみる。私が所持しているデバイスのうち、リビング の Echo Plus
をしゃべらせる際のコマンドは下記の通り。エンコードの関係上、日本語入力する場合にspeak:
の後に半角スペースが必要になる点は注意が必要。
docker run --rm -t arc-cmd -d "リビング の Echo Plus" -e "speak: テスト"
実行後、指定したAlexaが-e
オプションで指定した文章を発話すれば完了。
その他のオプションに関しては下記のURLを参考