-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCapacityInformerBot.py
More file actions
173 lines (143 loc) · 5.47 KB
/
CapacityInformerBot.py
File metadata and controls
173 lines (143 loc) · 5.47 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
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
from telegram.ext import Updater
from telegram.ext import CommandHandler
import json
import os
import requests
with open("bot.conf", "r") as f:
CONFS = json.loads(f.read())
updater = Updater(token=CONFS['TOKEN'])
dispatcher = updater.dispatcher
j = updater.job_queue
ENDPOINT = CONFS['ENDPOINT']
total_list = {}
if os.path.exists("./backup_list"):
with open("./backup_list", "r") as f:
total_list = json.loads(f.read())
def checker(bot, job):
global total_list, ENDPOINT
for chat_id in total_list.keys():
chat_id_key = str(chat_id)
for crn in total_list[chat_id_key]:
print("Trying now: {} => {}".format(chat_id_key, crn))
URL = f"{ENDPOINT}{crn}"
r = requests.get(URL)
result_json = r.json()
print("Result is: {}".format(result_json))
if result_json != None:
if result_json["space"] != 0:
bot.send_message(chat_id=chat_id, text=f"Current empty space in course with CRN: {crn} is => {result_json['space']}")
total_list[chat_id].remove(crn)
update_total_list()
def stop(bot, update, args):
global total_list
chat_id = update.message.chat_id
if len(args) == 1:
crn = args[0]
chat_id_key = str(chat_id)
if crn in total_list[chat_id_key]:
total_list[chat_id_key].remove(crn)
bot.send_message(chat_id=chat_id, text=f"The course with CRN: {crn} is removed from your tracked course list.")
update_total_list()
else:
bot.send_message(chat_id=chat_id, text="Give me a CRN Code")
def update_total_list():
global total_list
with open("backup_list", "w") as f:
f.write(json.dumps(total_list))
def check_crn(bot, update, args):
global total_list
chat_id = update.message.chat_id
chat_id_key = str(chat_id)
if len(args) == 1:
crn_code = args[0]
if len(crn_code) == 5 and crn_code.isdigit():
URL = f"{ENDPOINT}{crn_code}"
r = requests.get(URL)
result_json = r.json()
print("Result is: ".format(result_json))
if result_json != None:
bot.send_message(chat_id=chat_id, text=f"Current empty space in class with CRN {crn_code} is => {result_json['space']}")
if chat_id_key not in total_list.keys():
total_list[chat_id_key] = [crn_code]
update_total_list()
bot.send_message(chat_id=chat_id, text=f"The course with {crn_code} is now tracked. When there is an available seat, you will be informed by a message.")
elif crn_code not in total_list[chat_id_key]:
total_list[chat_id_key].append(crn_code)
update_total_list()
bot.send_message(chat_id=chat_id, text=f"The course with {crn_code} is now tracked. When there is an available seat, you will be informed by a message.")
else:
bot.send_message(chat_id=chat_id, text=f"The course with {crn_code} is already tracked by you. If you would like to stop tracking use /stop command.")
else:
bot.send_message(chat_id=chat_id, text="Incorrect CRN Code")
else:
bot.send_message(chat_id=chat_id, text="Give me a CRN Code")
def list_crn(bot, update):
global total_list
chat_id = update.message.chat_id
print(chat_id)
print(total_list)
chat_id_key = str(chat_id)
if chat_id_key in total_list.keys():
print("THERE")
list_message = "The courses is actively tracked by you are: " + str(total_list[chat_id_key])
print(list_message)
bot.send_message(chat_id=chat_id, text=list_message)
else:
print("HERE")
error_message = "You haven't traced any course yet."
bot.send_message(chat_id=chat_id, text=error_message)
def help(bot, update):
help_message = """ Commands:
/check <CRNCODE> (to start tracking a course)
/list (to list the courses you are tracking)
/stop <CRNCODE> (to stop tracking a course)
Example:
/check 15132
/list
/stop 15132
"""
bot.send_message(chat_id=update.message.chat_id, text=help_message)
def start(bot, update):
welcoming_message = """
_________________________________________________
......., .
' , ``
' , . `
' ` `
'. , ` ` ` .
' , ` ` .
` ` `
` ` : :
` ` :
` ` '
` ` '
, ` '
, ` ' `
, , `. ` ' , ` `
, , ` , ` `
, , ` ' ` `
( , ` )
~~ ~~
_________________________________________________
Wellcome to the Course Capacity Informer Bot.
Usage:
/check <CRNCODE>
Example:
/check 15132
For more information use /help
"""
bot.send_message(chat_id=update.message.chat_id, text=welcoming_message)
job1 = j.run_repeating(checker, interval=60, first=0)
job2 = j.run_repeating(checker, interval=60, first=30)
help_handler = CommandHandler('help', help)
start_handler = CommandHandler('start', start)
checkcrn_handler = CommandHandler('check', check_crn, pass_args=True)
listcrn_handler = CommandHandler('list', list_crn)
stopcrn_handler =CommandHandler('stop', stop, pass_args=True)
dispatcher.add_handler(help_handler)
dispatcher.add_handler(start_handler)
dispatcher.add_handler(checkcrn_handler)
dispatcher.add_handler(listcrn_handler)
dispatcher.add_handler(stopcrn_handler)
updater.start_polling()
updater.idle()