Created
May 30, 2018 16:32
-
-
Save chocopie116/92209c5a9c84ef9688a23073be17cc8a to your computer and use it in GitHub Desktop.
GOOGLE CALENDAR SLACK BOT
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 doPost(e) { | |
var text = e.parameter.text; | |
var channel = e.parameter.channel_name; | |
//TODO validation error handling | |
//textには次のような文字列が入ることを期待していて、日付を抽出しようとしている => @yotei 2018/05/30 | |
var targetDate = text.replace('@yotei ', ''); | |
postGoogleCalendar(channel, targetDate); | |
return HtmlService.createHtmlOutput('ok'); | |
} | |
function postGoogleCalendar(channelName, targetDateString) { | |
var s = listupEvent("xxxxxx@gmail.com", targetDateString); // a.GoogleカレンダーのID | |
var message = "\n■" + targetDateString +"の予定\n" + s; // b.メッセージの見出し | |
var payload = { | |
"text" : message, | |
"channel" : channelName, | |
"icon_emoji" : ":cal:", | |
"username" : "google calendar", | |
} | |
postSlack(payload); | |
} | |
function listupEvent(cal_id, targetDateString) | |
{ | |
var list = ""; | |
var cal = CalendarApp.getCalendarById(cal_id); | |
var events = cal.getEventsForDay(new Date(targetDateString)); | |
if (events.length === 0) { | |
return "予定はありません\(^o^)/"; | |
} | |
for(var i=0; i < events.length; i++){ | |
s = ""; | |
if (events[i].isAllDayEvent()) { | |
s += "終日 "; | |
} else { | |
s += Utilities.formatDate(events[i].getStartTime(),"GMT+0900","HH:mm"); | |
s += Utilities.formatDate(events[i].getEndTime(), "GMT+0900","~ HH:mm "); | |
} | |
s += "|" + events[i].getTitle(); | |
list += s + "\n"; | |
} | |
return list; | |
} | |
function postSlack(payload) | |
{ | |
var options = { | |
"method" : "POST", | |
"payload" : JSON.stringify(payload) | |
} | |
var url = "https://hooks.slack.com/services/xxxx/xxxx/xxxxx"; //e.g. slack incoming webhook. | |
//TODO error handling | |
var response = UrlFetchApp.fetch(url, options); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment