-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbamboo.py
102 lines (82 loc) · 2.78 KB
/
bamboo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import json
import logging
import base64
import datetime
from urllib2 import Request, urlopen, URLError, HTTPError
from datetime import datetime
from datetime import timedelta
logger = logging.getLogger()
logger.setLevel(logging.INFO)
#
# Configure these variables for your environment
#
# The domain of your BambooHR account (uk or us)
BAMBOO_DOMAIN = "uk"
# Your BambooHR account name (the first part of your BambooHR url)
BAMBOO_ACCOUNT = "REDACTED"
# Your BambooHR API Key - note that if you use a personal API key generated from the
# BambooHR API and then run this code on AWS you are exposing all of the BambooHR data
# you can access to anybody who can view your Lambda code! Contact BambooHR support
# to get a specific API key for this purpose.
BAMBOO_API_KEY = "REDACTED"
# The incoming slack webhook to send messages to
SLACK_WEB_HOOK = "REDACTED"
# The slack channel to send messages to
SLACK_CHANNEL = "general"
# Do not modify below here
base64string = base64.encodestring('%s:%s' % (BAMBOO_API_KEY, "blah")).replace('\n', '')
bamboodomains = {
"uk" : "api.bamboohr.co.uk",
"us" : "api.bamboohr.com"
}
def whosout(today):
out = []
bamboorequest = Request("https://{0}/api/gateway.php/{1}/v1/time_off/whos_out/?filter=off&end={2}".format(bamboodomains[BAMBOO_DOMAIN], BAMBOO_ACCOUNT, today.strftime("%Y-%m-%d")))
bamboorequest.add_header("Authorization", "Basic %s" % base64string)
bamboorequest.add_header("Accept", "application/json")
try:
result = urlopen(bamboorequest)
return json.loads(result.read())
except HTTPError as e:
logger.error("Request failed: %d %s", e.code, e.reason)
except URLError as e:
logger.error("Server connection failed: %s", e.reason)
return names
def posttoslack(text):
slack_message = {
"attachments": [
{
"fallback": "Who's out?",
"text": text,
"mrkdwn_in": ["text"],
"username": "bamboo-bot",
"fields": [],
"color": "#F3F300"
}
],
"channel": SLACK_CHANNEL
}
req = Request(SLACK_WEB_HOOK, json.dumps(slack_message))
try:
response = urlopen(req)
response.read()
logger.info("Message posted to %s", slack_message['channel'])
except HTTPError as e:
logger.error("Request failed: %d %s", e.code, e.reason)
except URLError as e:
logger.error("Server connection failed: %s", e.reason)
def lambda_handler(event, context):
today = datetime.now()
out = whosout(today)
if out:
names = []
for who in out:
if who.get("type") == "holiday":
posttoslack("*Today is:* {} :confetti_ball:".format(who.get("name")))
else:
names.append(who.get("name"))
if names:
text = "*Who's out today:* \n{}".format('\n'.join(names))
posttoslack(text)
else:
posttoslack("Nobody is out today! :tada:")