Slack 与 Google Calendar 的集成将于 2023 年 3 月 1 日终止,目前没有其他选择
https://slack.com/help/articles/360047938054-Google-Calendar-for-Team-Events-for-Slack
我能够使用 Google Scripts + Slack 的 Incoming Web Hooks 功能组装一个基本的机器人,但是它缺少一些我无法弄清楚的功能:
它具有的特点:
它缺乏的功能:
// 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)
}