forked from greghesp/assistant-relay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
211 lines (185 loc) · 6.25 KB
/
index.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
'use strict'
const SSDP = require('node-ssdp').Server;
const path = require('path');
const express = require('express');
const GoogleAssistant = require('google-assistant');
const GRConfig = require('./config.json');
const readline = require('readline');
const async = require('async');
//Define SSDP Server Configuration
const ssdpServer = new SSDP({
location: 'http://' + require('ip').address() + ':3000/desc.xml',
sourcePort: 1900,
ssdpTtl: 3,
});
const authKeys = GRConfig.users;
const config = {
conversation: {
lang: GRConfig.language,
},
users: {},
assistants: {}
}
Object.keys(authKeys).forEach(function(k){
config.users[k] = {};
config.users[k].keyFilePath = path.resolve(__dirname, `${authKeys[k]}`);
config.users[k].savedTokensPath = path.resolve(__dirname, `${k}-tokens.json`);
})
//Define SSDP USN type
ssdpServer.addUSN('urn:greghesp-com:device:GAssist:1');
const app = express()
app.use(express.static(path.join(__dirname, 'xml')));
app.use(express.static(path.join(__dirname, 'audio')));
//Start SSDP Server
ssdpServer.start(function(){
console.log('Fired up the SSDP Server for network discovery...')
});
// Endpoint API
app.post('/custom', function (req, res) {
sendTextInput(req.query.command, req.query.user)
res.status(200).json({
message: `Custom command executed`,
command: `${req.query.command}`
});
})
app.post('/customBroadcast', function (req, res) {
sendTextInput(`broadcast ${req.query.text}`, req.query.user)
res.status(200).json({
message: `Custom broadcast command executed`,
command: `broadcast ${req.query.text}`
});
})
app.post('/nestStream', function (req, res) {
if(req.query.stop) {
sendTextInput(`Stop ${req.query.chromecast}`);
res.status(200).json({
message: `Nest stream command executed`,
command: `Stop ${req.query.chromecast}`
});
}
if(!checkUser(req.query.user)) {
console.log(`User ${req.query.user} not found. Cannot execute request`);
return res.status(400).json({
message: `User not found. Who is ${req.query.user}?`,
command: `Show ${req.query.camera} on ${req.query.chromecast} | User: ${req.query.user}`
});
}
sendTextInput(`Show ${req.query.camera} on ${req.query.chromecast}`, req.query.user)
res.status(200).json({
message: `Nest stream command executed`,
command: `Show ${req.query.camera} on ${req.query.chromecast}`
});
})
app.post('/broadcast', function (req, res) {
const preset = req.query.preset;
const user = req.query.user;
if(!checkUser(user)) {
console.log(`User ${user} not found. Cannot execute request`);
return res.status(400).json({
message: `User not found. Who is ${user}?`,
command: `Preset: ${preset} | User: ${user}`
});
}
switch(preset) {
case 'wakeup':
sendTextInput(`broadcast wake up everyone`, user);
break;
case 'breakfast':
sendTextInput(`broadcast breakfast is ready`, user);
break;
case 'lunch':
sendTextInput(`broadcast it's lunch time`, user);
break;
case 'dinner':
sendTextInput('broadcast dinner is served', user);
break;
case 'timetoleave':
sendTextInput(`broadcast its time to leave`, user);
break;
case 'arrivedhome':
sendTextInput(`broadcast i'm home`, user);
break;
case 'ontheway':
sendTextInput(`broadcast i'm on the way`, user);
break;
case 'movietime':
sendTextInput(`broadcast the movie is about to start`, user);
break;
case 'tvtime':
sendTextInput(`broadcast the show is about to start`, user);
break;
case 'bedtime':
sendTextInput(`broadcast we should go to bed`, user);
break;
default:
sendTextInput(`broadcast you selected a preset broadcast, but didn't say which one`, user);
}
res.status(200).json({
message: `Predefined command executed`,
command: `${req.query.preset}`
});
})
//Start Express Web Server
app.listen(3000, () => console.log('Firing up the Web Server for communication on port 3000'))
//Assistant Integration
const startConversation = (conversation) => {
conversation
//.on('audio-data', data => console.log('Got some audio data'))
.on('response', (text) => {
if (text) {
console.log('Google Assistant:', text)
var newLineSplit = text.split("\n")
// Ignore lines if Assistant responds with extra interactive data (such as a "See More" web URL)
if (newLineSplit.length > 1){
text = newLineSplit[0]
}
sendTextInput(`broadcast ${text}`)
}
})
//.on('volume-percent', percent => console.log('New Volume Percent:', percent))
//.on('device-action', action => console.log('Device Action:', action))
.on('ended', (error, continueConversation) => {
if (error) {
console.log('Conversation Ended Error:', error);
} else if (continueConversation) {
console.log('Support for Actions on Google are not yet supported by Assistant Relay');
conversation.end();
} else {
console.log('Conversation Complete');
conversation.end();
}
})
.on('error', (error) => {
console.log('Conversation Error:', error);
});
};
const sendTextInput = (text, n) => {
console.log(`Received command ${text}`);
let assistant = Object.keys(config.assistants)[0];
assistant = config.assistants[`${assistant}`];
config.conversation.textQuery = text;
if(n) {
console.log(`User specified was ${n}`)
assistant = config.assistants[`${n}`]
}
console.log(`No user specified, using ${Object.keys(config.assistants)[0]}`)
assistant.start(config.conversation, startConversation);
}
async.forEachOfLimit(config.users, 1, function(i, k, cb){
let auth = i;
config.assistants[k] = new GoogleAssistant(i)
let assistant = config.assistants[k];
assistant.on('ready', () => cb());
assistant.on('error', (e) => {
console.log(`Assistant Error when activating user ${k}. Trying next user`);
return cb();
})
}, function(err){
if(err) return console.log(err.message);
sendTextInput(`broadcast Assistant Relay is now setup and running`)
//sendTextInput()
})
function checkUser(user) {
const users = Object.keys(GRConfig.users);
return users.includes(user)
}