varexpress=require('express');vargooglehome=require('./google-home-notifier');varngrok=require('ngrok');varbodyParser=require('body-parser');varapp=express();constserverPort=8091;// default port
vardeviceName='Google Home Mini';varip='192.168.10.50';// default IP
varurlencodedParser=bodyParser.urlencoded({extended:false});app.post('/google-home-notifier',urlencodedParser,function(req,res){if(!req.body)returnres.sendStatus(400)console.log(req.body);vartext=req.body.text;if(req.query.ip){ip=req.query.ip;}varlanguage='ja';// default language code
if(req.query.language){language;}googlehome.ip(ip,language);googlehome.device(deviceName,language);if(text){try{if(text.startsWith('http')){varmp3_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);vartext=req.query.text;if(req.query.ip){ip=req.query.ip;}varlanguage='ja';// default language code
if(req.query.language){language;}googlehome.ip(ip,language);googlehome.device(deviceName,language);if(text){try{if(text.startsWith('http')){varmp3_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アドレスはアプリから調べることができます:
修正後、以下のコマンドを実行します:
1
2
3
4
5
6
7
% 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>
別なターミナルを立ち上げて、以下のようにするとGoogle Homeを喋らせることができました:
1
% curl -X POST -d "text=ゴールデンウィークが終わろうとしていて、サザエさんシンドロームにかかりつつあります。" http://localhost:8091/google-home-notifier
vargooglehome=require('./google-home-notifier');varlanguage='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);});
% 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 <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>
Device notified