14
votes

I'm looking for examples of a pattern where a demon script running within a GoogleAppsForBusiness domain can parse incoming email messages. Some messages will will contain a call to yet a different GAScript that could, for example, change the ACL setting of a specific document.

I'm assuming someone else has already implemented this pattern but not sure how I go about finding examples.

thx

3

3 Answers

14
votes

You can find script examples in the Apps Script user guide and tutorials. You may also search for related discussions on the forum. But I don't think there's one that fits you exactly, all code is out there for sure, but not on a single script.

It's possible that someone wrote such script and never published it. Since it's somewhat straightforward to do and everyone's usage is different. For instance, how do you plan on marking your emails (the ones you've already read, executed, etc)? It may be nice to use a gmail filter to help you out, putting the "command" emails in a label right away, and the script just remove the label (and possibly set another one). Point is, see how it can differ a lot.

Also, I think it's easier if you can keep all functions in the same script project. Possibly just on different files. As calling different scripts is way more complicated.

Anyway, he's how I'd start it:

//set a time-driven trigger to run this function on the desired frequency
function monitorEmails() {
  var label = GmailApp.getUserLabelByName('command');
  var doneLabel = GmailApp.getUserLabelByName('executed');
  var cmds = label.getThreads();
  var max = Math.min(cmds.length,5);
  for( var i = 0; i < max; ++i ) {
    var email = cmds[i].getMessages()[0];
    var functionName = email.getBody();
    //you may need to do extra parsing here, depending on your usage

    var ret = undefined;
    try {
      ret = this[functionName]();
    } catch(err) {
      ret = err;
    }
    //replying the function return value to the email
    //this may make sense or not
    if( ret !== undefined )
      email.reply(ret);
    cmds[i].removeLabel(label).addLabel(doneLabel);
  }
}

ps: I have not tested this code

6
votes

You can create a google app that will be triggered by an incoming email message sent to a special address for the app. The message is converted to an HTTP POST which your app receives.

More details here: https://developers.google.com/appengine/docs/python/mail/receivingmail

I havn't tried this myself yet but will be doing so in the next few days.

0
votes

There are two ways. First you can use Google pub/sub and handle incomming notifications in your AppScrit endpoint. The second is to use the googleapis npm package inside your AppScript code an example here. Hope it helps.

These are the steps:

  • made a project on https://console.cloud.google.com/cloudpubsub/topicList?project=testmabs thing?
  • made a pubsub topic
  • made a subscription to the webhook url
  • added that url to the sites i own, i guess? I think I had to do DNS things to confirm i own it, and the error was super vague to figure out that was what i had to do, when trying to add the subscription
  • added permission to the topic for "[email protected]" as publisher (I also added ....apps.googleusercontent.com and [email protected] but i dont think I needed them)
  • created oauth client info and downloaded it in the credentials section of the google console. (oauthtrash.json)