Search code examples
google-apps-scriptgmail

Gmail auto reply script - how to set conditional time and stop multiple replies?


I have adapted a Gmail auto reply script to send emails to people who message me on my non-working days. I would like the script to begin sending replies on a Friday from 16:30, but I'm not sure if my code is correct.

My script did not appear to be sending any replies this past Friday when tested, but was working fine on Saturday, Sunday and Monday.

function autoReply() {
var interval = 5;
  var date = new Date();
  var day = date.getDay();
  var hour = date.getHours();
  var minute = date.getMinutes();
  if ([1,6,0].indexOf(day) > -1 || (day == 1 && hour < 8) || (day == 5 && hour == 16 && minute >= 30)) {
    var timeFrom = Math.floor(date.valueOf()/1000) - 60 * interval;
    var threads = GmailApp.search('is:inbox after:' + timeFrom);
    for (var i = 0; i < threads.length; i++) {
      if (threads[i].isUnread()){
        threads[i].reply("Hi," + "\n\n Thanks for your email. My working days are Tuesday to Friday, so I won't pick up your message until Tuesday morning.");
      threads[i].markRead();
      threads[i].star();
      }
    }
  }
}

Additionally, is there any way to adapt my script so that it won't send the auto reply message to senders that have already received it (as with Gmail's standard auto reply feature). I have searched Stack Overflow for this solution, using terms such as 'conditional replies', but to no avail. (I appreciate I may be using the wrong terms entirely!)

Any help would be greatly appreciated!


Solution

  • You need to complement your if statement by the condition (day == 5 && hour >= 17)

    To verify either an email has been sent to certain senders already, you can use PropertiesService. This allows you to save the senders in cache and to compare for every new email either its sender is already contained in the properties:

    function autoReply() {
    var interval = 5;
      var date = new Date();
      var day = date.getDay();
      var hour = date.getHours();
      var minute = date.getMinutes();
      if ([1,6,0].indexOf(day) > -1 || (day == 1 && hour < 8) || (day == 5 && hour == 16 && minute >= 30)|| (day == 5 && hour >= 17)){
        var timeFrom = Math.floor(date.valueOf()/1000) - 60 * interval;
        var threads = GmailApp.search('is:inbox after:' + timeFrom);
        for (var i = 0; i < threads.length; i++) {
          if (threads[i].isUnread()){
             var sender=threads[i].getMessages()[0].getFrom();
            if(PropertiesService.getScriptProperties().getKeys().length==0){
               PropertiesService.getScriptProperties().setProperty('from', '');
             }
            var scriptProperties = PropertiesService.getScriptProperties().getProperty('fromArray');
            if(scriptProperties.indexOf(sender)==-1){
              threads[i].reply("Hi," + "\n\n Thanks for your email. My working days are Tuesday to Friday, so I won't pick up your message until Tuesday morning.")
              threads[i].markRead();
              threads[i].star();
              scriptProperties=scriptProperties+sender; 
              PropertiesService.getScriptProperties().setProperty('from',  scriptProperties);
              }
          }
        }
      }
    }