LoginSignup
3
1

More than 1 year has passed since last update.

MacからAlexaをCLI経由でしゃべらせる

Last updated at Posted at 2022-06-26

概要

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.

必要だと言われているouthtooljqコマンドはインストール済みである。

$ 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の各値を、ユーザの設定に合わせて変更する。

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行目を下記のように修正の必要がある。

alexa-remote-control.sh

~~省略~~

- ${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と同じディレクトリに配置する。

Dockerfile
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を参考

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1