15

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 Answers 3

14

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

Sign up to request clarification or add additional context in comments.

6 Comments

Is there now way to trigger the script when an email is received (Instead of just setting a timer)?
@threed Not that I know of
Well that's just useless then. Nothing like procmail on GMail?
Here's a direct link to the gmail docs: developers.google.com/apps-script/reference/gmail
great idea. I will now test it.
|
6

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.

4 Comments

The question was asking about a solution in Google Apps Script, not AppEngine.
Ah well that's the result of having too many different products with a similar name, much overlap and confusion... apologies for trying my best to be helpful. My own implementation was successful by the way.
@EdRandall I know it's been a while, but do you mind sharing the basic logic that you implemented? I find myself in need of something similar, having to trigger a python script hopefully via an email (python script now resides on an EC2 instance, but I could very well move to AppENgine, is it the same thing?) Thanks
@newyuppie sure, the source of my app is in bitbucket.org/edrandall/sdn-email The app.yaml config file "handlers" directive points you to main.app (which is main.py), the work is done in sdn/sdn_email.py
0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.