Last active
January 17, 2018 14:59
-
-
Save Hunachi/fc229b4af504bfa8b01a294c48b03af7 to your computer and use it in GitHub Desktop.
main.js for google home notifer in first-dream .
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* $ node main.js | |
* */ | |
const express = require('express'); | |
const googlehome = require('./google-home-notifier'); | |
const schedule = require("node-schedule"); | |
const ngrok = require('ngrok'); | |
const bodyParser = require('body-parser'); | |
const app = express(); | |
const serverPort = 8091; // default port | |
const deviceName = ''; | |
var ip = ''; // default IP | |
const urlencodedParser = bodyParser.urlencoded({extended: false}); | |
const dateSetting = function (callback) { | |
const date = new Date(); | |
for (let i = 1; i < 4; i++) { | |
setSchedule(date, i, function () { | |
callback(); | |
}) | |
} | |
}; | |
const setSchedule = function (date, time, callback) { | |
date.setHours(date.getHours() + 1 * time); | |
date.setDate(date.getDate() + 30 * time); | |
//date.setMinutes(date.getMinutes() + 1); //プレゼン用 | |
console.log(date); | |
schedule.scheduleJob(date, function () { | |
callback(); | |
}); | |
}; | |
app.post('/google-home-notifier', urlencodedParser, function (req, res) { | |
if (!req.body) return res.sendStatus(400); | |
console.log(req.body); | |
const text = req.body.text; | |
if (req.query.ip) { | |
ip = req.query.ip; | |
} | |
const language = 'ja'; // default language code | |
const text_ = '一富士、二鷹、三茄子。。。一富士、二鷹、三茄子。。。一富士、二鷹、三茄子。。。一富士、二鷹、三茄子。'; | |
googlehome.ip(ip, language); | |
if (text) { | |
try { | |
if (text === 'おやすみ') { | |
console.log('おやすみなさい'); | |
res.send(deviceName + ' will say: ' + text_ + '\n'); | |
dateSetting(function () { | |
try { | |
googlehome.notify(text_, function (notifyRes) { | |
console.log(notifyRes); | |
}); | |
} catch (err) { | |
console.log(err); | |
res.sendStatus(500); | |
res.send(err); | |
} | |
}) | |
} 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 () { | |
ngrok.connect(serverPort, function (err, url) { | |
console.log('Endpoints:'); | |
console.log(' ' + url + '/google-home-notifier'); | |
console.log('GET example:'); | |
console.log('curl -X GET ' + url + '/google-home-notifier?text=Hello+Google+Home'); | |
console.log('POST example:'); | |
console.log('curl -X POST -d "text=Hello Google Home" ' + url + '/google-home-notifier'); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment