-1

I'm looking to see if anyone has created a chatbot in google apps script to handle webhooks for hangouts? I have a bot created but I'm not sure on how to input the webhook url into the bots code so that the message can be deployed into the chat room.

Cooper
  • 59,616
  • 6
  • 23
  • 54
Aaron
  • 1
  • 1

1 Answers1

1

I have not exactly created what you are looking for, however I believe the answer you are looking for can be found here. Basically there is an event whenever your bot enters a space. When that event is triggered you can add the space id to a list stored somewhere.(Spreadsheet , PropertiesService,etc)

Once the list is stored you can deploy your application as a web app. You can read more about web apps here but two things you need to know is google gives you a url to make web requests to, as well as pair of events called doGet (when someone makes a get request) and doPost (when someone makes a post request). You can create the function do post and get the parameters when your web app is posted to.

Finally upon receiving the post you can do a fetch call to the google api to post the message you just received from the request to all of the spaces you are in by doing a api fetch call to each ID.

Below will be code directly posted from the API in the first link.

// Example bot for Hangouts Chat that demonstrates bot-initiated messages
// by spamming the user every minute.
//
// This bot makes use of the Apps Script OAuth2 library at:
//     https://github.com/googlesamples/apps-script-oauth2
//
// Follow the instructions there to add the library to your script.

// When added to a space, we store the space's ID in ScriptProperties.
function onAddToSpace(e) {
  PropertiesService.getScriptProperties()
      .setProperty(e.space.name, '');
  return {
    'text': 'Hi! I\'ll post a message here every minute. ' +
            'Please remove me after testing or I\'ll keep spamming you!'
  };
}

// When removed from a space, we remove the space's ID from ScriptProperties.
function onRemoveFromSpace(e) {
  PropertiesService.getScriptProperties()
      .deleteProperty(e.space.name);
}

// Add a trigger that invokes this function every minute via the 
// "Edit > Current Project's Triggers" menu. When it runs, it will
// post in each space the bot was added to.
function onTrigger() {
  var spaceIds = PropertiesService.getScriptProperties()
      .getKeys();
  var message = { 'text': 'Hi! It\'s now ' + (new Date()) };
  for (var i = 0; i < spaceIds.length; ++i) {
    postMessage(spaceIds[i], message);
  }
}
var SCOPE = 'https://www.googleapis.com/auth/chat.bot';
// The values below are copied from the JSON file downloaded upon
// service account creation.
var SERVICE_ACCOUNT_PRIVATE_KEY = '-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE 
KEY-----\n';
var SERVICE_ACCOUNT_EMAIL = 'service-account@project-id.iam.gserviceaccount.com';

// Posts a message into the given space ID via the API, using
// service account authentication.
function postMessage(spaceId, message) {
  var service = OAuth2.createService('chat')
      .setTokenUrl('https://accounts.google.com/o/oauth2/token')
      .setPrivateKey(SERVICE_ACCOUNT_PRIVATE_KEY)
      .setClientId(SERVICE_ACCOUNT_EMAIL)
      .setPropertyStore(PropertiesService.getUserProperties())
      .setScope(SCOPE);
  if (!service.hasAccess()) {
   Logger.log('Authentication error: %s', service.getLastError());
    return;
  }
  var url = 'https://chat.googleapis.com/v1/' + spaceId + '/messages';
   UrlFetchApp.fetch(url, {
    method: 'post',
    headers: { 'Authorization': 'Bearer ' + service.getAccessToken() },
    contentType: 'application/json',
    payload: JSON.stringify(message),
  });
}
John Thompson
  • 386
  • 3
  • 10
  • thanks, seems like a lot to do just to enable webhook's to work in hangout's chat, while with slack i can just grab the add-on and I'm set. – Aaron Jan 11 '19 at 18:56