通过电子邮件触发 Google Apps 脚本 [英] Trigger Google Apps Script by email

查看:52
本文介绍了通过电子邮件触发 Google Apps 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种模式示例,其中在 GoogleAppsForBusiness 域中运行的恶魔脚本可以解析传入的电子邮件.某些消息将包含对不同 GAScript 的调用,例如,可以更改特定文档的 ACL 设置.

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.

谢谢

推荐答案

您可以在 Apps Script 用户指南中找到脚本示例教程.您还可以在论坛上搜索相关讨论.但我不认为有一个完全适合您的,所有代码肯定都在那里,但不是在一个脚本上.

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.

可能有人写了这样的脚本但从未发布过.因为做起来有点简单,而且每个人的用法都不一样.例如,您打算如何标记您的电子邮件(您已经阅读、执行等的电子邮件)?使用 gmail 过滤器来帮助您可能会很好,立即将命令"电子邮件放在标签中,脚本只是删除标签(并可能设置另一个标签).重点是,看看它有什么不同.

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:我还没有测试过这段代码

ps: I have not tested this code

这篇关于通过电子邮件触发 Google Apps 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆