-
Notifications
You must be signed in to change notification settings - Fork 4
/
googleassistant.js
52 lines (43 loc) · 1.86 KB
/
googleassistant.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
'use strict';
const path = require('path');
const grpc = require('grpc');
const protoFiles = require('google-proto-files');
const GoogleAuth = require('google-auth-library');
//Import proto files
const PROTO_ROOT_DIR = protoFiles('..');
const embedded_assistant_pb = grpc.load({
root: PROTO_ROOT_DIR,
file: path.relative(PROTO_ROOT_DIR, protoFiles.embeddedAssistant.v1alpha2)
}).google.assistant.embedded.v1alpha2;
class GoogleAssistant {
constructor(credentials) {
GoogleAssistant.prototype.endpoint_ = "embeddedassistant.googleapis.com";
this.client = this.createClient_(credentials);
this.locale = "en-US";
this.deviceModelId = 'default';
this.deviceInstanceId = 'default';
}
createClient_(credentials) {
const sslCreds = grpc.credentials.createSsl();
// https://github.com/google/google-auth-library-nodejs/blob/master/ts/lib/auth/refreshclient.ts
const auth = new GoogleAuth();
const refresh = new auth.UserRefreshClient();
refresh.fromJSON(credentials, function (res) { });
const callCreds = grpc.credentials.createFromGoogleCredential(refresh);
const combinedCreds = grpc.credentials.combineChannelCredentials(sslCreds, callCreds);
const client = new embedded_assistant_pb.EmbeddedAssistant(this.endpoint_, combinedCreds);
return client;
}
}
// GA auth
const homedir = require('homedir')
const deviceCredentials = require(`${homedir()}/.config/google-oauthlib-tool/credentials.json`); // Path to the Google credentials
const CREDENTIALS = {
client_id: deviceCredentials.client_id,
client_secret: deviceCredentials.client_secret,
refresh_token: deviceCredentials.refresh_token,
type: "authorized_user"
};
var assistant = new GoogleAssistant(CREDENTIALS);
exports.assistant = assistant;
exports.embedded_assistant_pb = embedded_assistant_pb;