简体   繁体   English

Google Apps 脚本根据 email 正文中的关键字发送不同的自动回复

[英]Google Apps Script to send different auto-replies based on keyword in email body

Basically I am trying to setup different auto-replies based on the content of the email body.基本上我正在尝试根据 email 正文的内容设置不同的自动回复。 Below is my current code... In this case, I am trying to set it up so that if an email was sent with the body "test" it will auto-reply with "This is my test".下面是我当前的代码......在这种情况下,我正在尝试对其进行设置,以便如果 email 与正文“测试”一起发送,它将自动回复“这是我的测试”。 But if it's anything else it will say "Sorry, your keyword was not recognized".但如果是其他内容,它会说“抱歉,您的关键字未被识别”。 Preferably would like to add more than one keyword in the future...最好希望将来添加多个关键字...

However, no matter what I send, it is only auto-replying with "This is my test"....但是,无论我发送什么,它都只会自动回复“这是我的测试”......

Any thoughts on how to fix this?关于如何解决这个问题的任何想法?

function autoReplier() 
{var labelObj = GmailApp.getUserLabelByName('autoreply');
var gmailThreads;
var messages;
var sender;for (var gg = 0; gg < labelObj.getUnreadCount(); gg++) {
gmailThreads = labelObj.getThreads()[gg];
messages = gmailThreads.getMessages();
for (var ii = 0; ii < messages.length; ii++) 
  {if (messages[ii].isUnread()){msg = messages[ii].getPlainBody();
    if (msg = "test") {
        sender = messages[ii].getFrom();MailApp.sendEmail(sender, "Auto Reply", "This is my test");                                                                     
        messages[ii].markRead();
        messages[ii].moveToTrash();
    } else {
        sender = messages[ii].getFrom();MailApp.sendEmail(sender, "Auto Reply", "Sorry, your keyword was not 
recognized");                                                                         
        messages[ii].markRead();
        messages[ii].moveToTrash();
}
}
}
}
}

Explanation / Issues:说明/问题:

You have one obvious issue with your code and one additional modification you need to do according to your comments:您的代码有一个明显的问题,并且需要根据您的评论进行另一项修改:

  • Replace if(msg = "test") with if(msg == "test") otherwise the first if block statement will be always executed.if(msg = "test")替换为if(msg == "test")否则将始终执行第一个if块语句。

  • According to your comments msg is not just a single word but a text.根据您的评论, msg不仅仅是一个单词,而是一个文本。 And you want to check if test is between two words.并且您想检查test是否在两个单词之间。 Assuming that these words are this and that you can use the following regular expression to see if test is between this and that :假设这些词是this that您可以使用以下正则表达式来查看test是否介于thisthat之间:

     const regex = RegExp(/(?<=this.*?)test(?=.*?that)/); regex.test(msg) // this returns true if test is between this and that

The RegExp.prototype.test() method allows you to validate your regular expression and get true if there is a match between the regular expression and a specified string, in your case msg . RegExp.prototype.test()方法允许您验证您的正则表达式,并在正则表达式和指定字符串(在您的情况下为msg )之间存在匹配时为true

Solution:解决方案:

function autoReplier() 
{var labelObj = GmailApp.getUserLabelByName('autoreply');
var gmailThreads;
var messages;
const regex = RegExp(/(?<=this.*?)test(?=.*?that)/); //modify this and that if you want to use other words
var sender;
for (var gg = 0; gg < labelObj.getUnreadCount(); gg++) {
gmailThreads = labelObj.getThreads()[gg];
messages = gmailThreads.getMessages();
for (var ii = 0; ii < messages.length; ii++) 
  {if (messages[ii].isUnread()){msg = messages[ii].getPlainBody();
    if (regex.test(msg)) {
        sender = messages[ii].getFrom();
        MailApp.sendEmail(sender, "Auto Reply", "This is my test");                                                                     
        messages[ii].markRead();
        messages[ii].moveToTrash();
    } else {
        sender = messages[ii].getFrom();
        MailApp.sendEmail(sender, "Auto Reply", "Sorry, your keyword was not recognized");                                                                         
        messages[ii].markRead();
        messages[ii].moveToTrash();
}}}}
}

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

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