・・・と言っても前回も書きましたがすでに実現されている方がいるので、それを参考にさせていただく手はないです。
・LINE Channelを作成
・ラズパイにwebhookサービスを作成
これらを順に説明します。
LINE Channelを作成
LINE developers で「Messaging API(ボット)をはじめる」を選択し、新規Channelを作成
作成したLINE Channelの設定画面で、以下を設定
Webhook設定を「利用する」
Webhook URLを、自宅サーバのURL(https://line.取得したドメイン)
こちらも同じく、下記の方の記事が参考になります。
このために前回SSL証明書を作ったんです。
で次からwebhookのサービスを作成していきます。
node.jsのインストール
これはこちらで参考にさせてもらいました。
ついでにgoogle-home-notifierのインストールとテストもこれを参考にさせてもらいました。
・既存node.jsの削除
・nvmのインストール
・node.jsのインストール
・google-home-notifi
の順でやっていきます。
既存node.jsの削除
既存でnode.jsが入っているので、一旦削除します。
1 | sudo apt-get autoremove nodejs |
nvmのインストール
nvmインストールのためにホームにディレクトリを作成
1 2 | sudo mkdir ~/.nvm sudo chmod 777 ~/.nvm |
gitからnvmをクローンしてきます
1 | git clone https://github.com/creationix/nvm.git ~/.nvm |
nvmの設定を有効にします
1 | source ~/.nvm/nvm.sh |
できました。
1 | nvm --version |
でチェックしておきます。
node.jsインストール
インストールできるnode.jsのバージョンを確認し、LTS版をインストールします。
1 | nvm ls-remote | grep LTS |
現時点ではv8.11.3です。node.jsをインストールします
1 | nvm install v8.11.3 |
できました。
1 2 | node --version npm --version |
node.jsとnpmが入っているかチェックしておきます。
オッケー、ラズパイ! 次いってみよーー
google-home-notifierのインストール
ホームディレクトリでも/usr/localの下でもいいので、インストールディレクトリを作成しnpmの初期化をします。
1 | npm init |
で、google-home-notifierのインストールです。
1 | npm install google-home-notifier |
サンプルプログラムを動かして喋ってくれればオッケー、ラズパイ!です。
webhookサービスjsの作成
さて、話は戻って、webhookのサービスを作成します。
こちらも同じく、
LINEに送信したメッセージを、Google Homeで読み上げ、家族に通知
こちらを参考にさせていただきました。
すでに前の記事に外部公開の設定ができているのでngrokは動かしません。
gitにソースをあげていただいているのでダウンロードします。
configファイルはうちでは名前解決でGoogleHomeを探させているのでこのように設定しています。
1 2 3 4 5 6 7 8 9 | { "server_port": "8080", "server_ip": "localhost", "googlehome_name": "Google-Home", "beginning_sentence": "からメッセージが届きました。", "speakable_userid": "" } |
ここで、ポートについてですが、前回の記事のapache設定でProxyPassの設定をしました。
そのさいのポートと同じにしておいてください。
あと、プログラム自体は下記のようにIDを拾って誰からのメッセージかを喋るように変更してみました。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | const config = require('./config/default.json'); // const config = require('./config/myconfig.json'); // for http server const http = require('http'); // for googlehome const googlehome = require('google-home-notifier'); const language = 'ja'; function googlehome_init() { googlehome.device(config.googlehome_name, language); // googlehome.ip(config.googlehome_ip, language); } function googlehome_speak(text) { googlehome.notify(text, function (res) { console.log('googlehome_res : ' + res + ' speech_text : ' + text); }); } function speak_prefix(userid) { if(userid == 'AさんのユーザID') { return 'Aさん'; } if(userid == 'BさんのユーザID') { return 'Bさん'; } if(userid == 'CさんのユーザID') { return 'Cさん'; } } // main() googlehome_init(); http.createServer(function (request, response) { let post_data = ''; request.on('data', function (chunk) { post_data += chunk; }); request.on('end', function () { console.log('post_data : ' + post_data); const webhook = JSON.parse(post_data).events[0]; if (webhook.type != 'message' || webhook.message.type != 'text') { return; } // 特定の人からのメッセージのみ発話 if (config.speakable_userid == '' || webhook.source.userId == config.speakable_userid) { const data_text = webhook.message.text; // console.log('data_text : ' + data_text); googlehome_speak(speak_prefix(webhook.source.userId) + config.beginning_sentence + data_text); } response.writeHead(200, { 'Content-Type': 'text/plain' }); response.end(); }); }).listen(config.server_port); |
でけた。
webhookサービス起動
まずはコンソールで起動させてみます。
1 | node server_for_line.js |
これで動けばサービス化しましょう。
node.jsのサービス化
サービス化にはforeverを使います。厳密にはサービスではないですが、一旦これでいいでしょう。
npmでforeverをインストールします
1 | npm install forever |
で、下記コマンドでずっと動いてくれるようにします。
1 | forever start /インストールしたディレクトリ/googlehome/speak_message/server_for_line.js |
これで、LINEに
オッケー、ラズパイ!!
ってメッセージを送るとGoogleHomeが、
オッケー、ラズパイ
って喋ってくれます(笑)。
やっとできたぞーー。