Last active
June 19, 2020 10:52
-
-
Save muyajil/171f2f2bd2704aecb5e90a99bc315c29 to your computer and use it in GitHub Desktop.
Set the slack status from your calendar
This file contains hidden or 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 setSlackStatusFromCalendar() { | |
| var calendarId = PropertiesService.getScriptProperties().getProperty('CALENDAR_ID'); | |
| var calendar = CalendarApp.getCalendarById(calendarId); | |
| var now = new Date(); | |
| var twoHoursFromNow = new Date(now.getTime() + (2 * 60 * 60 * 1000)); | |
| var events = calendar.getEvents(now, twoHoursFromNow); | |
| if (events.length === 0){ | |
| setSlackStatus('free'); | |
| } else { | |
| var away = false; | |
| for(var i=0; i < events.length; i++){ | |
| if (events[i].getTitle().indexOf("OOO") > -1){ | |
| away = true; | |
| } | |
| } | |
| if (away){ | |
| setSlackStatus('away'); | |
| } else { | |
| setSlackStatus('busy'); | |
| } | |
| } | |
| } | |
| function setSlackStatus(status){ | |
| var slackAccessToken = PropertiesService.getScriptProperties().getProperty('SLACK_ACCESS_TOKEN'); | |
| let slackStatus; | |
| if (status === 'free'){ | |
| slackStatus = { | |
| "profile": { | |
| "status_text": "Free", | |
| "status_emoji": ":rainbow:", | |
| "status_expiration": 0 | |
| } | |
| } | |
| } else if (status === 'busy'){ | |
| slackStatus = { | |
| "profile": { | |
| "status_text": "Busy", | |
| "status_emoji": ":octagonal_sign:", | |
| "status_expiration": 0 | |
| } | |
| } | |
| } else if (status === 'away'){ | |
| slackStatus = { | |
| "profile": { | |
| "status_text": "Away", | |
| "status_emoji": ":palm_tree:", | |
| "status_expiration": 0 | |
| } | |
| } | |
| } | |
| options = { | |
| 'method': 'post', | |
| 'contentType': 'application/json;charset=utf-8', | |
| 'headers': { | |
| 'Authorization': 'Bearer ' + slackAccessToken | |
| }, | |
| 'payload': JSON.stringify(slackStatus) | |
| } | |
| var response = UrlFetchApp.fetch("https://www.slack.com/api/users.profile.set", options); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment