CONTEXT: Read Reddit thread here
This Slack integration with Google Calendar is sunsetting on March 1, 2023 and there are currently no alternatives
https://slack.com/help/articles/360047938054-Google-Calendar-for-Team-Events-for-Slack
I was able to hack together a rudimentary bot using Google Scripts + Slack's Incoming Web Hooks functionality however it is missing a few features I couldn't figure out:
Features it has:
- Weekly summaries [I set an event trigger on Sunday night]
Features it lacks:
- Post reminders 2 days before an event
- Post a reminder the day-of the event at a specified time
- Get notified when an event is created or updated [an event's title, time, or location is updated] and ONLY post that event
- Slackbot sends each event as a separate category [currently, they are all bundled]
Steps to get setup:
Create a Webhook
- Create an incoming webhook in Slack.
- Go to Slack custom integrations directory > Search for “Incoming WebHooks” > Click “Add configuration”. Fill out the details for the Slack channel to which your Slackbot will post, a description of your Slackbot, etc. (the script can override these fields, just name the webhook something you'll remember.)
- Copy and save the webhook url for use later.
Then create a Google Script here
- Click 'Services' and add the 'AdSense' and 'Calendar' services
Now, copy in the below code and add your custom webhook for the WebhookURL variable
// Resources https://www.kutil.org/2016/02/integrate-google-apps-and-slack-with.html
// Slack messages formatting reference https://app.slack.com/block-kit-builder/T0247NYLG9Y
// This is the code for updating the vacations channel with upcoming PTO
function UpcomingPTO() {
var WebhookURL = "https://hooks.slack.com/services/XXX/YYYY/ZZZZZZZ";
// Define PTO Calendar ID variables
var PTOCal = '[email protected]'
var cal = CalendarApp.getCalendarById(PTOCal);
// Sets how far in the future to look for events and get the color of the calendar
var StartTime = new Date();
var EndTime = new Date(StartTime.getTime() + 7776000000); //90 days from today
var color = cal.getColor();
var events = cal.getEvents(StartTime, EndTime);
var EventTitles = [];
for (var i= 0; i < events.length; i++) {
var NextEvent = [];
NextEvent.push(events[i].getTitle())
var NextEventStartDate = [];
NextEventStartDate.push(events[i].getStartTime())
var NextEventEndDate = [];
NextEventEndDate.push(events[i].getEndTime())
var StartDate = new Date(NextEventStartDate).toLocaleDateString('en-us', { weekday: 'short', month: 'short', day: 'numeric' })
var EndDate = new Date(NextEventEndDate).toLocaleDateString('en-us', { weekday: 'short', month: 'short', day: 'numeric', year: 'numeric' })
EventTitles.push("\n\n *" + NextEvent + "* \n" + StartDate + " to " + EndDate);
}
var payload = {
"channel" : "#1-vacations", // <-- optional parameter, use if you want to override default channel
"username" : "Team PTO", // <-- optional parameter, use if you want to override default "robot" name
"text" : 'There are ' + events.length + ' upcoming PTO events soon',
"icon_emoji": ":palm_tree:", // <-- optional parameter, use if you want to override default icon,
"attachments":[
{
"color": color,
"fields":[
{
"title": EventTitles, // The title may not contain markup and will be escaped for you
"value": String(EventTitles), // Text value of the field. May contain standard message markup and must be escaped as normal and multi-line
"short":false // Optional flag indicating whether the `value` is short enough to be displayed side-by-side with other values
}
]
}
]
}
Logger.log(events)
Logger.log(NextEvent)
Logger.log(EventTitles)
sendToSlack_(WebhookURL,payload)
}
function sendToSlack_(WebhookURL,payload) {
var options = {
"method" : "post",
"contentType" : "application/json",
"payload" : JSON.stringify(payload)
};
return UrlFetchApp.fetch(WebhookURL, options)
}