forked from endoplasmic/google-assistant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
46 lines (36 loc) · 1.22 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
'use strict';
const EventEmitter = require('events');
const util = require('util');
const Assistant = require('./components/assistant');
const Auth = require('./components/auth');
const Conversation = require('./components/conversation');
function GoogleAssistant(authConfig, callback) {
if (authConfig === undefined) {
const error = new Error('Missing auth config object!');
this.emit('error', error);
if (callback) callback(error);
return;
}
let assistant;
// we need to auth with Google right out of the gate
const auth = new Auth(authConfig);
auth.on('ready', (client) => {
assistant = new Assistant(client);
this.emit('ready', assistant);
if (callback) callback(assistant);
});
this.start = (conversationConfig, callback) => {
if (assistant === undefined) {
const error = new Error('Tried calling start() before the ready event!');
this.emit('error', error);
if (callback) callback(error);
return;
}
const conversation = new Conversation(assistant, conversationConfig);
this.emit('started', conversation);
if (callback) callback(conversation);
};
return this;
}
util.inherits(GoogleAssistant, EventEmitter);
module.exports = GoogleAssistant;