tmkiefer
tmkiefer

Reputation: 235

Trouble Sending Notifications to Guests for New Calendar Event -- Google Apps

On the final leg of my first solo practical app, and I just cannot seem to get this last piece.

Below is my code, which adds user to s specific calendar event. addGuest is working -- when it runs, they are added to that event.

They need to get an email, however, so they have confirmation of event. The old automatically generated g-cal invite email would do just fine.

From what I can tell, this should work, however it only adds guest, no email sent when testing:

function assignToCal() {
  var row = scheduled.getLastRow();
  var timePicked = scheduled.getRange(row, 2).getValue();
  var email = scheduled.getRange(row, 3).getValue();
  var calId;

  for(var i = 0; i < slotTimes.length; i++)
  {
    if(slotTimes[i] == timePicked)
    {
      calId = slotIds[i];
    }
  }

  var session = calendar.getEventSeriesById(calId);
  session.addGuest(email);


  function sendInvite(calendar, calId, email) {
    var event = Calendar.Events.get(calendar, calId);
    event.attendees.push({
      email: email
    });
    event = Calendar.Events.patch(event, calendarId, eventId, {
      sendNotifications: true
    });
  }

  Logger.log(timePicked);
  Logger.log(calId);
};

Another option seems to be using this POST request to update sendNotifications for guests. However, to be completely honest I've never done an http request, and have also been attempting this for the past couple days and am just confused. If anyone has advice to make that work for what needs to be done here, that's cool, too.

Thanks!

Upvotes: 0

Views: 1242

Answers (1)

Teyam
Teyam

Reputation: 8082

As a given solution given in issue #574 in Google Apps Script Issues tracker, you can also try using Advanced Calendar service. As mentioned,

You can use Calendar.Events.patch() to add attendees, and if you set the optional parameter sendNotifications to "true" the attendees will get an invite.

Sample code implementation:

function sendInvite(calendarId, eventId, email) {
  var event = Calendar.Events.get(calendarId, eventId);
  if(event.attendees) {
    event.attendees.push({
      email: email
    });
  } else {
    event.attendees = new Array({email: email});
  }
  event = Calendar.Events.patch(event, calendarId, eventId, {
    sendNotifications: true
  });
}

Important Note: You won't receive an email notification if you're the event owner and you're trying to add your own email address.

To better understand how to implements this, please also check the additional explanations in Issue #574. Hope that helps!

Upvotes: 1

Related Questions