-
Notifications
You must be signed in to change notification settings - Fork 0
/
birthday.py
82 lines (69 loc) · 1.89 KB
/
birthday.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
import os
import urllib, json
import datetime
import config
# Date Format
dateFormat = "%b %d"
# Date formatting
def suffix(d):
return 'th' if 11<=d<=13 else {1:'st',2:'nd',3:'rd'}.get(d%10, 'th')
def custom_strftime(format, t):
return t.strftime(format).replace('{S}', str(t.day) + suffix(t.day))
# Retrieve data
json_url = urllib.urlopen(config.EMPLOYEES_URL)
employees = json.loads(json_url.read())
# Filtering function to find today's Birthdays
today = datetime.date.today()
def isBirthdayToday(employee):
if (employee and 'birthday' in employee and employee['birthday'] != '' and datetime.datetime.strptime(employee['birthday'], dateFormat).strftime(dateFormat) == today.strftime(dateFormat)):
return True
else:
return False
birthdayEmployees = filter(isBirthdayToday, employees)
birthdayEmployees.sort(key=lambda x: x['first_name'])
if (len(birthdayEmployees) == 0):
exit()
# Slack payload
data = {
"blocks": [
{
"type": "header",
"text": {
"type": "plain_text",
"text": "Today`s Birthdays :tada:\n"
}
},
{
"type": "header",
"text": {
"type": "plain_text",
"text": custom_strftime("{S} %B", today)
}
}
]
}
divider = {
"type": "divider"
}
for employee in birthdayEmployees:
employeeData = {
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*" + employee["display_name"] + "*\n<@" + employee['slack_id'] + ">\n_Team_: " + employee['department'] + "\n_Position_: " + employee['title']
},
"accessory": {
"type": "image",
"image_url": "" + employee["image_192"],
"alt_text": "" + employee["display_name"]
}
}
data["blocks"].append(divider)
data["blocks"].append(employeeData)
data["blocks"].append(divider)
# Slack POST
command = "curl --silent -X POST -H 'Content-type: application/json'"
command += " --data '" + json.dumps(data) + "'"
command += " " + config.SLACK_WEBHOOK_URL
command += " > /dev/null"
os.system(command)