Skip to content

Instantly share code, notes, and snippets.

@ouadie-lahdioui
Last active July 17, 2018 08:13
Show Gist options
  • Save ouadie-lahdioui/9e81a2bf613ab178b9d7163ce981bac4 to your computer and use it in GitHub Desktop.
Save ouadie-lahdioui/9e81a2bf613ab178b9d7163ce981bac4 to your computer and use it in GitHub Desktop.
Google Hangouts API
- event.space.type ==> "DM" or "ROOM"
- event.space.displayName
- event.space.name
- event.user.displayName
- event.message.text
# Events :
- MESSAGE
- ADDED_TO_SPACE
- REMOVED_FROM_SPACE
# Google authentification :
## Manually get a JWT :
let jwtClient = new google.auth.JWT(
gkeys.client_email,
null,
gkeys.private_key,
['https://www.googleapis.com/auth/chat.bot']
);
const jwtClient = new google.auth.fromJSON(gkeys);
jwtClient.scopes = ['https://www.googleapis.com/auth/chat.bot'];
jwtClient.authorize((err, tokens) => {
if (err) {
console.log('Error create JWT hangoutchat');
reject(err);
} else {
console.log("====>" + JSON.stringify(tokens));
resolve(tokens.access_token);
}
});
## Automaticaly get a JWT token or Google compute
function getAccessToken() {
let params = {
scopes: 'https://www.googleapis.com/auth/chat.bot'
};
return google
.auth
.getClient(params)
.then(res => res.getAccessToken());
}
## Send a Google Hangouts message via REST API :
function postMessage() {
return new Promise((resolve, reject) => {
let space = '0AEz1AAAAAE';
getAccessToken()
.then((result) => {
unirest.post(`https://chat.googleapis.com/v1/spaces/${space}/messages`)
.headers({
"Content-Type": "application/json",
"Authorization": "Bearer " + result.token
})
.send(JSON.stringify({
'text': 'Hello Botkit :)',
}))
.end((res) => {
console.log(JSON.stringify(res));
resolve();
})
}).catch((err) => {
reject(err);
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment