/
Slack_Bot.py
389 lines (374 loc) · 20.2 KB
/
Slack_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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
from __future__ import print_function
from apiclient import discovery
from httplib2 import Http
from oauth2client import file, client, tools
from apiclient.http import MediaIoBaseDownload
from googleapiclient.discovery import build
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
import random
import os
import io
import pyautogui
import subprocess
import webbrowser
import sys
import pyautogui
import time
import smtplib
import openpyxl
from openpyxl import load_workbook
import re
from slackclient import SlackClient
#Downlaods training sign up
SCOPES = 'https://www.googleapis.com/auth/drive.readonly'
def main():
store = file.Storage('token.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
creds = tools.run_flow(flow, store)
service = build('drive', 'v3', http=creds.authorize(Http()))
file_id = '***'
request = service.files().export_media(fileId=file_id,
mimeType='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
fh = io.FileIO('FIXsignups.xlsx', 'wb')
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
status, done = downloader.next_chunk()
print("FIXsignups.xlsx updated.")
if __name__ == '__main__':
main()
#Downloads Personnel database
def main():
store = file.Storage('token.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
creds = tools.run_flow(flow, store)
service = build('drive', 'v3', http=creds.authorize(Http()))
file_id = '***'
request = service.files().export_media(fileId=file_id,
mimeType='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
fh = io.FileIO('Database.xlsx', 'wb')
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
status, done = downloader.next_chunk()
print('Database.xlsx updated.')
if __name__ == '__main__':
main()
#Downloads Feedback Form
def main():
store = file.Storage('token.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
creds = tools.run_flow(flow, store)
service = build('drive', 'v3', http=creds.authorize(Http()))
file_id = '***'
request = service.files().export_media(fileId=file_id,
mimeType='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
fh = io.FileIO('Feedback_Form.xlsx', 'wb')
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
status, done = downloader.next_chunk()
print('Feedback_Form.xlsx updated.')
if __name__ == '__main__':
main()
# instantiate Slack client
slack_client = SlackClient(os.environ.get('SLACK_BOT_TOKEN'))
# starterbot's user ID in Slack: value is assigned after the bot starts up
starterbot_id = None
# constants
RTM_READ_DELAY = 1 # 1 second delay between reading from RTM
MENTION_REGEX = "^<@(|[WU].+?)>(.*)"
#Notifies JUSTIN iff FIX trainees haven't been contacted
wb = openpyxl.load_workbook('FIXsignups.xlsx')
ws = wb.active
wb.get_sheet_names()
sheet = wb.get_sheet_by_name('Form Responses 1')
print("scanning for needy FIX trainees...")
for row in range(2, sheet.max_row +1):
if sheet['I' + str(row)].value is None:
slack_client.api_call(
"chat.postMessage",
channel="***",
text= ['This trainee needs to be contacted!', sheet['C' + str(row)].value, sheet['B' + str(row)].value]
)
#Sends DM to JUSTIN iff volunteer available
wb = openpyxl.load_workbook('FIXsignups.xlsx')
ws = wb.active
wb.get_sheet_names()
sheet = wb.get_sheet_by_name('Form Responses 1')
print("checking for new FIX applicants...")
for row in range(2, sheet.max_row +1):
if sheet['D' + str(row)].value is not None and "Justin" in sheet['D' + str(row)].value and sheet['B' + str(row)].value not in open("PriorTrainJUSM.txt").read():
slack_client.api_call(
"chat.postMessage",
channel="***",
text= ['A new volunteer wants to train with you!', sheet['C' + str(row)].value, sheet['B' + str(row)].value, "MONDAYS", sheet['D' + str(row)].value]
)
open("PriorTrainJUSM.txt", "a").write(sheet['B' + str(row)].value)
open("PriorTrainJUSM.txt", "a").close()
print(sheet['B' + str(row)].value, 'sent to Justin.')
if sheet['E' + str(row)].value is not None and "Justin" in sheet['E' + str(row)].value and sheet['B' + str(row)].value not in open("PriorTrainJUST.txt").read():
slack_client.api_call(
"chat.postMessage",
channel="***",
text= ['A new volunteer wants to train with you!', sheet['C' + str(row)].value, sheet['B' + str(row)].value, "TUESDAYS", sheet['E' + str(row)].value]
)
open("PriorTrainJUST.txt", "a").write(sheet['B' + str(row)].value)
open("PriorTrainJUST.txt", "a").close()
print(sheet['B' + str(row)].value, 'sent to Justin')
if sheet['F' + str(row)].value is not None and "Justin" in sheet['F' + str(row)].value and sheet['B' + str(row)].value not in open("PriorTrainJUSW.txt").read():
slack_client.api_call(
"chat.postMessage",
channel="***",
text= ['A new volunteer wants to train with you!', sheet['C' + str(row)].value, sheet['B' + str(row)].value, "WEDNESDAYS", sheet['F' + str(row)].value]
)
open("PriorTrainJUSW.txt", "a").write(sheet['B' + str(row)].value)
open("PriorTrainJUSW.txt", "a").close()
print(sheet['B' + str(row)].value, 'sent to Justin.')
if sheet['G' + str(row)].value is not None and "Justin" in sheet['G' + str(row)].value and sheet['B' + str(row)].value not in open("PriorTrainJUSTH.txt").read():
slack_client.api_call(
"chat.postMessage",
channel="***",
text= ['A new volunteer wants to train with you!', sheet['C' + str(row)].value, sheet['B' + str(row)].value, "THURSDAYS", sheet['G' + str(row)].value]
)
open("PriorTrainJUSTH.txt", "a").write(sheet['B' + str(row)].value)
open("PriorTrainJUSTH.txt", "a").close()
print(sheet['B' + str(row)].value, 'sent to Justin.')
if sheet['H' + str(row)].value is not None and "Justin" in sheet['H' + str(row)].value and sheet['B' + str(row)].value not in open("PriorTrainJUSF.txt").read():
slack_client.api_call(
"chat.postMessage",
channel="***",
text= ['A new volunteer wants to train with you!', sheet['C' + str(row)].value, sheet['B' + str(row)].value, "FRIDAYS", sheet['G' + str(row)].value]
)
open("PriorTrainJUSF.txt", "a").write(sheet['B' + str(row)].value)
open("PriorTrainJUSF.txt", "a").close()
print(sheet['B' + str(row)].value, 'sent to Justin.')
#Sends DM to ZOE iff volunteer available
for row in range(2, sheet.max_row +1):
if sheet['D' + str(row)].value is not None and "Zoe" in sheet['D' + str(row)].value and sheet['B' + str(row)].value not in open("PriorTrainZOEM.txt").read():
slack_client.api_call(
"chat.postMessage",
channel="***",
text= ['A new volunteer wants to train with you!', sheet['C' + str(row)].value, sheet['B' + str(row)].value, "MONDAYS", sheet['D' + str(row)].value]
)
open("PriorTrainZOEM.txt", "a").write(sheet['B' + str(row)].value)
open("PriorTrainZOEM.txt", "a").close()
print(sheet['B' + str(row)].value, 'sent to Zoe.')
if sheet['E' + str(row)].value is not None and "Zoe" in sheet['E' + str(row)].value and sheet['B' + str(row)].value not in open("PriorTrainZOET.txt").read():
slack_client.api_call(
"chat.postMessage",
channel="***",
text= ['A new volunteer wants to train with you!', sheet['C' + str(row)].value, sheet['B' + str(row)].value, "TUESDAYS", sheet['E' + str(row)].value]
)
open("PriorTrainZOET.txt", "a").write(sheet['B' + str(row)].value)
open("PriorTrainZOET.txt", "a").close()
print(sheet['B' + str(row)].value, 'sent to Zoe.')
if sheet['F' + str(row)].value is not None and "Zoe" in sheet['F' + str(row)].value and sheet['B' + str(row)].value not in open("PriorTrainZOEW.txt").read():
slack_client.api_call(
"chat.postMessage",
channel="***",
text= ['A new volunteer wants to train with you!', sheet['C' + str(row)].value, sheet['B' + str(row)].value, "WEDNESDAYS", sheet['F' + str(row)].value]
)
open("PriorTrainZOEW.txt", "a").write(sheet['B' + str(row)].value)
open("PriorTrainZOEW.txt", "a").close()
print(sheet['B' + str(row)].value, 'sent to Zoe.')
if sheet['G' + str(row)].value is not None and "Zoe" in sheet['G' + str(row)].value and sheet['B' + str(row)].value not in open("PriorTrainZOETH.txt").read():
slack_client.api_call(
"chat.postMessage",
channel="***",
text= ['A new volunteer wants to train with you!', sheet['C' + str(row)].value, sheet['B' + str(row)].value, "THURSDAYS", sheet['G' + str(row)].value]
)
open("PriorTrainZOETH.txt", "a").write(sheet['B' + str(row)].value)
open("PriorTrainZOETH.txt", "a").close()
print(sheet['B' + str(row)].value, 'sent to Zoe.')
if sheet['H' + str(row)].value is not None and "Zoe" in sheet['H' + str(row)].value and sheet['B' + str(row)].value not in open("PriorTrainZOEF.txt").read():
slack_client.api_call(
"chat.postMessage",
channel="***",
text= ['A new volunteer wants to train with you!', sheet['C' + str(row)].value, sheet['B' + str(row)].value, "FRIDAYS", sheet['G' + str(row)].value]
)
open("PriorTrainZOEF.txt", "a").write(sheet['B' + str(row)].value)
open("PriorTrainZOEF.txt", "a").close()
print(sheet['B' + str(row)].value, 'sent to Zoe.')
#Sends DM to TYLER iff volunteer available
for row in range(2, sheet.max_row +1):
if sheet['D' + str(row)].value is not None and "Tyler" in sheet['D' + str(row)].value and sheet['B' + str(row)].value not in open("PriorTrainTYLM.txt").read():
slack_client.api_call(
"chat.postMessage",
channel="***",
text= ['A new volunteer wants to train with you!', sheet['C' + str(row)].value, sheet['B' + str(row)].value, "MONDAYS", sheet['D' + str(row)].value]
)
open("PriorTrainTYLM.txt", "a").write(sheet['B' + str(row)].value)
open("PriorTrainTYLM.txt", "a").close()
print(sheet['B' + str(row)].value, 'sent to Tyler.')
if sheet['E' + str(row)].value is not None and "Tyler" in sheet['E' + str(row)].value and sheet['B' + str(row)].value not in open("PriorTrainTYLT.txt").read():
slack_client.api_call(
"chat.postMessage",
channel="***",
text= ['A new volunteer wants to train with you!', sheet['C' + str(row)].value, sheet['B' + str(row)].value, "TUESDAYS", sheet['E' + str(row)].value]
)
open("PriorTrainTLYT.txt", "a").write(sheet['B' + str(row)].value)
open("PriorTrainTYLT.txt", "a").close()
print(sheet['B' + str(row)].value, 'sent to Tyler.')
if sheet['F' + str(row)].value is not None and "Tyler" in sheet['F' + str(row)].value and sheet['B' + str(row)].value not in open("PriorTrainTYLW.txt").read():
slack_client.api_call(
"chat.postMessage",
channel="***",
text= ['A new volunteer wants to train with you!', sheet['C' + str(row)].value, sheet['B' + str(row)].value, "WEDNESDAYS", sheet['F' + str(row)].value]
)
open("PriorTrainTYLW.txt", "a").write(sheet['B' + str(row)].value)
open("PriorTrainTYLW.txt", "a").close()
print(sheet['B' + str(row)].value, 'sent to Tyler.')
if sheet['G' + str(row)].value is not None and "Tyler" in sheet['G' + str(row)].value and sheet['B' + str(row)].value not in open("PriorTrainTYLTH.txt").read():
slack_client.api_call(
"chat.postMessage",
channel="***",
text= ['A new volunteer wants to train with you!', sheet['C' + str(row)].value, sheet['B' + str(row)].value, "THURSDAYS", sheet['G' + str(row)].value]
)
open("PriorTrainTYLTH.txt", "a").write(sheet['B' + str(row)].value)
open("PriorTrainTYLTH.txt", "a").close()
print(sheet['B' + str(row)].value, 'sent to Tyler.')
if sheet['H' + str(row)].value is not None and "Tyler" in sheet['H' + str(row)].value and sheet['B' + str(row)].value not in open("PriorTrainTYLF.txt").read():
slack_client.api_call(
"chat.postMessage",
channel="***",
text= ['A new volunteer wants to train with you!', sheet['C' + str(row)].value, sheet['B' + str(row)].value, "FRIDAYS", sheet['H' + str(row)].value]
)
open("PriorTrainTYLF.txt", "a").write(sheet['B' + str(row)].value)
open("PriorTrainTYLF.txt", "a").close()
print(sheet['B' + str(row)].value, 'sent to Tyler.')
#Sends DM to AMANDA iff volunteer available
for row in range(2, sheet.max_row +1):
if sheet['D' + str(row)].value is not None and "Amanda" in sheet['D' + str(row)].value and sheet['B' + str(row)].value not in open("PriorTrainAMAM.txt").read():
slack_client.api_call(
"chat.postMessage",
channel="***",
text= ['A new volunteer wants to train with you!', sheet['C' + str(row)].value, sheet['B' + str(row)].value, "MONDAYS", sheet['D' + str(row)].value]
)
open("PriorTrainAMAM.txt", "a").write(sheet['B' + str(row)].value)
open("PriorTrainAMAM.txt", "a").close()
print(sheet['B' + str(row)].value, 'sent to Amanda.')
if sheet['E' + str(row)].value is not None and "Amanda" in sheet['E' + str(row)].value and sheet['B' + str(row)].value not in open("PriorTrainAMAT.txt").read():
slack_client.api_call(
"chat.postMessage",
channel="***",
text= ['A new volunteer wants to train with you!', sheet['C' + str(row)].value, sheet['B' + str(row)].value, "TUESDAYS", sheet['E' + str(row)].value]
)
open("PriorTrainAMAT.txt", "a").write(sheet['B' + str(row)].value)
open("PriorTrainAMAT.txt", "a").close()
print(sheet['B' + str(row)].value, 'sent to Amanda.')
if sheet['F' + str(row)].value is not None and "Amanda" in sheet['F' + str(row)].value and sheet['B' + str(row)].value not in open("PriorTrainAMAW.txt").read():
slack_client.api_call(
"chat.postMessage",
channel="***",
text= ['A new volunteer wants to train with you!', sheet['C' + str(row)].value, sheet['B' + str(row)].value, "WEDNESDAYS", sheet['F' + str(row)].value]
)
open("PriorTrainAMAW.txt", "a").write(sheet['B' + str(row)].value)
open("PriorTrainAMAW.txt", "a").close()
print(sheet['B' + str(row)].value, 'sent to Amanda.')
if sheet['G' + str(row)].value is not None and "Amanda" in sheet['G' + str(row)].value and sheet['B' + str(row)].value not in open("PriorTrainAMATH.txt").read():
slack_client.api_call(
"chat.postMessage",
channel="***",
text= ['A new volunteer wants to train with you!', sheet['C' + str(row)].value, sheet['B' + str(row)].value, "THURSDAYS", sheet['G' + str(row)].value]
)
open("PriorTrainAMATH.txt", "a").write(sheet['B' + str(row)].value)
open("PriorTrainAMATH.txt", "a").close()
print(sheet['B' + str(row)].value, 'sent to Amanda.')
if sheet['H' + str(row)].value is not None and "Amanda" in sheet['H' + str(row)].value and sheet['B' + str(row)].value not in open("PriorTrainAMAF.txt").read():
slack_client.api_call(
"chat.postMessage",
channel="***",
text= ['A new volunteer wants to train with you!', sheet['C' + str(row)].value, sheet['B' + str(row)].value, "FRIDAYS", sheet['G' + str(row)].value]
)
open("PriorTrainAMAF.txt", "a").write(sheet['B' + str(row)].value)
open("PriorTrainAMAF.txt", "a").close()
print(sheet['B' + str(row)].value, 'sent to Amanda.')
def parse_bot_commands(slack_events):
"""
Parses a list of events coming from the Slack RTM API to find bot commands.
If a bot command is found, this function returns a tuple of command and channel.
If its not found, then this function returns None, None.
"""
for event in slack_events:
if event["type"] == "message" and not "subtype" in event:
user_id, message = parse_direct_mention(event["text"])
if user_id == starterbot_id:
return message, event["channel"]
return None, None
def parse_direct_mention(message_text):
"""
Finds a direct mention (a mention that is at the beginning) in message text
and returns the user ID which was mentioned. If there is no direct mention, returns None
"""
matches = re.search(MENTION_REGEX, message_text)
# the first group contains the username, the second group contains the remaining message
return (matches.group(1), matches.group(2).strip()) if matches else (None, None)
def handle_command(command, channel):
"""
Executes bot command if the command is known
"""
# Default response is help text for the user
default_response = "Person not found. Last name may need to be capitalized. Try *{}*.".format("@Rog Smog find [Last name]")
# Finds and executes the given command, filling in response
response = None
#Score lookup
if command.startswith("score"):
print("Score request recieved. Processing...")
wb = openpyxl.load_workbook('Feedback_Form.xlsx')
sheet = wb.active
for row in range(2, sheet.max_row +1):
if sheet['B' + str(row)].value is not None:
open("FeedbackEmails.txt", "a").write(sheet['B' + str(row)].value)
open("PriorTrainJUST.txt", "a").close()
if sheet['B' + str(row)].value is not None and eval('command').replace('score ','') in sheet['B' + str(row)].value:
slack_client.api_call(
"chat.postMessage",
channel=channel,
text= ["FIX", sheet['BX' + str(row)].value, "VT", sheet['AZ' + str(row)].value, "Live", sheet['AB' + str(row)].value]
)
print("Score request processed.")
if eval('command').replace('score ','') not in open("FeedbackEmails.txt").read():
slack_client.api_call(
"chat.postMessage",
channel=channel,
text= "Email not found."
)
open('FeedbackEmails.txt', 'w').close()
#Person Lookup
if command.startswith("find"):
print("Info request recieved. Processing...")
wb = openpyxl.load_workbook('Database.xlsx')
sheet = wb.active
for row in range(2, sheet.max_row +1):
if sheet['D' + str(row)].value is not None and eval('command').replace('find ','') in sheet['D' + str(row)].value:
response = [sheet['B' + str(row)].value, sheet['D' + str(row)].value, sheet['G' + str(row)].value, sheet['L' + str(row)].value, sheet['H' + str(row)].value, sheet['R' + str(row)].value]
# Sends the response back to the channel
slack_client.api_call(
"chat.postMessage",
channel=channel,
text=response or default_response
)
print("request processed.")
print(eval('command').replace('find ',''))
if __name__ == "__main__":
if slack_client.rtm_connect(with_team_state=False):
print("Slack Bot is online.")
# Read bot's user ID by calling Web API method `auth.test`
starterbot_id = slack_client.api_call("auth.test")["user_id"]
while True:
command, channel = parse_bot_commands(slack_client.rtm_read())
if command:
handle_command(command, channel)
time.sleep(RTM_READ_DELAY)
else:
print("Connection failed. Exception traceback printed above.")