-
Notifications
You must be signed in to change notification settings - Fork 6
/
api-server.js
50 lines (45 loc) · 1.38 KB
/
api-server.js
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
require("date-utils");
const express = require("express");
const googlehome = require("./google-home-voicetext");
const bodyParser = require("body-parser");
const app = express();
const serverPort = 8080;
const deviceName = "Google Home";
googlehome.device(deviceName);
if (process.env["GOOGLE_HOME_IP"]) {
googlehome.ip(process.env["GOOGLE_HOME_IP"]);
}
const urlencodedParser = bodyParser.urlencoded({ extended: false });
app.post("/google-home-voicetext", urlencodedParser, function(req, res) {
now = new Date().toFormat("YYYY-MM-DD HH24:MI:SS");
console.log(now);
if (!req.body) return res.sendStatus(400);
console.log(req.body);
const 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() {
console.log('POST "text=Hello Google Home" to:');
console.log(
" http://{Server IP address}:" + serverPort + "/google-home-voicetext"
);
console.log("example:");
console.log(
'curl -X POST -d "text=こんにちは、Googleです。" http://{Server IP address}:' +
serverPort +
"/google-home-voicetext"
);
});