-
Notifications
You must be signed in to change notification settings - Fork 1
/
duty_bot.py
195 lines (154 loc) · 5.88 KB
/
duty_bot.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# Duty Bot
# Imports
import slack
from ical import iCal
from datetime import date
import traceback
import requests
from config import SECRETS
from config import TESTING_MODE
from config import ROOMPACT
# Global Variables
duty_channel = "#duty"
trade_channel = "#duty-trade-tracker"
log_channel = "#bot-log"
if TESTING_MODE: duty_channel = trade_channel = log_channel
# Secrets
ical_url = SECRETS.get("ical_url") # Duty Schedule from DUTY 1920
rlc_ical_url = SECRETS.get("rlc_ical_url") # RLC Duty Schedule from URL DUTY 1920
oauth_token = SECRETS.get("oauth_token") # Duty Bot OAuth Token for slack
# Slack Client
slack_client = slack.WebClient(token=oauth_token)
# Post Slack Message (attachment)
# Sending it as an attachment with the color #1f4387,
# posts the message with the mesa blue line next to it
def post_attachment_slack_message(channel: str, pretext: str, message: str, color="1f4387"):
slack_client.chat_postMessage(
channel = channel,
attachments = [
{
"color": color,
"pretext": pretext,
"text": message
}
]
)
# Post Slack Message
# Post a normal slack message to the specified channel
def post_slack_message(channel: str, text: str):
slack_client.chat_postMessage(
channel = channel,
text = text
)
# Post Exception Message
# Given an exception, it will post the exceptions information to Slack
def post_exception():
post_slack_message(log_channel, "*Duty Bot has crashed!* :rotating_light: <!channel>")
post_attachment_slack_message(
log_channel,
"The following error was detected:",
traceback.format_exc(),
"ff0000"
)
# Date to String
# Given a str in ical date format (YYYYMMDD),
# Return it in a readable format (Month DD, YYYY)
months = { '01':'January', '02':'February', '03':'March',
'04': 'April', '05':'May', '06':'June',
'07':'July', '08':'August', '09':'September',
'10':'October', '11':'November', '12':'December' }
def date_to_string(date: str) -> str:
year = date[0:4]
month = date[4:6]
day = date[6:]
return f"{months[month]} {day}, {year}"
# Get Today
# Return today's date as a string `YYYY-MM-DD`
def get_today() -> str:
today = date.today()
today_str = f"{today.year}-{today.month:02d}-{today.day:02d}"
return today_str
# Fetch the duty members from roompact
# Sends a request to the download schedule url for a specific day.
# Takes the emails of the people assigned to that day and fetches their user id from Slack
# Returns a list of user ids
def fetch_duty_members_from_roompact():
# Roompact Config values
cookie = ROOMPACT.get("cookie")
region_id = ROOMPACT.get("region_id")
timezone = ROOMPACT.get("timezone")
# Get today's date
today_str = get_today()
# Construct the request to Roompact
# Should return a CSV of "First name", "Last name", "Email", etc.
url = f"https://roompact.com/schedule/download?start={today_str}&end={today_str}®ion_id={region_id}&tz={timezone}"
r = requests.get(url, cookies=cookie)
if(r.status_code == 200):
decoded_content = r.content.decode("utf-8")
# If the token is expired, the response will be a login page
if decoded_content.startswith("<!DOCTYPE"):
raise ValueError("Expired token")
# Convert the decoded content to rows
rows = decoded_content.split('\n')[:-1]
# Pull out the header row
header = rows[0].split(",")
rows = rows[1:]
# Find the index of the email column
email_index = header.index("Email")
# Construct a list slack ids for the duty members for that day
duty_members = []
for row in rows:
row_values = row.split(",")
email = row_values[email_index]
try:
# Take the email of the member and find the related slack user id
response = slack_client.users_lookupByEmail(email=email)
duty_members.insert(0, f"<@{response.data['user']['id']}>")
except:
# Print first and last name if the user can't be found
name = row_values[0] + " " + row_values[1]
name = name.replace('"', '')
duty_members.insert(0, name)
return duty_members
else:
raise Exception(f"Roompact request failed. Status Code: {r.status_code}. Error Message {r.text}")
# Post Daily RLC Schedule
def post_daily_rlc_schedule():
# Load RLC calendar
cal = iCal(rlc_ical_url)
# Get today's date
today_str = get_today().replace('-', '')
# Find rlcs on duty for today
rlcs = cal.get_event_summaries(today_str)
# Post rlcs on slack
if len(rlcs) > 0:
formatted_message = "*" + "* | *".join(rlcs) + "*"
post_attachment_slack_message(duty_channel, None, formatted_message, "87693b")
# Post Daily Duty Schedule
def post_daily_duty_schedule():
# Find duty team for today
duty_members = fetch_duty_members_from_roompact()
# Post duty team on slack
for index, member in enumerate(duty_members):
duty_number = f"D{index}" if index != 0 else "DC"
formatted_message = f"*{duty_number}: {member}*"
post_attachment_slack_message(duty_channel, None, formatted_message)
# Post RLCs on duty
# post_daily_rlc_schedule()
# AWS Handler
# This is the entry point for the AWS lambda function
def aws_handler(event, lambda_context):
try:
post_daily_duty_schedule()
except:
# Notify slack that an exception occured
if not TESTING_MODE: post_exception()
raise
# Main
if __name__ == "__main__":
try:
post_daily_duty_schedule()
except:
# Notify slack that an exception occured
if not TESTING_MODE: post_exception()
raise