Last active
November 29, 2022 23:01
-
-
Save danieljin/342de40c8f3eee89a8fc5c0148ae474f to your computer and use it in GitHub Desktop.
Slack Google Calendar for Team Events Replacement using Googgle App Scripts
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
// google configs | |
const calendarID = 'XXXXXXXX@import.calendar.google.com'; | |
// slack configs | |
const webhookUrl = 'https://hooks.slack.com/services/XXXXXX/YYYYYYY/ZZZZZZ'; | |
const channel = '#xxx' | |
const username = 'XXXXXX' | |
// note: this can be cleaned up but wanted to get something quick and dirty out there. | |
// throw this in a new App Script on https://script.google.com and have it trigger to run once a day. | |
function execute() { | |
const cal = CalendarApp.getCalendarById(calendarID); | |
let today = new Date(); | |
let tomorrow = new Date(today); | |
tomorrow.setDate(tomorrow.getDate() + 1); | |
let saturday = getNextSaturday(new Date(today)); | |
let todaysEvents = cal.getEventsForDay(today).map(cleanEvent); | |
let upcomingEvents = cal.getEvents(tomorrow,saturday).map(cleanEvent); | |
upcomingEvents = upcomingEvents.filter(upcomingEvent => !todaysEvents.some(todaysEvent => upcomingEvent.eventID === todaysEvent.eventID)); | |
// should only be called monday through friday | |
if (today.getDay() < 6) { | |
messageSlack(convertEventsToAttachments(todaysEvents), 1); | |
messageSlack(convertEventsToAttachments(upcomingEvents), 0); | |
} | |
} | |
function cleanEvent(event) { | |
return { | |
eventID: event.getId(), | |
eventTitle: event.getTitle(), | |
startDate: event.isAllDayEvent() ? event.getAllDayStartDate() : event.getStartTime(), | |
endDate: event.isAllDayEvent() ? event.getAllDayEndDate() : event.getEndTime() | |
}; | |
} | |
function convertEventsToAttachments(events) { | |
return events.map((event) => { | |
if (event.startDate == event.endDate) { | |
finalDateString = Utilities.formatDate(event.startDate, 'GMT-5','MMMMM d, yyyy'); | |
} else { | |
finalDateString = Utilities.formatDate(event.startDate, 'GMT-5','MMMMM d - ') + Utilities.formatDate(new Date(event.endDate - 1), 'GMT-5','MMMMM d, yyyy'); | |
} | |
return { | |
'text': `*${event.eventTitle}*\r\n${finalDateString}`, | |
'mrkdwn_in': ['text'] | |
}; | |
}); | |
} | |
function getNextSaturday(today) { | |
return new Date(today.setDate(today.getDate() + ((7 - today.getDay() + 6) % 7 || 7))); | |
} | |
function messageSlack(events, isToday) { | |
let text = events.length == 0 ? `There are no events ${isToday ? 'today' : 'this week'}` : `There ${events.length > 1 ? 'are' : 'is'} *${events.length}* event${events.length > 1 ? 's' : ''} ${isToday ? 'today' : 'the rest of this week'}`; | |
let payload = { | |
'channel': channel, | |
'username' : username, | |
'icon_emoji' : ':calendar:', | |
'text' : text, | |
'attachments': events | |
}; | |
let options = { | |
'method': 'post', | |
'contentType': 'application/json', | |
'payload': JSON.stringify(payload) | |
}; | |
return UrlFetchApp.fetch(webhookUrl, options); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment