-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcombinedNotify.py
90 lines (86 loc) · 2.99 KB
/
combinedNotify.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
import os
from slack_sdk import WebClient
import sqlite3 as sql
import json
import os
from dotenv import load_dotenv
import time
load_dotenv()
db_global = "global.db"
test_users = ['Keith White', 'Ayush Kulkarni', 'Manthan Acharya', 'Areen Panda']
slack_token = os.getenv('SLACK_TOKEN')
client = WebClient(token=slack_token)
conn_global = sql.connect(db_global)
cur_global = conn_global.cursor()
sendBreakSchedule = True
def Break_Notify():
time.sleep(10)
cur_global.execute("SELECT value FROM config WHERE key = 'schedule_match'")
nextMatch = str(cur_global.fetchall()[0][0])
#nextMatch = 8
print("Next match is ", nextMatch)
offBreakScouts = []
try:
results = cur_global.execute(
"select Scout,BreakEnd from break_schedule where BreakEnd - ? = 2 AND Notified=0", (nextMatch,)).fetchall()[0]
print(results)
offBreakScouts = json.loads(results[0])
breakEnd = results[1]
except:
offBreakScouts = []
breakStart = 0
for scoutOnBreak in offBreakScouts:
slackUid = ""
try:
slackUid = cur_global.execute(
"select userid from Slack_UserIDs where name = ? ", (scoutOnBreak,)).fetchall()[0][0]
except:
slackUid = "none"
print("Sending end of break message to ", scoutOnBreak, slackUid)
msgText = "Match " + \
str(nextMatch) + " is next. You have a break is ending in 2 matches. Please return to the stands "
response = client.chat_postMessage(
channel=slackUid,
text=msgText
)
if len(offBreakScouts) > 0:
result = cur_global.execute(
"update break_schedule set Notified=1 where BreakEnd = ? AND Notified=0", (breakEnd,))
conn_global.commit()
def get_scouts():
cur_global.execute("SELECT * FROM scouts ORDER BY name")
results = cur_global.fetchall()
scouts = {}
for i in range(len(results)):
scouts[results[i][0]] = results[i][1]
# print(len(results))
# print(scouts)
return (scouts)
def slack_notify(name, message):
try:
slackUid = cur_global.execute(
"select userid from Slack_UserIDs where name = ? ", (name,)).fetchall()[0][0]
except:
slackUid = "none"
print("Sending sign in/out message to ",name, slackUid)
response = client.chat_postMessage(
channel=slackUid,
text=message
)
def Login_Notify():
time.sleep(10)
print("sleep over")
currentScoutList = get_scouts()
for index in currentScoutList:
if currentScoutList[index] != initialScoutList[index]:
if currentScoutList[index] == 1:
slack_notify(index, "You are Signed In.")
print("Sign in")
else:
slack_notify(index, "You are Signed Out")
print("Sign out")
initialScoutList[index] = currentScoutList[index]
initialScoutList = get_scouts()
while (True):
Login_Notify()
Break_Notify()