通过电子邮件触发Google Apps脚本 [英] Trigger Google Apps Script by email
问题描述
我正在寻找模式示例,其中在GoogleAppsForBusiness域中运行的恶魔脚本可以解析传入的电子邮件。有些消息将包含对不同GAScript的调用,例如,可以更改特定文档的ACL设置。
我假设已经有其他人实施这种模式,但不知道我怎么去找到例子。
thx
您可以在Apps脚本用户指南和tutorials 。您也可以在论坛上搜索相关讨论。但我认为没有一个能够完全符合你的要求,所有的代码都在那里,但不是在一个脚本上。
有人可能写了这样的脚本并从未发布它。由于做起来有些简单,每个人的使用都不一样。例如,你打算如何标记你的电子邮件(你已经阅读,执行过的那些)?使用gmail过滤器可以很好地帮助你,将命令电子邮件放在标签上,而脚本只是删除标签(可能会设置另一个标签)。要点是,看它有多大差别。
另外,如果你可以将所有的功能放在同一个脚本项目中,我认为它更容易。可能只是在不同的文件。因为调用不同的脚本比较复杂。无论如何,他是我的开始:
//设置一个时间驱动的触发器,以期望的频率运行此函数
function monitorEmails(){
var label = GmailApp.getUserLabelByName ('命令');
var doneLabel = GmailApp.getUserLabelByName('executed');
var cmds = label.getThreads();
var max = Math.min(cmds.length,5);
for(var i = 0; i var email = cmds [i] .getMessages()[0];
var functionName = email.getBody();
//你可能需要在这里做额外的解析,这取决于你的使用情况
var ret = undefined;
尝试{
ret = this [functionName]();
} catch(err){
ret = err;
}
//将函数返回值回复给电子邮件
//这可能有意义
if(ret!==未定义)
email.reply (RET);
cmds [i] .removeLabel(label).addLabel(doneLabel);
ps:我没有测试过这段代码
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
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
这篇关于通过电子邮件触发Google Apps脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!