Created
April 27, 2017 00:57
-
-
Save tmd45/b89faca607de9e023d0dfcfd1039f84b 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 = 'xxxxx' | |
var token = 'xxxxx' | |
var inEventEmoji = ':date:' | |
var noEventEmoji = ':globe_with_meridians:' | |
var notWorking = ':house:' | |
var ima = new Imananishiton(email, token, inEventEmoji, noEventEmoji, notWorking) | |
ima.nanishiton() | |
} | |
var Imananishiton = function(email, token, inEventEmoji, noEventEmoji, notWorking) { | |
this.email = email | |
this.token = token | |
this.inEventEmoji = inEventEmoji | |
this.noEventEmoji = noEventEmoji | |
this.notWorking = notWorking | |
} | |
Imananishiton.prototype = { | |
nanishiton: function() { | |
var events = this.getCurrentEvents() | |
var notWorks = this.getCurrentWorks() | |
var message = this.createStatusMessage(events[0], notWorks) | |
var emoji = this.createStatusEmoji(events[0], notWorks) | |
this.changeSlackStatus(message, emoji) | |
}, | |
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) | |
}, | |
getCurrentWorks: function() { | |
var now = new Date(); | |
var hour = now.getHours(); | |
var day = now.getDay(); | |
return (day < 1 || day > 5 || hour < 10 || hour > 19) | |
}, | |
createStatusMessage: function(event, notWorks) { | |
if (notWorks) { | |
return 'Out of works' | |
} | |
if (!event || this.isPrivateEvent(event)) { | |
return 'It works' | |
} | |
var schedule = this.getEventSchedule(event) | |
var message = 'Schedule: ' + event.getTitle() | |
if (event.getLocation() !== '') { | |
message += ' @ ' + event.getLocation() | |
} | |
return message + '【' + schedule['start'] + ' ~ ' + schedule['end'] + '】' | |
}, | |
isPrivateEvent: function(event) { | |
return event.getVisibility() !== CalendarApp.Visibility.DEFAULT | |
}, | |
getEventSchedule: function(event) { | |
return { | |
start: Utilities.formatDate(event.getStartTime(), 'Asia/Tokyo', 'HH:mm'), | |
end: Utilities.formatDate(event.getEndTime(), 'Asia/Tokyo', 'HH:mm'), | |
} | |
}, | |
createStatusEmoji: function(event, notWorks) { | |
if (notWorks) { | |
return this.notWorking | |
} else if (!event || this.isPrivateEvent(event)) { | |
return this.noEventEmoji | |
} else { | |
return this.inEventEmoji | |
} | |
}, | |
changeSlackStatus: function(message, emoji) { | |
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