Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
GOOGLE CALENDAR SLACK BOT
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
You can’t perform that action at this time.