Forked from mburtscher/Google Calendar for Teams.gs
Last active
September 26, 2024 19:29
-
-
Save ogoldberg/9a807342ed1c508800e62a64b75ba623 to your computer and use it in GitHub Desktop.
Slack "Google Calendar for Teams" replacement
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var webhookUrl = 'https://hooks.slack.com/services/XXXXXXXXXX/XXXXXXXXXX/XXXXXXXXXX'; // Webhook URL | |
var calendarId = '[email protected]'; // Id of the calendar | |
var slackChannel = '#team-calendar'; // Name of Slack channel to post updates to | |
var threshold = 30; // Seconds to look back after "onEventUpdated" event was received | |
var messageSingle = 'There is *1 event*'; | |
var messageMultiple = 'There are *{events} events*'; | |
var messageCreated = 'A *new event* has been created:'; | |
function sendDailyEventsSummary() { | |
sendEventsSummary(1, "today") | |
} | |
function sendWeeklyEventsSummary() | |
{ | |
sendEventsSummary(7, "this week") | |
} | |
function sendEventsSummary(numOfDays, periodDescriptor) { | |
var start = new Date(); | |
start.setHours(0, 0, 0, 0); | |
var end = new Date(start.getTime() + numOfDays * 24 * 60 * 60 * 1000); | |
var events = Calendar.Events.list(calendarId, { | |
timeMin: start.toISOString(), | |
timeMax: end.toISOString(), | |
singleEvents: true, | |
orderBy: 'startTime' | |
}).items; | |
if (events.length === 0) { | |
Logger.log('No events found.'); | |
return; | |
} | |
// Build the message | |
var message = composeMessageHeader(events.length, periodDescriptor) | |
for (var event of events) { | |
message += `\n\n` + formatEvent(event); | |
} | |
sendMessage(message); | |
} | |
function composeMessageHeader(eventCount, periodDescriptor) { | |
if (eventCount == 1) { | |
return `${messageSingle} ${periodDescriptor}` | |
} else { | |
return `${messageMultiple.replace('{events}', eventCount)} ${periodDescriptor}`; | |
} | |
} | |
function onEventUpdated(e) | |
{ | |
var syncStartTime = new Date(new Date() - threshold * 1000); | |
var events = CalendarApi.Events.list(calendarId, { | |
updatedMin: syncStartTime.toISOString() | |
}).items; | |
for (var event of events) { | |
if (event.status === 'cancelled') { | |
// Event has been cancelled / deleted. | |
continue; | |
} | |
var created = new Date(event.created); | |
if (created < syncStartTime) { | |
// Event has been created earlier and was only updated. | |
continue; | |
} | |
sendMessage(messageCreated + '\n\n' + formatEvent(event)); | |
} | |
} | |
function formatEvent(event) { | |
var result = `> *<${event.htmlLink}|${event.summary}>*\n`; | |
if (event.start.date){ | |
result += `> ${(new Date(event.start.date)).toLocaleString('en-US', {dateStyle: 'full', timeStyle: 'short'})}`; | |
} else if (event.start.dateTime) { | |
result += `> ${(new Date(event.start.dateTime)).toLocaleString('en-US', {dateStyle: 'full', timeStyle: 'short'})}`; | |
} | |
if (event.location) { | |
result += `\n> <https://maps.google.com/maps?q=${encodeURIComponent(event.location)}|${event.location}>`; | |
} | |
return result; | |
} | |
function sendMessage(message) { | |
return UrlFetchApp.fetch(webhookUrl, { | |
"method" : "post", | |
"contentType" : "application/json", | |
"payload" : JSON.stringify({ | |
"channel": "#events", | |
"username": "Events", | |
"text": message, | |
"icon_emoji": ":calendar:" | |
}) | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the script, but seems not work for me. I use calendar address var calendarId = '[email protected]';