Skip to content

Instantly share code, notes, and snippets.

@Vonter
Created March 2, 2023 12:34
Show Gist options
  • Select an option

  • Save Vonter/28c2ec619d826354acfebe1e6a47c6f6 to your computer and use it in GitHub Desktop.

Select an option

Save Vonter/28c2ec619d826354acfebe1e6a47c6f6 to your computer and use it in GitHub Desktop.
Google Apps Script for sending Google Calendar event notifications to Mattermost. Inspired by: https://github.com/DjMuffinTops/Google-Calendar-API-to-Discord-Webhook
// Inspired by: https://github.com/DjMuffinTops/Google-Calendar-API-to-Discord-Webhook
// This Google Apps Script Will Send a POST to a Mattermost Webhook creating embed messages of any events starting within the next minute of execution.
// Any events that have already started will not appear.
// This script should be triggered every minute using Google Triggers.
const CHANNEL_POST_URL = "MATTERMOST_WEBHOOK";
const CALENDAR_ID = "CALENDAR_ID";
const NO_VALUE_FOUND = "N/A";
const minsInAdvance = 15; // Set the number of minutes in advance you'd like events to be posted. Must be 1 or greater
// Import Luxon
eval(UrlFetchApp.fetch('https://cdn.jsdelivr.net/npm/luxon@2.0.2/build/global/luxon.min.js').getContentText());
let DateTime = luxon.DateTime;
const DTnow = DateTime.now().startOf('minute'); // Will consider 'now' as the beginning the minute to deal with second offsets issues with trigger over time.
function postEventsToChannel() {
// .list parameters. See https://developers.google.com/calendar/api/v3/reference/events/list?hl=en
let optionalArgs = {
timeMin: DTnow.toISO(),
timeMax: DTnow.plus({minutes: minsInAdvance}).toISO(), // Will only show events starting in the next x minutes
showDeleted: false,
singleEvents: true,
orderBy: 'startTime'
};
let response = Calendar.Events.list(CALENDAR_ID, optionalArgs);
let events = response.items;
if (events.length > 0) {
for (i = 0; i < events.length; i++) {
let event = events[i];
let ISOStartDate = event.start.dateTime || event.start.date;
let ISOEndDate = event.end.dateTime || event.end.date;
// The Calendar API's .list function will continously return events whose endDate has not been reached yet (timeMin is based on the event's end time)
// Since this script is meant to run every minute, we have to skip these events ourselves
if (DateTime.fromISO(ISOStartDate) < DTnow.plus({minutes: minsInAdvance - 1})) {
Logger.log(`Event ${event.summary} [${event.id}] has already started. Skipping`);
continue;
}
let StartDate = DateTime.fromISO(ISOStartDate).toLocaleString(DateTime.TIME_SIMPLE);
let EndDate = DateTime.fromISO(ISOEndDate).toLocaleString(DateTime.TIME_SIMPLE);
// Build the POST request
let options = {
"method": "post",
"headers": {
"Content-Type": "application/json",
},
"payload": JSON.stringify({
"text": `[Google Calendar Event](${event.htmlLink}) \n **Start:** ${StartDate} \n **End:** ${EndDate} \n **Details:** ${event.summary}`
})
};
Logger.log(options, null, 2);
UrlFetchApp.fetch(CHANNEL_POST_URL, options);
}
} else {
Logger.log(`No events starting within ${minsInAdvance} minute(s) found.`);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment