简体   繁体   English

通过电子邮件触发Google Apps脚本

[英]Trigger Google Apps Script by email

I'm looking for examples of a pattern where a demon script running within a GoogleAppsForBusiness domain can parse incoming email messages. 我正在寻找一个模式示例,其中在GoogleAppsForBusiness域中运行的恶魔脚本可以解析传入的电子邮件。 Some messages will will contain a call to yet a different GAScript that could, for example, change the ACL setting of a specific document. 某些消息将包含对不同GAScript的调用,例如,可能会更改特定文档的ACL设置。

I'm assuming someone else has already implemented this pattern but not sure how I go about finding examples. 我假设其他人已经实现了这种模式但不确定我如何寻找示例。

thx 谢谢

You can find script examples in the Apps Script user guide and tutorials . 您可以在Apps脚本用户指南教程中找到脚本示例。 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). 使用gmail过滤器来帮助你,将“命令”电子邮件立即放入标签中,并且脚本只需删除标签(并可能设置另一个标签),这可能会很不错。 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 ps:我还没有测试过这段代码

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. 邮件将转换为您的应用程序收到的HTTP POST。

More details here: https://developers.google.com/appengine/docs/python/mail/receivingmail 有关详细信息,请访问: 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. 我自己还没有试过这个,但是接下来的几天就会这样做。

There are two ways. 有两种方法。 First you can use Google pub/sub and handle incomming notifications in your AppScrit endpoint. 首先,您可以使用Google pub / sub并在AppScrit端点处理incomming通知。 The second is to use the googleapis npm package inside your AppScript code an example here . 第二个是在你的AppScript代码中使用googleapis npm包这里的一个例子 Hope it helps. 希望能帮助到你。

These are the steps: 这些是步骤:

  • made a project on https://console.cloud.google.com/cloudpubsub/topicList?project=testmabs thing? https://console.cloud.google.com/cloudpubsub/topicList?project=testmabs上制作了一个项目?
  • made a pubsub topic 做了一个pubsub主题
  • made a subscription to the webhook url 订阅了webhook网址
  • 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 我想我必须做DNS事情以确认我拥有它,并且错误是超级模糊的,以确定这是我必须做的,当试图添加订阅时
  • added permission to the topic for "gmail-api-push@system.gserviceaccount.com" as publisher (I also added ....apps.googleusercontent.com and youtrackapiuser.caps@gmail.com but i dont think I needed them) 添加了“gmail-api-push@system.gserviceaccount.com”主题作为发布者的权限(我还添加了.... apps.googleusercontent.com和youtrackapiuser.caps@gmail.com但我认为我不需要它们)
  • created oauth client info and downloaded it in the credentials section of the google console. 创建了oauth客户端信息并将其下载到谷歌控制台的凭据部分。 (oauthtrash.json) (oauthtrash.json)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM