-
Notifications
You must be signed in to change notification settings - Fork 75
/
console-input.js
65 lines (58 loc) · 2.02 KB
/
console-input.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
'use strict';
const readline = require('readline');
const path = require('path');
const GoogleAssistant = require('../index');
const config = {
auth: {
keyFilePath: path.resolve(__dirname, 'YOUR_KEY_FILE.json'),
savedTokensPath: path.resolve(__dirname, 'tokens.json'), // where you want the tokens to be saved
},
conversation: {
lang: 'en-US', // defaults to en-US, but try other ones, it's fun!
showDebugInfo: false, // default is false, bug good for testing AoG things
},
};
const startConversation = (conversation) => {
// setup the conversation
conversation
.on('response', text => console.log('Assistant Response:', text))
.on('debug-info', info => console.log('Debug Info:', info))
// if we've requested a volume level change, get the percentage of the new level
.on('volume-percent', percent => console.log('New Volume Percent:', percent))
// the device needs to complete an action
.on('device-action', action => console.log('Device Action:', action))
// once the conversation is ended, see if we need to follow up
.on('ended', (error, continueConversation) => {
if (error) {
console.log('Conversation Ended Error:', error);
} else if (continueConversation) {
promptForInput();
} else {
console.log('Conversation Complete');
conversation.end();
}
})
// catch any errors
.on('error', (error) => {
console.log('Conversation Error:', error);
});
};
const promptForInput = () => {
// type what you want to ask the assistant
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question('Type your request: ', (request) => {
// start the conversation
config.conversation.textQuery = request;
assistant.start(config.conversation, startConversation);
rl.close();
});
};
const assistant = new GoogleAssistant(config.auth);
assistant
.on('ready', promptForInput)
.on('error', (error) => {
console.log('Assistant Error:', error);
});