-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbirthday_bot.py
More file actions
46 lines (37 loc) · 1.4 KB
/
birthday_bot.py
File metadata and controls
46 lines (37 loc) · 1.4 KB
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
from slack_sdk.web import WebClient
from get_calendar_events import GetCalendarEvents
import os
import sys
import re
if __name__ == '__main__':
"""Create message and post to slack channel with given channel id"""
try:
# set channel id
channel_id = sys.argv[1]
except:
print('Please provide channel_id as cmd line argumente: \n $ python app_calendar.py <channel_id>')
sys.exit(2)
# get email addresses of those who have birthday today
C = GetCalendarEvents()
emails = C.get_birthdays()
# exit if no one has birthday
if (len(emails) == 0):
exit()
# we need to pass the 'Bot User OAuth Token'
slack_token = os.environ['SLACK_BOT_TOKEN']
# creating an instance of the Webclient class
client = WebClient(token=slack_token)
# post birthday greeting(s) on Slack
for email in emails:
try:
# get slack user name
user_name = client.users_lookupByEmail(email=email)['user']['name']
except:
print('User name not found')
continue
# construct and post message
message = "Happy birthday, <@" + user_name + ">! :balloon: \n"
response = client.chat_postMessage(channel=channel_id,
username="scanbot",
icon_emoji=":robot_face:",
text=message)