From d8dec791c0bbce8c3bf224f7ea835af8aaa7546c Mon Sep 17 00:00:00 2001 From: Julien Bras Date: Wed, 12 Dec 2018 13:41:49 -0500 Subject: [PATCH 1/3] hangouts chat: allow to use GOOGLE_APPLICATION_CREDENTIALS_DATA with json instead of a file --- lib/GoogleHangoutsBot.js | 50 ++++++++++++++++++++++++++++------------ 1 file changed, 35 insertions(+), 15 deletions(-) diff --git a/lib/GoogleHangoutsBot.js b/lib/GoogleHangoutsBot.js index d970f9a81..b1e87f941 100644 --- a/lib/GoogleHangoutsBot.js +++ b/lib/GoogleHangoutsBot.js @@ -159,23 +159,43 @@ function GoogleHangoutsBot(configuration) { }); google_hangouts_botkit.middleware.spawn.use(function(worker, next) { + if(process.env.GOOGLE_APPLICATION_CREDENTIALS) { + /* + * if the env variable containing a json file path is present then do classic + * auth + */ + let params = { + scopes: 'https://www.googleapis.com/auth/chat.bot' + }; - let params = { - scopes: 'https://www.googleapis.com/auth/chat.bot' - }; - - google - .auth - .getClient(params) - .then(client => { - worker.authClient = client; - worker.api = google.chat({version: api_version, auth: worker.authClient}); - next(); - }) - .catch(err => { - console.error('Could not get google auth client !'); - throw new Error(err); + google + .auth + .getClient(params) + .then(client => { + worker.authClient = client; + worker.api = google.chat({version: api_version, auth: worker.authClient}); + next(); + }) + .catch(err => { + console.error('Could not get google auth client !'); + throw new Error(err); + }); + } + else if(process.env.GOOGLE_APPLICATION_CREDENTIALS_DATA) { + // else we use the env containing the actual data + let keys = JSON.parse(process.env.GOOGLE_APPLICATION_CREDENTIALS_DATA); + let client = google.auth.fromJSON(keys); + client.scopes = ['https://www.googleapis.com/auth/chat.bot']; + worker.authClient = client; + worker.api = google.chat({ + version: api_version, + auth: worker.authClient }); + next(); + } + else { + throw new Error('Can\'t find GOOGLE_APPLICATION_CREDENTIALS or GOOGLE_APPLICATION_CREDENTIALS_DATA, auth failed'); + } }); google_hangouts_botkit.middleware.normalize.use(function handlePostback(bot, message, next) { From a08d8f58aeb4b55b1ac679ec5e27755d4d28d4ef Mon Sep 17 00:00:00 2001 From: Julien Bras Date: Tue, 18 Dec 2018 13:36:21 -0500 Subject: [PATCH 2/3] hangouts chat: allow to use GOOGLE_APPLICATION_CREDENTIALS_DATA with json instead of a file // optimise --- lib/GoogleHangoutsBot.js | 72 +++++++++++++++------------------------- 1 file changed, 26 insertions(+), 46 deletions(-) diff --git a/lib/GoogleHangoutsBot.js b/lib/GoogleHangoutsBot.js index b1e87f941..abbea051f 100644 --- a/lib/GoogleHangoutsBot.js +++ b/lib/GoogleHangoutsBot.js @@ -159,54 +159,34 @@ function GoogleHangoutsBot(configuration) { }); google_hangouts_botkit.middleware.spawn.use(function(worker, next) { - if(process.env.GOOGLE_APPLICATION_CREDENTIALS) { - /* - * if the env variable containing a json file path is present then do classic - * auth - */ - let params = { - scopes: 'https://www.googleapis.com/auth/chat.bot' - }; + let params = { + scopes: 'https://www.googleapis.com/auth/chat.bot', + ...configuration.google_auth_params + }; - google - .auth - .getClient(params) - .then(client => { - worker.authClient = client; - worker.api = google.chat({version: api_version, auth: worker.authClient}); - next(); - }) - .catch(err => { - console.error('Could not get google auth client !'); - throw new Error(err); - }); - } - else if(process.env.GOOGLE_APPLICATION_CREDENTIALS_DATA) { - // else we use the env containing the actual data - let keys = JSON.parse(process.env.GOOGLE_APPLICATION_CREDENTIALS_DATA); - let client = google.auth.fromJSON(keys); - client.scopes = ['https://www.googleapis.com/auth/chat.bot']; - worker.authClient = client; - worker.api = google.chat({ - version: api_version, - auth: worker.authClient + google + .auth + .getClient(params) + .then(client => { + worker.authClient = client; + worker.api = google.chat({version: api_version, auth: worker.authClient}); + next(); + }) + .catch(err => { + console.error('Could not get google auth client !'); + throw new Error(err); }); - next(); - } - else { - throw new Error('Can\'t find GOOGLE_APPLICATION_CREDENTIALS or GOOGLE_APPLICATION_CREDENTIALS_DATA, auth failed'); - } - }); - - google_hangouts_botkit.middleware.normalize.use(function handlePostback(bot, message, next) { - message.user = message.user.name; - if (message.message) { - message.channel = message.message.thread.name; - message.text = message.message.argumentText ? message.message.argumentText.trim() : ''; - } else { - message.channel = message.space.name; - } - next(); + }); + + google_hangouts_botkit.middleware.normalize.use(function handlePostback(bot, message, next) { + message.user = message.user.name; + if (message.message) { + message.channel = message.message.thread.name; + message.text = message.message.argumentText ? message.message.argumentText.trim() : ''; + } else { + message.channel = message.space.name; + } + next(); }); google_hangouts_botkit.middleware.categorize.use(function(bot, message, next) { From c28c734df354477908567854af40422f837d21b9 Mon Sep 17 00:00:00 2001 From: Julien Bras Date: Tue, 18 Dec 2018 13:40:36 -0500 Subject: [PATCH 3/3] hangouts chat: clean up PR --- lib/GoogleHangoutsBot.js | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/lib/GoogleHangoutsBot.js b/lib/GoogleHangoutsBot.js index abbea051f..2b25d4e9d 100644 --- a/lib/GoogleHangoutsBot.js +++ b/lib/GoogleHangoutsBot.js @@ -159,9 +159,10 @@ function GoogleHangoutsBot(configuration) { }); google_hangouts_botkit.middleware.spawn.use(function(worker, next) { + let params = { scopes: 'https://www.googleapis.com/auth/chat.bot', - ...configuration.google_auth_params + ...configuration.google_auth_params }; google @@ -176,17 +177,17 @@ function GoogleHangoutsBot(configuration) { console.error('Could not get google auth client !'); throw new Error(err); }); - }); - - google_hangouts_botkit.middleware.normalize.use(function handlePostback(bot, message, next) { - message.user = message.user.name; - if (message.message) { - message.channel = message.message.thread.name; - message.text = message.message.argumentText ? message.message.argumentText.trim() : ''; - } else { - message.channel = message.space.name; - } - next(); + }); + + google_hangouts_botkit.middleware.normalize.use(function handlePostback(bot, message, next) { + message.user = message.user.name; + if (message.message) { + message.channel = message.message.thread.name; + message.text = message.message.argumentText ? message.message.argumentText.trim() : ''; + } else { + message.channel = message.space.name; + } + next(); }); google_hangouts_botkit.middleware.categorize.use(function(bot, message, next) {