GoogleHomeが家にあるので、しゃべらせてみようと思います。

google-home-notifier を使った例が多いので、これを使ってみます

(2020/3/30 Nodejs 8から12にupdate)

インストール

Node.jsとnpm、そしてgoogle-home-notifier をインストールします
安定板のnodejsは12を使います
# 最新版を入れるのでリポジトリ追加
# curl --silent --location https://rpm.nodesource.com/setup_12.x | bash -


# パッケージのインストール # dnf install nodejs
# dnf install --enablerepo=PowerTools avahi avahi-compat-libdns_sd avahi-compat-libdns_sd-devel
# dnf install nss-mdns

# node --version v12.16.1 # npm --version 6.13.4

# npm install google-home-notifier


google-home-notifierの設定


node_modules/mdns/lib/browser.js の変更

# vi node_modules/mdns/lib/browser.js

Browser.defaultResolverSequence = [ rst.DNSServiceResolve(), 'DNSServiceGetAddrInfo' in dns_sd ? rst.DNSServiceGetAddrInfo() : rst.getaddrinfo() , rst.makeAddressesUnique() ];

↓ Browser.defaultResolverSequence = [ rst.DNSServiceResolve(), 'DNSServiceGetAddrInfo' in dns_sd ? rst.DNSServiceGetAddrInfo() : rst.getaddrinfo({families:[4]}) , rst.makeAddressesUnique() ];


サンプルコードを利用してしゃべらせてみます。
ipの設定をしないと、日本語はしゃべってくれないようです

example.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;

var language   = 'ja';
var deviceName = 'googlehome';
var ip         = '192.168.0.216';
googlehome.device(deviceName,language);
googlehome.ip(ip, language);
// googlehome.accent('uk'); // uncomment for british voice

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 (text){
    try {
      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 POST "text=Hello Google Home"');
  }

})

app.listen(serverPort, function () {
  ngrok.connect(serverPort, function (err, url) {
    console.log('POST "text=Hello Google Home" to:');
    console.log('    http://localhost:' + serverPort + '/google-home-notifier');
    console.log('    ' +url + '/google-home-notifier');
    console.log('example:');
    console.log('curl -X POST -d "text=Hello Google Home" ' + url + '/google-home-notifier');
  });
})
一部APIの仕様が変わっているとのことで、下記対応をす
google-home-notifierで"Error: get key failed from google"とエラーが出る問題の対処法

実行

サーバ側
# 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 <http://0pointer.de/blog/projects/avahi-compat.html>
*** 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 <http://0pointer.de/blog/projects/avahi-compat.html>
POST "text=Hello Google Home" to:
    http://localhost:8091/google-home-notifier
    undefined/google-home-notifier
example:
curl -X POST -d "text=Hello Google Home" undefined/google-home-notifier


クライアント側
textをPOSTして、しゃべらせてみます
# curl -X POST -d "text=おっすおらグーグルホームだぞ" http://localhost:8091/google-home-notifier

参考サイト