-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·492 lines (445 loc) · 19.2 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
'use strict';
// Electron
const electron = require('electron');
const app = electron.app;
const BrowserWindow = electron.BrowserWindow;
const { ipcMain } = require('electron');
const path = require('path');
const url = require('url');
// Load configuration
const config = require('./config.json');
const packjson = require('./package.json');
// Logger
const bunyan = require('bunyan');
// Command Processing
const Commander = require('./commandProcess.js');
const ffmpeg = require('fluent-ffmpeg');
const mime = require('mime');
// Transciber as a child process
const fork = require('child_process').fork;
const program = path.resolve('transcriber.js');
const child = fork(program);
let debug = /--debug/.test(process.argv[2]);
let win;
function createWindow() {
// Create the browser window.
win = new BrowserWindow({
width: 1200,
height: 900,
show: !!debug
});
// and load the index.html of the app.
win.loadURL(url.format({
pathname: path.join(__dirname, 'index.html'),
protocol: 'file:',
slashes: true
}));
// Open the DevTools in debug mode
debug && win.webContents.on('did-frame-finish-load', () => win.webContents.openDevTools());
// Emitted when the window is closed.
win.on('closed', () => win = null);
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow);
// Quit when all windows are closed.
app.on('window-all-closed', () => {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit()
}
});
app.on('activate', function () {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (win === null) {
createWindow();
}
});
// SDK logger
var sdkLogger = bunyan.createLogger({
name: 'sdk',
stream: process.stdout,
level: config.sdkLogLevel
});
// Application logger
var logger = bunyan.createLogger({
name: 'app',
stream: process.stdout,
level: 'info'
});
// Node utils
var util = require('util');
var assert = require('assert');
// File system
var fs = require('fs');
// Circuit SDK
logger.info('[APP]: get Circuit instance');
var Circuit = require('circuit-sdk');
logger.info('[APP]: Circuit set bunyan logger');
Circuit.setLogger(sdkLogger);
var client = new Circuit.Client({
client_id: config.bot.client_id,
client_secret: config.bot.client_secret,
domain: config.domain,
scope: 'ALL'
});
var Robot = function () {
var self = this;
var conversation = null;
var commander = new Commander(logger);
var user = {};
//*********************************************************************
//* initBot
//*********************************************************************
this.initBot = function () {
logger.info(`[ROBOT]: initialize robot`);
return new Promise(function (resolve, reject) {
//Nothing to do for now
resolve();
});
};
//*********************************************************************
//* logonBot
//*********************************************************************
this.logonBot = function () {
return new Promise(function(resolve, reject) {
var retry;
self.addEventListeners(client);
var logon = function () {
client.logon().then(logonUser => {
logger.info(`[ROBOT]: Client created and logged as ${logonUser.userId}`);
user = logonUser;
clearInterval(retry);
setTimeout(resolve, 5000);
}).catch(error => {
logger.error(`[ROBOT]: Error logging Bot. Error: ${error}`);
});
}
logger.info(`[ROBOT]: Create robot instance with id: ${config.bot.client_id}`);
retry = setInterval(logon, 2000);
});
};
//*********************************************************************
//* updateUserData
//*********************************************************************
this.updateUserData = function () {
return new Promise(function (resolve, reject) {
user.firstName = config.bot.first_name;
user.lastName = config.bot.last_name;
user.jobTitle = config.bot.job_title;
user.company = config.bot.company;
logger.info(`[ROBOT]: Update user ${user.userId} data with firstname: ${user.firstName} and lastname: ${user.lastName}`);
client.updateUser(user).then(self.setPresence({ state: Circuit.Enums.PresenceState.AVAILABLE })).then(resolve);
});
}
//*********************************************************************
//* addEventListeners
//*********************************************************************
this.addEventListeners = function (client) {
logger.info(`[ROBOT]: addEventListeners`);
Circuit.supportedEvents.forEach(function(e) {
logger.info(`[ROBOT] add Event listener for ${e}`);
client.addEventListener(e, self.processEvent)
});
};
//*********************************************************************
//* setPresence
//*********************************************************************
this.setPresence = function (presence) {
return new Promise(function (resolve, reject) {
client.setPresence(presence).then(resolve);
});
};
//*********************************************************************
//* logEvent -- helper
//*********************************************************************
this.logEvent = function (evt) {
logger.info(`[ROBOT]: ${evt.type} event received`);
logger.debug(`[ROBOT]:`, util.inspect(evt, { showHidden: true, depth: null }));
};
//*********************************************************************
//* getConversation
//*********************************************************************
this.getConversation = function () {
return new Promise(function (resolve, reject) {
if (config.convId) {
client.getConversationById(config.convId)
.then(conv => {
logger.info(`[ROBOT]: checkIfConversationExists`);
if (conv) {
logger.info(`[ROBOT]: conversation ${conv.convId} exists`);
resolve(conv);
} else {
logger.info(`[ROBOT]: conversation with id ${conv.convId} does not exist`);
reject(`conversation with id ${conv.convId} does not exist`);
}
});
} else {
client.getDirectConversationWithUser(config.botOwnerEmail)
.then(conv => {
logger.info(`[ROBOT]: checkIfConversationExists`);
if (conv) {
logger.info(`[ROBOT]: conversation ${conv.convId} exists`);
resolve(conv);
} else {
logger.info(`[ROBOT]: conversation does not exist, create new conversation`);
return client.createDirectConversation(config.botOwnerEmail);
}
});
}
});
};
//*********************************************************************
//* say Hi
//*********************************************************************
this.sayHi = function (evt) {
return new Promise(function (resolve, reject) {
logger.info(`[ROBOT]: say hi`);
self.getConversation()
.then(conv => {
logger.info(`[ROBOT]: send conversation item`);
conversation = conv;
resolve();
return self.buildConversationItem(null, `Hi from ${config.bot.nick_name}`,
`I am ready`).
then(item => client.addTextItem(conversation.convId, item));
});
});
};
//*********************************************************************
//* buildConversationItem
//*********************************************************************
this.buildConversationItem = function (parentId, subject, content, attachments) {
return new Promise(function (resolve, reject) {
var attach = attachments && [attachments];
var item = {
parentId: parentId,
subject: subject,
content: content,
contentType: Circuit.Constants.TextItemContentType.RICH,
attachments: attach
};
resolve(item);
})
};
//*********************************************************************
//* terminate -- helper
//*********************************************************************
this.terminate = function (err) {
var error = new Error(err);
logger.error(`[ROBOT]: Robot failed ${error.message}`);
logger.error(error.stack);
process.exit(1);
};
//*********************************************************************
//* processEvent
//*********************************************************************
this.processEvent = function (evt) {
self.logEvent(evt);
switch (evt.type) {
case 'itemAdded':
self.processItemAddedEvent(evt);
break;
case 'itemUpdated':
self.processItemUpdatedEvent(evt);
break;
case 'callStatus':
self.processCallStatusEvent(evt);
break;
case 'userUpdated':
self.processUserUpdatedEvent(evt);
break;
default:
logger.info(`[ROBOT]: unhandled event ${evt.type}`);
break;
}
};
//*********************************************************************
//* processUserUpdatedEvent
//*********************************************************************
this.processUserUpdatedEvent = function (evt) {
user = evt.user;
};
//*********************************************************************
//* processItemAddedEvent
//*********************************************************************
this.processItemAddedEvent = function (evt) {
if (evt.item.text && evt.item.creatorId !== user.userId) {
logger.info(`[ROBOT] Recieved itemAdded event with itemId [${evt.item.itemId}] and content [${evt.item.text.content}]`);
self.processCommand(evt.item.convId, evt.item.parentItemId || evt.item.itemId, evt.item.text.content);
}
};
//*********************************************************************
//* processItemUpdatedEvent
//*********************************************************************
this.processItemUpdatedEvent = function (evt) {
if (evt.item.text && evt.item.creatorId !== user.userId) {
if (evt.item.text.content) {
var lastPart = evt.item.text.content.split('<hr/>').pop();
logger.info(`[ROBOT] Recieved itemUpdated event with: ${lastPart}`);
self.processCommand(evt.item.parentItemId || evt.item.itemId, lastPart);
}
}
};
//*********************************************************************
//* processCallStatusEvent
//*********************************************************************
this.processCallStatusEvent = function (evt) {
logger.info(`[ROBOT]: Received callStatus event with call state ${evt.call.state}`);
if (evt.call.reason === `sdpConnected`) {
logger.info(`[ROBOT] SDP Connected. Start Recording`);
this.startRecording(call);
}
// if (evt.call.state === 'Started') {
// self.stream(evt.call.convId, `start`);
// }
};
//*********************************************************************
//* isItForMe?
//*********************************************************************
this.isItForMe = function (command) {
return (command.indexOf('mention') !== -1 && command.indexOf(user.displayName) !== -1);
};
//*********************************************************************
//* processCommand
//*********************************************************************
this.processCommand = function (convId, itemId, command) {
logger.info(`[ROBOT] Processing command: [${command}]`);
if (self.isItForMe(command)) {
var withoutName = command.substr(command.indexOf('</span> ') + 8);
logger.info(`[ROBOT] Command is for me. Processing [${withoutName}]`);
commander.processCommand(withoutName, function (reply, params) {
logger.info(`[ROBOT] Interpreting command to ${reply} with parms ${JSON.stringify(params)}`);
switch (reply) {
case 'status':
self.reportStatus(convId, itemId);
transcode(config.ogg_file, config.raw_file);
break;
case 'version':
self.reportVersion(convId, itemId);
break;
case 'showHelp':
self.showHelp(convId, itemId);
break;
case 'startStream':
self.stream(convId, `start`);
break;
case 'stopStream':
self.stream(convId, `stop`);
break;
case 'dial':
self.dial(convId, itemId, params);
break;
case 'shutdown':
self.shutdown();
break;
default:
logger.info(`[ROBOT] I do not understand [${withoutName}]`);
self.buildConversationItem(itemId, null,
`I do not understand <b>[${withoutName}]</b>`).
then(item => client.addTextItem(convId || conversation.convId, item));
break;
}
});
} else {
logger.info(`[ROBOT] Ignoring command: it is not for me`);
}
};
//*********************************************************************
//* reportStatus
//*********************************************************************
this.reportStatus = function (convId, itemId) {
self.buildConversationItem(itemId, null,
`Status <b>On</b>`).
then(item => client.addTextItem(convId || conversation.convId, item));
};
//*********************************************************************
//* reportVersion
//*********************************************************************
this.reportVersion = function (convId, itemId) {
self.buildConversationItem(itemId, null,
`Version: <b>${packjson.version}</b>`).
then(item => client.addTextItem(convId || conversation.convId, item));
};
//*********************************************************************
//* showHelp
//*********************************************************************
this.showHelp = function (convId, itemId) {
logger.info(`[ROBOT] Displaying help...`);
commander.buildHelp().then(help => self.buildConversationItem(itemId, 'HELP', help)
.then(item => client.addTextItem(convId || conversation.convId, item)));
};
//*********************************************************************
//* dial phone number
//*********************************************************************
this.dial = function (convId, itemId, params) {
if (!params || !params.length) {
logger.error(`[ROBOT] No number to dial`);
self.buildConversationItem(itemId, `ERROR`, "Unable to dial. Number missing")
.then(item => client.addTextItem(convId || conversation.convId, item));
} else {
logger.info(`[ROBOT] Sending dial message to renderer`);
win.webContents.send("dial", params && params.join());
self.buildConversationItem(itemId, `Dialing`, `Dialing ${params.join()}`)
.then(item => client.addTextItem(convId || conversation.convId, item));
}
}
this.startRecording = async function(call) {
logger.info(`[ROBOT] Sending startRecording to renderer`);
win.webContents.send("startRecording", call);
}
this.shutdown = function (reason) {
logger.warn(`[ROBOT] Shutting down. Reason: ${reason}`);
client.logout();
throw new Error('Terminated by user');
}
}
ipcMain.on("recordingReady", function(sender, params) {
logger.info(`[ROBOT] Recording is ready for transcoding`);
// Transcode file for google speech transcription
transcode(config.ogg_file, config.raw_file).then(function() {
logger.info(`[ROBOT] Transcoding complete`);
}).catch(e => logger.error(e));
});
// /opt/ffmpeg/ffmpeg -acodec opus -i test.raw -f s16le -acodec pcm_s16le -ar 16000 output.raw
function transcode(fileIn, fileOut) {
return new Promise(function(resolve, reject) {
if (!fileIn || !fileOut) {
throw new Error('You must specify a path for both input and output files.');
}
if (!fs.existsSync(fileIn)) {
throw new Error(`Input file must exist. Input file: ${fileIn}`);
}
if (mime.lookup(fileIn).indexOf('audio') > -1) {
try {
ffmpeg()
.input(fileIn)
.outputOptions([
'-f s16le',
'-acodec pcm_s16le',
'-vn',
'-ac 1',
'-ar 16k',
'-map_metadata -1'
])
.save(fileOut)
.on('end', () => resolve(fileOut));
} catch (e) {
reject(e);
}
} else {
throw new Error('File must have audio mime.');
}
});
}
//*********************************************************************
//* main
//*********************************************************************
var robot = new Robot();
robot.initBot()
.then(robot.logonBot)
.then(robot.sayHi)
.catch(robot.terminate);