Skip to content

Instantly share code, notes, and snippets.

@nabinno
Created September 3, 2019 01:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nabinno/6f0aabfefe42f40ef35b3d776d4d0cff to your computer and use it in GitHub Desktop.
Save nabinno/6f0aabfefe42f40ef35b3d776d4d0cff to your computer and use it in GitHub Desktop.
function main() {
var events = listUpEventOfCalendar();
var list = formatEvents(events);
Logger.log(list);
postToSlack(list);
}
function listUpEventOfCalendar() {
var events = [];
var cals = CalendarApp.getAllCalendars();
cals.forEach (function (cal) {
var calEvents = cal.getEventsForDay(new Date());
calEvents.forEach (function (calEvent) {
events.push(calEvent);
});
});
events.sort(function(a, b) {
return (a.getStartTime() <= b.getStartTime() ? -1 : 1);
});
return events;
}
function formatEvents(events) {
var list = "";
var titles = [];
events.forEach (function (e) {
var creators = e.getCreators() + "";
var s = creators.replace(/@.+/, '') + ": ";
var title = e.getTitle();
var startTime = e.getStartTime();
var endTime = e.getEndTime();
var location = e.getLocation();
if (
titles.indexOf(title) != -1 ||
!(title.match(/foo/) || title.match(/bar/))
) {
return;
}
titles.push(title);
if (e.isAllDayEvent()) {
s += Utilities.formatDate(startTime, "GMT+0900", "MM/dd");
s += " 終日 ";
} else {
s += Utilities.formatDate(startTime, "GMT+0900", "HH:mm");
s += Utilities.formatDate(endTime, "GMT+0900", "〜HH:mm ");
if (location != "") {
s += "@" + location + " ";
}
}
s += title;
list += s + "\n";
});
return list;
}
function postToSlack(list) {
var now = new Date();
var nowDay = now.getDay();
if ([0, 6].indexOf(nowDay) !== -1) return;
var week = ['日', '月', '火', '水', '木', '金', '土'];
var nowDate = ((now.getMonth() + 1) + '/' + now.getDate() + ' (' + week[nowDay] + ')');
if (list == "") list = "\n.";
var payload = {
"text": nowDate + "の予定をお知らせします。\n```" + list + "```\n※スケジュールは随時更新されます。\n最新のスケジュールはGoogleカレンダーで各自確認してください。\nhttps://www.google.com/calendar/",
"channel": "#announce_deployment", // 送信したいチャンネル
"username": "GoogleCalendar", // 送信者名
"icon_emoji": ":timer_clock:" // 送信時のアイコン
}
var response = UrlFetchApp.fetch(
"https://hooks.slack.com/services/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
{
"method": "POST",
"payload": JSON.stringify(payload)
}
);
return response.getContentText("UTF-8");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment