forked from endoplasmic/google-assistant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
58 lines (47 loc) · 1.53 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
'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;
const assistantReady = () => {
if (assistant) {
this.emit('ready', assistant);
if (callback) callback(assistant);
}
};
if (authConfig.oauth2Client) {
// we are passing in a client that is already authed with Google
assistant = new Assistant(authConfig.oauth2Client);
process.nextTick(assistantReady);
} else {
// we need to auth with Google right out of the gate
const auth = new Auth(authConfig);
auth.on('ready', (client) => {
assistant = new Assistant(client);
assistantReady();
});
}
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;