From 0649ccc219e745c4373a7ba32137f2a5598dac48 Mon Sep 17 00:00:00 2001 From: kazu634 Date: Wed, 5 May 2021 21:03:36 +0900 Subject: [PATCH 1/4] Add asciinema shortcode. --- layouts/shortcodes/asciinema.html | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 layouts/shortcodes/asciinema.html diff --git a/layouts/shortcodes/asciinema.html b/layouts/shortcodes/asciinema.html new file mode 100644 index 0000000..45cad0e --- /dev/null +++ b/layouts/shortcodes/asciinema.html @@ -0,0 +1,2 @@ +{{ $id := .Get 0 }} + \ No newline at end of file From c94b819cef23af919695230c5ed367951fc12e70 Mon Sep 17 00:00:00 2001 From: kazu634 Date: Wed, 5 May 2021 21:11:56 +0900 Subject: [PATCH 2/4] =?UTF-8?q?=E5=88=9D=E7=A8=BF=E3=82=92=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../misc/2021-05-05-Google-Home-Notifier.md | 255 ++++++++++++++++++ 1 file changed, 255 insertions(+) create mode 100644 content/labs/misc/2021-05-05-Google-Home-Notifier.md diff --git a/content/labs/misc/2021-05-05-Google-Home-Notifier.md b/content/labs/misc/2021-05-05-Google-Home-Notifier.md new file mode 100644 index 0000000..26e78e4 --- /dev/null +++ b/content/labs/misc/2021-05-05-Google-Home-Notifier.md @@ -0,0 +1,255 @@ ++++ +author = "kazu634" +description = "ゴールデンウィークの暇潰しにGoogle Nestで任意の文章を喋らせるようにしてみました。" +categories = ["Labs", "Program"] +tags = ["node.js"] +date = "2021-05-05T20:53:36+09:00" +title = "google-home-notifierでGoogle Nestを喋らせてみました。" ++++ + +google-home-notifierでGoogle Nestを喋らせてみました。これまでの流れをまとめてみました。 + +## TL;DR +発端はこのような形でした: + +{{< tweet 1389508813200396291 >}} + +{{< tweet 1389554264310124545 >}} + +{{< tweet 1389775994584657922 >}} + +{{< tweet 1389862890853650432 >}} + +というわけで、以下のようにコマンドを実行すると、 + +{{< asciinema 411953 >}} + +次のようにGoogle Homeを喋らせることができました。 + +{{< youtube jyTQXF0ogIc >}} + +## 事前準備 +`Node.js`を利用できるようにしておきましょう。ここでは詳しくは解説しません。また、Ubuntuの場合は、以下のパッケージをインストールしておきます: + +- `libnss-mdns` +- `libavahi-compat-libdnssd-dev` + +## google-home-notifierのインストール +オフィシャルなGithubリポジトリはメンテナンスがされておらず、うまく動作しなかったため、プルリクが来ていたリポジトリからインストールする必要があるようでした。 + +### リポジトリからのインストール +[Support google-tts-api@2.0.2 by TomPenguin · Pull Request #55 · noelportugal/google-home-notifier · GitHub](https://github.com/noelportugal/google-home-notifier/pull/55)を参考にして、以下のコマンドを実行して、リポジトリからインストールします: + +```bash +% git clone https://github.com/TomPenguin/google-home-notifier.git +% cd google-home-notifier +% git checkout origin/feature/support-new-google-tts-api +``` + +### 必要なライブラリのインストール +以下のコマンドを実行して、必要なライブラリをダウンロードします: + +```bash +% npm install +``` + +### npm install実施後に行うソース修正 +修正が必要ということなので、以下を修正します: + +|項目 |値 | +|----|---------------------------------| +|修正対象| `node_modules/mdns/lib/browser.js`| + +修正前: + +```js +Browser.defaultResolverSequence = [ + rst.DNSServiceResolve(), 'DNSServiceGetAddrInfo' in dns_sd ? rst.DNSServiceGetAddrInfo() : rst.getaddrinfo() +, rst.makeAddressesUnique() +]; +``` + +修正後: + +```js +Browser.defaultResolverSequence = [ + rst.DNSServiceResolve(), 'DNSServiceGetAddrInfo' in dns_sd ? rst.DNSServiceGetAddrInfo() : rst.getaddrinfo({families:[4]}) +, rst.makeAddressesUnique() +]; +``` + +## google-home-notifierを利用してみる +調べてみた限り、以下の二通りの方法があるようでした: + +1. example.jsを利用する +2. 自分でコードを作成する + +### example.jsを利用して、APIサーバを動かす + +git cloneしたディレクトリにある`example.js`を以下のように修正します。修正前は`ngrok`を利用したり、ポルトガル語前提だったりするため。 + + +```js +var express = require('express'); +var googlehome = require('./google-home-notifier'); +var ngrok = require('ngrok'); +var bodyParser = require('body-parser'); +var app = express(); +const serverPort = 8091; // default port + +var deviceName = 'Google Home Mini'; +var ip = '192.168.10.50'; // default IP + +var urlencodedParser = bodyParser.urlencoded({ extended: false }); + +app.post('/google-home-notifier', urlencodedParser, function (req, res) { + + if (!req.body) return res.sendStatus(400) + console.log(req.body); + + var text = req.body.text; + + if (req.query.ip) { + ip = req.query.ip; + } + + var language = 'ja'; // default language code + if (req.query.language) { + language; + } + + googlehome.ip(ip, language); + googlehome.device(deviceName,language); + + if (text){ + try { + if (text.startsWith('http')){ + var mp3_url = text; + googlehome.play(mp3_url, function(notifyRes) { + console.log(notifyRes); + res.send(deviceName + ' will play sound from url: ' + mp3_url + '\n'); + }); + } else { + googlehome.notify(text, function(notifyRes) { + console.log(notifyRes); + res.send(deviceName + ' will say: ' + text + '\n'); + }); + } + } catch(err) { + console.log(err); + res.sendStatus(500); + res.send(err); + } + }else{ + res.send('Please GET "text=Hello Google Home"'); + } +}) + +app.get('/google-home-notifier', function (req, res) { + + console.log(req.query); + + var text = req.query.text; + + if (req.query.ip) { + ip = req.query.ip; + } + + var language = 'ja'; // default language code + if (req.query.language) { + language; + } + + googlehome.ip(ip, language); + googlehome.device(deviceName,language); + + if (text) { + try { + if (text.startsWith('http')){ + var mp3_url = text; + googlehome.play(mp3_url, function(notifyRes) { + console.log(notifyRes); + res.send(deviceName + ' will play sound from url: ' + mp3_url + '\n'); + }); + } else { + googlehome.notify(text, function(notifyRes) { + console.log(notifyRes); + res.send(deviceName + ' will say: ' + text + '\n'); + }); + } + } catch(err) { + console.log(err); + res.sendStatus(500); + res.send(err); + } + }else{ + res.send('Please GET "text=Hello+Google+Home"'); + } +}) + +app.listen(serverPort, function () { +}) +``` + +なお、Google HomeのIPアドレスはアプリから調べることができます: + +[image:77389DA9-2AA9-4895-BDBB-E9EA20B6C7B9-3039-000005BAEFB19F6D/Photo May 5, 2021 204057.jpg] + +修正後、以下のコマンドを実行します: + +```bash +% node example.js +*** WARNING *** The program 'node' uses the Apple Bonjour compatibility layer of Avahi. +*** WARNING *** Please fix your application to use the native API of Avahi! +*** WARNING *** For more information see +*** WARNING *** The program 'node' called 'DNSServiceRegister()' which is not supported (or only supported partially) in the Apple Bonjour compatibility layer of Avahi. +*** WARNING *** Please fix your application to use the native API of Avahi! +*** WARNING *** For more information see +``` + +別なターミナルを立ち上げて、以下のようにするとGoogle Homeを喋らせることができました: + +```bash +% curl -X POST -d "text=ゴールデンウィークが終わろうとしていて、サザエさんシンドロームにかかりつつあります。" http://localhost:8091/google-home-notifier +``` + +もしくは、`GET`リクエストでも大丈夫みたい: + +```bash +% curl "http://localhost:8091/google-home-notifier?text=Hello+Google+home" +``` + +### 自分でコードを作成する +自分でコードを作成する場合は、このようなソースコードを作ればいいみたいです。 + +```js +var googlehome = require('./google-home-notifier'); +var language = 'ja'; // if not set 'us' language will be used + +// googlehome.device('Google Home Mini', language); // Change to your Google Home name +// or if you know your Google Home IP +googlehome.ip('192.168.10.50', language); + +googlehome.notify('ゴールデンウィークが終わろうとしていて、サザエさんシンドロームにかかりつつあります。', function(res) { + console.log(res); +}); +``` + +これを`test.js`などとして保存し、以下のようにすると、Google Homeを喋らせることができました: + +```bash +% node test.js +*** WARNING *** The program 'node' uses the Apple Bonjour compatibility layer of Avahi. +*** WARNING *** Please fix your application to use the native API of Avahi! +*** WARNING *** For more information see +*** WARNING *** The program 'node' called 'DNSServiceRegister()' which is not supported (or only supported partially) in the Apple Bonjour compatibility layer of Avahi. +*** WARNING *** Please fix your application to use the native API of Avahi! +*** WARNING *** For more information see +Device notified +``` + +## 参考 +- [Google Home/Nestを喋らせるgoogle-home-notifierの導入マニュアル [2020年12月版] - RemoteRoom](https://remoteroom.jp/diary/2020-12-31/) +- [GitHub - noelportugal/google-home-notifier: Send notifications to Google Home](https://github.com/noelportugal/google-home-notifier) +- [GitHub - TomPenguin/google-home-notifier at feature/support-new-google-tts-api](https://github.com/TomPenguin/google-home-notifier/tree/feature/support-new-google-tts-api) +- [![asciicast](https://asciinema.org/a/411953.svg)](https://asciinema.org/a/411953) From 0d8a6cc09cd94a9cdc6eb60ffaa4dc095ff2da71 Mon Sep 17 00:00:00 2001 From: kazu634 Date: Wed, 5 May 2021 21:15:38 +0900 Subject: [PATCH 3/4] =?UTF-8?q?=E8=A8=98=E4=BA=8B=E3=81=AE=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- content/labs/misc/2021-05-05-Google-Home-Notifier.md | 1 - 1 file changed, 1 deletion(-) diff --git a/content/labs/misc/2021-05-05-Google-Home-Notifier.md b/content/labs/misc/2021-05-05-Google-Home-Notifier.md index 26e78e4..13dc43c 100644 --- a/content/labs/misc/2021-05-05-Google-Home-Notifier.md +++ b/content/labs/misc/2021-05-05-Google-Home-Notifier.md @@ -252,4 +252,3 @@ Device notified - [Google Home/Nestを喋らせるgoogle-home-notifierの導入マニュアル [2020年12月版] - RemoteRoom](https://remoteroom.jp/diary/2020-12-31/) - [GitHub - noelportugal/google-home-notifier: Send notifications to Google Home](https://github.com/noelportugal/google-home-notifier) - [GitHub - TomPenguin/google-home-notifier at feature/support-new-google-tts-api](https://github.com/TomPenguin/google-home-notifier/tree/feature/support-new-google-tts-api) -- [![asciicast](https://asciinema.org/a/411953.svg)](https://asciinema.org/a/411953) From ec62f2b42f4d1cf35864d49e4da93720e3a6ae86 Mon Sep 17 00:00:00 2001 From: kazu634 Date: Wed, 5 May 2021 21:18:34 +0900 Subject: [PATCH 4/4] =?UTF-8?q?=E7=94=BB=E5=83=8F=E3=81=AE=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- content/labs/misc/2021-05-05-Google-Home-Notifier.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/labs/misc/2021-05-05-Google-Home-Notifier.md b/content/labs/misc/2021-05-05-Google-Home-Notifier.md index 13dc43c..b41b5d7 100644 --- a/content/labs/misc/2021-05-05-Google-Home-Notifier.md +++ b/content/labs/misc/2021-05-05-Google-Home-Notifier.md @@ -193,7 +193,7 @@ app.listen(serverPort, function () { なお、Google HomeのIPアドレスはアプリから調べることができます: -[image:77389DA9-2AA9-4895-BDBB-E9EA20B6C7B9-3039-000005BAEFB19F6D/Photo May 5, 2021 204057.jpg] +![Image](https://farm66.staticflickr.com/65535/51160371155_3b1420c44e_c.jpg) 修正後、以下のコマンドを実行します: