我如何向这个 Slack 机器人添加功能以取代即将消失的“谷歌团队活动日历”

问题描述 投票:0回答:0

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 功能组装一个基本的机器人,但是它缺少一些我无法弄清楚的功能:

它具有的特点:

  • 每周总结【我在周日晚上设置了事件触发】

它缺乏的功能:

  • 在活动前 2 天发布提醒
  • 在活动当天的指定时间发布提醒
  • 在事件创建或更新时得到通知 [事件的标题、时间或位置更新] 并且只发布该事件
  • Slackbot 将每个事件作为一个单独的类别发送[目前,它们都是捆绑的]
// 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)
}
javascript calendar google-calendar-api slack-api collaboration
© www.soinside.com 2019 - 2024. All rights reserved.