forked from saiedmethal/MMM-AssistantMk2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauth_and_test.js
65 lines (57 loc) · 1.98 KB
/
auth_and_test.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
//Original:https://github.com/endoplasmic/google-assistant/blob/master/examples/console-input.js
'use strict';
const readline = require('readline');
const path = require('path');
const GoogleAssistant = require('google-assistant');
const config = {
auth: {
keyFilePath: path.resolve(__dirname, 'credentials.json'),
savedTokensPath: path.resolve(__dirname, 'token.json'), // where you want the tokens to be saved
},
conversation: {
lang: 'en-US', // defaults to en-US, but try other ones, it's fun!
},
};
const startConversation = (conversation) => {
// setup the conversation
conversation
.on('response', text => console.log('Assistant Response:', text))
// 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);
});