Created
June 22, 2018 04:15
-
-
Save nyomo/50f1fd2e2ed268e0a123367535f3efdb to your computer and use it in GitHub Desktop.
今会社で使ってる奴。
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 myFunction() { | |
var email = ''//メールアドレス | |
var token = '' // https://api.slack.com/custom-integrations/legacy-tokens で発行できるトークン | |
var default_emoji = ':satisfied:' // 予定が無いか限定公開の場合に表示する emoji | |
var default_message = 'わーわー' // 予定が無いか限定公開の場合に表示するメッセージ | |
var ima = new Imananishiton(email, token, default_emoji, default_message) | |
ima.nanishiton() | |
} | |
var Imananishiton = function(email, token, default_emoji, default_message) { | |
this.email = email | |
this.token = token | |
this.default_emoji = default_emoji | |
this.default_message = default_message | |
} | |
Imananishiton.prototype = { | |
nanishiton: function() { | |
var events = this.getCurrentEvents() | |
var status = [ | |
CalendarApp.GuestStatus.OWNER, | |
CalendarApp.GuestStatus.YES, | |
CalendarApp.GuestStatus.MAYBE, | |
CalendarApp.GuestStatus.INVITED | |
] | |
var filtered_events = events.filter(function(e) { | |
return (status.indexOf(e.getMyStatus()) >= 0) | |
}) | |
if(filtered_events.length == 0){ | |
this.changeSlackStatus("", "") | |
} | |
filtered_events.forEach(function(filtered_event){ | |
var message = this.createStatusMessage(filtered_event) | |
this.changeSlackStatus(message, filtered_event) | |
},this) | |
}, | |
getCurrentEvents: function() { | |
var start = new Date() | |
var end = new Date(start.getTime() + 60 * 1000) | |
var calendar = CalendarApp.getCalendarById(this.email) | |
return calendar.getEvents(start, end) | |
}, | |
createStatusMessage: function(event) { | |
if (!event || this.isPrivateEvent(event) || this.isAlldayEvent(event) ) { | |
return this.default_message | |
} | |
var schedule = this.getEventSchedule(event) | |
var message = event.getTitle() + '' | |
if (event.getLocation() !== '') { | |
message += ' @ ' + event.getLocation() | |
} | |
return message + '【' + schedule['start'] + ' ~ ' + schedule['end'] + '】' | |
}, | |
isPrivateEvent: function(event) { | |
return event.getVisibility() !== CalendarApp.Visibility.DEFAULT | |
}, | |
isAlldayEvent:function(event) { | |
return event.isAllDayEvent() | |
}, | |
getEventSchedule: function(event) { | |
return { | |
start: Utilities.formatDate(event.getStartTime(), 'Asia/Tokyo', 'HH:mm'), | |
end: Utilities.formatDate(event.getEndTime(), 'Asia/Tokyo', 'HH:mm'), | |
} | |
}, | |
changeSlackStatus: function(message, event) { | |
if (!event || this.isPrivateEvent(event) || this.isAlldayEvent(event) ) { | |
var emoji = this.default_emoji | |
message = this.default_message | |
} else { | |
if(message.slice(message,3) == "[休]"){ | |
var emoji = ':yasumi:' | |
message = message.substr(3) | |
}else{ | |
var emoji = ':date:' | |
} | |
} | |
var profile = { | |
'status_text': message, | |
'status_emoji': emoji, | |
} | |
UrlFetchApp.fetch("https://slack.com/api/users.profile.set?token=" + this.token + "&profile=" + encodeURIComponent(JSON.stringify(profile))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment