Last active
July 25, 2024 23:09
-
-
Save timgcarlson/503d7ec7a712855df5cf289e85230e39 to your computer and use it in GitHub Desktop.
Google Apps Script: Post Daily OOO Events to Slack
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
function postDailyOOOEventsToSlack() { | |
// The alphanumeric portion of your Google calendar ID | |
const calendarId = ''; | |
const fullCalendarId = `${calendarId}@group.calendar.google.com`; | |
// The link to your Slack app with an Incoming Webhook | |
const slackWebhookUrl = ''; | |
// Update this URL with your expected timezone | |
const calendarUrl = `https://calendar.google.com/calendar/embed?src=${calendarId}%40group.calendar.google.com&ctz=America%2FLos_Angeles/event`; | |
const today = new Date(); | |
const startOfToday = new Date(today.setHours(0, 0, 0, 0)); | |
const endOfDay = new Date(today.setHours(23, 59, 59, 999)); | |
const formattedToday = Utilities.formatDate(today, Session.getScriptTimeZone(), "EEE, MMM d, yyyy"); | |
const events = CalendarApp.getCalendarById(fullCalendarId).getEvents(startOfToday, endOfDay); | |
let blocks = [ | |
{ | |
"type": "section", | |
"text": { | |
"type": "mrkdwn", | |
"text": `*<${calendarUrl}|Out of Office> events for ${formattedToday}:*\n\n` | |
} | |
} | |
]; | |
const timeZone = Session.getScriptTimeZone(); | |
events.forEach((event, index) => { | |
const eventTitle = event.getTitle(); | |
const eventDescription = event.getDescription(); | |
const startTime = event.getStartTime(); | |
const endTime = event.getEndTime(); | |
const isAllDay = event.isAllDayEvent(); | |
let timeString = ""; | |
if (isAllDay) { | |
const totalDays = Math.ceil((endTime - startTime) / (1000 * 60 * 60 * 24)); | |
const currentDay = Math.ceil((startOfToday.getTime() - startTime.getTime()) / (1000 * 60 * 60 * 24)) + 1; | |
if (totalDays > 1) { | |
const startMonth = Utilities.formatDate(startTime, timeZone, "MMM d"); | |
const endMonth = Utilities.formatDate(endTime, timeZone, (startTime.getMonth() === endTime.getMonth() ? "d" : "MMM d")); | |
const endYear = startTime.getFullYear() === endTime.getFullYear() ? "" : `, ${endTime.getFullYear()}`; | |
timeString = `${startMonth} - ${endMonth}${endYear} (Day ${currentDay} of ${totalDays})`; | |
} else { | |
timeString = "All Day"; | |
} | |
} else { | |
const formattedStartTime = Utilities.formatDate(startTime, timeZone, "h:mma").toLowerCase().replace("am", "a").replace("pm", "p"); | |
const formattedEndTime = Utilities.formatDate(endTime, timeZone, "h:mma").toLowerCase().replace("am", "a").replace("pm", "p"); | |
const timeZoneName = (new Date()).toLocaleTimeString('en-us', { timeZoneName: 'short' }).split(' ')[2]; | |
if (startTime.getDate() !== endTime.getDate()) { | |
const endDate = Utilities.formatDate(endTime, timeZone, "MMM d"); | |
timeString = `${formattedStartTime} - ${endDate} ${formattedEndTime} (${timeZoneName})`; | |
} else { | |
timeString = `${formattedStartTime} - ${formattedEndTime} (${timeZoneName})`; | |
} | |
} | |
blocks.push( | |
{ | |
"type": "section", | |
"fields": [ | |
{ | |
"type": "mrkdwn", | |
"text": `*${eventTitle}*` | |
}, | |
{ | |
"type": "mrkdwn", | |
"text": `${timeString}` | |
} | |
] | |
} | |
); | |
if (eventDescription) { | |
blocks.push( | |
{ | |
"type": "section", | |
"text": { | |
"type": "mrkdwn", | |
"text": `_${eventDescription}_` | |
} | |
} | |
); | |
} | |
if (index < events.length - 1) { | |
blocks.push( | |
{ | |
"type": "divider" | |
} | |
); | |
} | |
}); | |
if (events.length === 0) { | |
blocks.push( | |
{ | |
"type": "section", | |
"text": { | |
"type": "mrkdwn", | |
"text": `No out of office events for today.` | |
} | |
} | |
); | |
} | |
const payload = { | |
blocks: blocks | |
}; | |
const options = { | |
method: 'post', | |
contentType: 'application/json', | |
payload: JSON.stringify(payload) | |
}; | |
UrlFetchApp.fetch(slackWebhookUrl, options); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Setting Up Daily OOO Updates to Slack
To make use of this script to post daily updates to your Slack channel, follow these steps:
In Google Calendar
In Slack
In Google Apps Script
@group.calendar.google.com
from the ID that you copied in Google Calendar.postDailyOOOEventsToSlack
function.