Compare commits

..

No commits in common. "44628a302150a2bb3d8ff04db20dfd8960e5ba13" and "d0cd483b7384e2cd063018ea1c2d18fa93b00280" have entirely different histories.

10 changed files with 126 additions and 118 deletions

62
main.py
View File

@ -11,18 +11,16 @@ import importlib
# global variables # global variables
STOP_REQUESTED = False STOP_REQUESTED = False
# some functions that increase readability of the code # some functions that increase readability of the code
def readfile(filename): def readfile(filename):
try: try:
return codecs.open(filename, encoding="utf-8").read() return codecs.open(filename, encoding = "utf-8").read()
except FileNotFoundError: except FileNotFoundError:
return False return False
except Exception as e: except Exception as e:
print(f"[ERROR] Unexpected error occurred in readfile() ({e})") print( "[ERROR] Unexpected error occured in readfile() ({0})".format(e) )
return False return False
# module object classes # module object classes
class ModuleV1: class ModuleV1:
def __init__(self, path, code, enabled, alias, predefine): def __init__(self, path, code, enabled, alias, predefine):
@ -44,8 +42,7 @@ class ModuleV1:
try: try:
exec(self.predefine) exec(self.predefine)
except Exception as e: except Exception as e:
print(f"[ERROR] module v1: module \"{self.path}\" ({self.alias}) raised exception \"{e}\" " print("[ERROR] module v1: module \"{}\" ({}) raised exception \"{}\" during predefine stage, disabling it...".format(self.path, self.alias, e))
f"during predefine stage, disabling it...")
# running the module # running the module
def process(self, msg): def process(self, msg):
@ -56,10 +53,9 @@ class ModuleV1:
exec(self.code) exec(self.code)
return self.RESPONCE return self.RESPONCE
except Exception as e: except Exception as e:
print(f"[ERROR] module v1: module \"{self.path}\" ({self.alias}) raised exception \"{e}\"") print("[ERROR] module v1: module \"{}\" ({}) raised exception \"{}\"".format(self.path, self.alias, e))
return "" return ""
class ModuleV2: class ModuleV2:
def __init__(self, path, index_file, enabled, alias): def __init__(self, path, index_file, enabled, alias):
self.version = 2 self.version = 2
@ -72,8 +68,8 @@ class ModuleV2:
# running the module # running the module
def process(self, msg): def process(self, msg):
try: try:
response = self.obj.process(msg, self.path) responce = self.obj.process(msg, self.path)
return response return responce
except Exception as e: except Exception as e:
print(f"[ERROR] module v2: module \"{self.path}\" ({self.alias}) raised exception \"{e}\"") print(f"[ERROR] module v2: module \"{self.path}\" ({self.alias}) raised exception \"{e}\"")
return None return None
@ -92,7 +88,7 @@ class ModuleControlUnit:
try: try:
meta_raw = readfile("modules/{}/meta.json".format(folder)) meta_raw = readfile("modules/{}/meta.json".format(folder))
if not meta_raw: if not meta_raw:
print(f"[WARN] module_loader: no meta.json found in module folder \"{folder}\"") print("[WARN] module_loader: no meta.json found in module folder \"{}\"".format(folder))
continue continue
meta = json.loads( meta_raw ) meta = json.loads( meta_raw )
@ -107,7 +103,7 @@ class ModuleControlUnit:
if not code: # False both when readfile() returns False and when the code string is empty if not code: # False both when readfile() returns False and when the code string is empty
print("[WARN] reload_modules: module {} does not have any code, skipping...".format(folder)) print("[WARN] reload_modules: module {} does not have any code, skipping...".format(folder))
continue continue
if "start_on_boot" in meta: if "start_on_boot" in meta:
enabled = meta["start_on_boot"] enabled = meta["start_on_boot"]
else: else:
@ -123,10 +119,9 @@ class ModuleControlUnit:
else: else:
predefine = False predefine = False
self.modules.append(ModuleV1(f"modules/{folder}/", code, enabled, alias, predefine)) self.modules.append( ModuleV1( "modules/{}/".format(folder), code, enabled, alias, predefine ) )
print(f"[INFO] reload_modules: successfully loaded {folder} as {alias} " print("[INFO] reload_modules: successfully loaded {} as {} (start_on_boot: {})".format(folder, alias, enabled))
f"(start_on_boot: {enabled})")
elif meta["version"] == 2: elif meta["version"] == 2:
if "index_file" in meta: if "index_file" in meta:
@ -146,15 +141,19 @@ class ModuleControlUnit:
self.modules.append(ModuleV2(f"modules/{folder}/", index_file, enabled, alias)) self.modules.append(ModuleV2(f"modules/{folder}/", index_file, enabled, alias))
print(f"[INFO] reload_modules: successfully loaded {folder} as {alias} " print(f"[INFO] reload_modules: successfully loaded {folder} as {alias} (start_on_boot: {enabled})")
f"(start_on_boot: {enabled})")
else: else:
print(f"[WARN] reload_modules: module {folder} requires unsupported version " print(f"[WARN] reload_modules: module {folder} requires unsupported version ({meta['version']} > 2), skipping...")
f"({meta['version']} > 2), skipping...")
except Exception as e: except Exception as e:
print(f"[ERROR] module_loader: error while loading module \"{folder}\" ({e})") print("[ERROR] module_loader: error while loading module \"{}\" ({})".format(folder, e))
# message queue object to go back to synchronous message processing
#class MessageQueue:
# def __init__(self):
# print("[INFO] Initializing the message queue...")
# self.queue = []
# synchronous message processor # synchronous message processor
@ -164,12 +163,12 @@ def queue_processor():
if len(message_queue) > 0: if len(message_queue) > 0:
msg = message_queue[0] msg = message_queue[0]
print("[DEBUG] queue_processor: {}".format(msg)) # debug print("[DEBUG] queue_processor: {}".format(msg)) # debug
# check for control commands # check for control commands
if msg["chat"]["id"] == 575246355: if msg["chat"]["id"] == 575246355:
if msg["text"][0] == "$": if msg["text"][0] == "$":
command = msg["text"][1:].split(" ") command = msg["text"][1:].split(" ")
if len(command) >= 2 and command[0] == "module": if len(command) >= 2 and command[0] == "module":
if command[1] == "reload": if command[1] == "reload":
print("[INFO] Module reloading triggered by a command") print("[INFO] Module reloading triggered by a command")
@ -181,21 +180,20 @@ def queue_processor():
del mcu.modules[:] del mcu.modules[:]
mcu.reload_modules() mcu.reload_modules()
del message_queue[0] del message_queue[0]
continue continue
# modules are used in here # modules are used in here
for mod in mcu.modules: for mod in mcu.modules:
if mod.enabled: if mod.enabled:
if mod.version == 1 or mod.version == 2: if mod.version == 1 or mod.version == 2:
responce = mod.process(msg) responce = mod.process(msg)
if responce: if responce:
updater.bot.send_message(chat_id=msg.chat.id, text=responce, updater.bot.send_message(chat_id = msg.chat.id, text = responce, disable_web_page_preview = True)
disable_web_page_preview=True)
print(f"Responded using module {mod.path} ({mod.alias}) with text: {responce}") print(f"Responded using module {mod.path} ({mod.alias}) with text: {responce}")
break break
del message_queue[0] del message_queue[0]
time.sleep(0.1) time.sleep(0.1)
@ -204,8 +202,8 @@ def queue_processor():
break break
else: else:
time.sleep(1) time.sleep(1)
except Exception: except Exception as e:
print(f"[ERROR] queue_processor: current message queue: {message_queue}") print("[ERROR] queue_processor: current message queue: {}".format(message_queue))
print("[ERROR] queue_processor: error while processing message, trying to delete it...") print("[ERROR] queue_processor: error while processing message, trying to delete it...")
try: try:
del message_queue[0] del message_queue[0]
@ -218,7 +216,7 @@ def queue_processor():
# telegram bot processor # telegram bot processor
def message_handler(update, context): def message_handler(update, context):
print("[DEBUG] Received new message") # just for testing print("[DEBUG] Received new message") # just for testing
message_queue.append(update.message) message_queue.append(update.message)
@ -231,7 +229,7 @@ message_queue = []
mcu = ModuleControlUnit() mcu = ModuleControlUnit()
processor_thread = threading.Thread(target=queue_processor, args=[]) processor_thread = threading.Thread( target = queue_processor, args = [] )
processor_thread.start() processor_thread.start()
@ -243,7 +241,7 @@ if not TOKEN:
sys.exit(1) sys.exit(1)
# connect to Telegram servers # connect to Telegram servers
updater = Updater(TOKEN, use_context=True) updater = Updater(TOKEN, use_context = True)
dispatcher = updater.dispatcher dispatcher = updater.dispatcher
# assign the handler for messages # assign the handler for messages

View File

@ -12,18 +12,16 @@ from telegram import Message, Chat
# global variables # global variables
STOP_REQUESTED = False STOP_REQUESTED = False
# some functions that increase readability of the code # some functions that increase readability of the code
def readfile(filename): def readfile(filename):
try: try:
return codecs.open(filename, encoding="utf-8").read() return codecs.open(filename, encoding = "utf-8").read()
except FileNotFoundError: except FileNotFoundError:
return False return False
except Exception as e: except Exception as e:
print(f"[ERROR] Unexpected error occurred in readfile() ({e})") print( "[ERROR] Unexpected error occured in readfile() ({0})".format(e) )
return False return False
# module object classes # module object classes
class ModuleV1: class ModuleV1:
def __init__(self, path, code, enabled, alias, predefine): def __init__(self, path, code, enabled, alias, predefine):
@ -39,14 +37,13 @@ class ModuleV1:
# set environmental variables # set environmental variables
def set_env(self): def set_env(self):
self.RESPONSE = "" self.RESPONCE = ""
def set_predefine(self): def set_predefine(self):
try: try:
exec(self.predefine) exec(self.predefine)
except Exception as e: except Exception as e:
print(f"[ERROR] module v1: module \"{self.path}\" ({self.alias}) raised exception \"{e}\" " print("[ERROR] module v1: module \"{}\" ({}) raised exception \"{}\" during predefine stage, disabling it...".format(self.path, self.alias, e))
f"during predefine stage, disabling it...")
# running the module # running the module
def process(self, msg): def process(self, msg):
@ -55,9 +52,9 @@ class ModuleV1:
self.MESSAGE = msg self.MESSAGE = msg
try: try:
exec(self.code) exec(self.code)
return self.RESPONSE return self.RESPONCE
except Exception as e: except Exception as e:
print(f"[ERROR] module v1: module \"{self.path}\" ({self.alias}) raised exception \"{e}\"") print("[ERROR] module v1: module \"{}\" ({}) raised exception \"{}\"".format(self.path, self.alias, e))
return "" return ""
@ -93,10 +90,10 @@ class ModuleControlUnit:
try: try:
meta_raw = readfile("modules/{}/meta.json".format(folder)) meta_raw = readfile("modules/{}/meta.json".format(folder))
if not meta_raw: if not meta_raw:
print(f"[WARN] module_loader: no meta.json found in module folder \"{folder}\"") print("[WARN] module_loader: no meta.json found in module folder \"{}\"".format(folder))
continue continue
meta = json.loads(meta_raw) meta = json.loads( meta_raw )
if "version" in meta: if "version" in meta:
if meta["version"] == 1: if meta["version"] == 1:
if "index_file" in meta: if "index_file" in meta:
@ -104,11 +101,11 @@ class ModuleControlUnit:
else: else:
index_file = "index.py" index_file = "index.py"
code = readfile("modules/{}/{}".format(folder, index_file)) code = readfile( "modules/{}/{}".format(folder, index_file) )
if not code: # False both when readfile() returns False and when the code string is empty if not code: # False both when readfile() returns False and when the code string is empty
print(f"[WARN] reload_modules: module {folder} does not have any code, skipping...") print("[WARN] reload_modules: module {} does not have any code, skipping...".format(folder))
continue continue
if "start_on_boot" in meta: if "start_on_boot" in meta:
enabled = meta["start_on_boot"] enabled = meta["start_on_boot"]
else: else:
@ -124,10 +121,9 @@ class ModuleControlUnit:
else: else:
predefine = False predefine = False
self.modules.append(ModuleV1(f"modules/{folder}/", code, enabled, alias, predefine)) self.modules.append( ModuleV1( "modules/{}/".format(folder), code, enabled, alias, predefine ) )
print(f"[INFO] reload_modules: successfully loaded {folder} as {alias} " print("[INFO] reload_modules: successfully loaded {} as {} (start_on_boot: {})".format(folder, alias, enabled))
f"(start_on_boot: {enabled})")
elif meta["version"] == 2: elif meta["version"] == 2:
if "index_file" in meta: if "index_file" in meta:
@ -147,15 +143,21 @@ class ModuleControlUnit:
self.modules.append(ModuleV2(f"modules/{folder}/", index_file, enabled, alias)) self.modules.append(ModuleV2(f"modules/{folder}/", index_file, enabled, alias))
print(f"[INFO] reload_modules: successfully loaded {folder} as {alias} " print(f"[INFO] reload_modules: successfully loaded {folder} as {alias} (start_on_boot: {enabled})")
f"(start_on_boot: {enabled})")
else: else:
print(f"[WARN] reload_modules: module {folder} requires unsupported version " print(f"[WARN] reload_modules: module {folder} requires unsupported version ({meta['version']} > 2), skipping...")
f"({meta['version']} > 2), skipping...")
except Exception as e: except Exception as e:
print(f"[ERROR] module_loader: error while loading module \"{folder}\" ({e})") print("[ERROR] module_loader: error while loading module \"{}\" ({})".format(folder, e))
# message queue object to go back to synchronous message processing
#class MessageQueue:
# def __init__(self):
# print("[INFO] Initializing the message queue...")
# self.queue = []
# synchronous message processor # synchronous message processor
def queue_processor(): def queue_processor():
@ -163,13 +165,13 @@ def queue_processor():
try: try:
if len(message_queue) > 0: if len(message_queue) > 0:
msg = message_queue[0] msg = message_queue[0]
print("[DEBUG] queue_processor: {}".format(msg)) # debug print("[DEBUG] queue_processor: {}".format(msg)) # debug
# check for control commands # check for control commands
if msg["chat"]["id"] == 575246355: if msg["chat"]["id"] == 575246355:
if msg["text"][0] == "$": if msg["text"][0] == "$":
command = msg["text"][1:].split(" ") command = msg["text"][1:].split(" ")
if len(command) >= 2 and command[0] == "module": if len(command) >= 2 and command[0] == "module":
if command[1] == "reload": if command[1] == "reload":
print("[INFO] Module reloading triggered by a command") print("[INFO] Module reloading triggered by a command")
@ -181,7 +183,7 @@ def queue_processor():
del mcu.modules[:] del mcu.modules[:]
mcu.reload_modules() mcu.reload_modules()
del message_queue[0] del message_queue[0]
continue continue
@ -189,9 +191,9 @@ def queue_processor():
for mod in mcu.modules: for mod in mcu.modules:
if mod.enabled: if mod.enabled:
if mod.version == 1 or mod.version == 2: if mod.version == 1 or mod.version == 2:
response = mod.process(msg) responce = mod.process(msg)
if response: if responce:
print(f"Responded using module {mod.path} ({mod.alias}) with text: {response}") print(f"Responded using module {mod.path} ({mod.alias}) with text: {responce}")
break break
del message_queue[0] del message_queue[0]
@ -201,8 +203,8 @@ def queue_processor():
else: else:
time.sleep(1) time.sleep(1)
except Exception: except Exception as e:
print(f"[ERROR] queue_processor: current message queue: {message_queue}") print("[ERROR] queue_processor: current message queue: {}".format(message_queue))
print("[ERROR] queue_processor: error while processing message, trying to delete it...") print("[ERROR] queue_processor: error while processing message, trying to delete it...")
try: try:
del message_queue[0] del message_queue[0]
@ -222,9 +224,10 @@ message_queue = []
mcu = ModuleControlUnit() mcu = ModuleControlUnit()
processor_thread = threading.Thread(target=queue_processor, args=[]) processor_thread = threading.Thread( target = queue_processor, args = [] )
processor_thread.start() processor_thread.start()
print("Enter testing messages one by one, end with an empty line") print("Enter testing messages one by one, end with an empty line")
while True: while True:
@ -232,4 +235,4 @@ while True:
if len(new_msg) == 0: if len(new_msg) == 0:
break break
message_queue.append(Message(9, round(time.time()), Chat(575246355, 'supergroup'), text=new_msg)) message_queue.append(Message(9, round(time.time()), Chat(575246355, 'supergroup'), text = new_msg))

View File

@ -1,31 +1,33 @@
from datetime import datetime from datetime import datetime
import json import json
def readfile(filename): def readfile(filename):
with open(module_path + filename) as f: with open(module_path + filename) as f:
return f.read() return f.read()
# global constants # global constants
# Accusative - znahidnyj # Accusative - znahidnyj
WEEKDAYS_ACCUSATIVE = ["понеділок", "вівторок", "середу", "четвер", "п'ятницю", "суботу", "неділю"] WEEKDAYS_ACCUSATIVE = ["понедулок", "вівторок", "середу", "четвер",
"п'ятницю", "суботу", "неділю"]
# Genitive - rodovyj # Genitive - rodovyj
WEEKDAYS_GENITIVE_NEXT = ["наступного понеділка", "наступного вівторка", "наступної середи", "наступного четверга", WEEKDAYS_GENITIVE_NEXT = ["наступного понеділка", "наступного вівторка",
"наступної п'ятниці", "наступної суботи", "наступної неділі"] "наступної середи", "наступного четверга",
"наступної п'ятниці", "наступної суботи",
"наступної неділі"]
WEEKDAYS_GENITIVE_THIS = ["цього понеділка", "цього вівторка",
"цієї середи", "цього четверга",
"цієї п'ятниці", "цієї суботи",
"цієї неділі"]
WEEKDAYS_GENITIVE_THIS = ["цього понеділка", "цього вівторка", "цієї середи", "цього четверга", "цієї п'ятниці",
"цієї суботи", "цієї неділі"]
# global variables # global variables
module_path = "" module_path = ""
def get_human_readable_date(start_datetime, end_datetime, def get_human_readable_date(start_datetime, end_datetime,
current_day, current_week): current_day, current_week):
human_readable_date = "" human_readable_date = ""
if ((current_day + 2) == int(start_datetime.strftime("%u"))) or \ if ((current_day + 2) == int(start_datetime.strftime("%u"))) or ((current_day == 6) and (start_datetime.strftime("%u") == "1")):
((current_day == 6) and (start_datetime.strftime("%u") == "1")):
human_readable_date += "завтра " human_readable_date += "завтра "
elif current_week != int(start_datetime.strftime("%W")) % 2: elif current_week != int(start_datetime.strftime("%W")) % 2:
human_readable_date += f"{WEEKDAYS_GENITIVE_NEXT[int(start_datetime.strftime('%u')) - 1]} " human_readable_date += f"{WEEKDAYS_GENITIVE_NEXT[int(start_datetime.strftime('%u')) - 1]} "
@ -43,7 +45,9 @@ def get_human_readable_date(start_datetime, end_datetime,
return human_readable_date return human_readable_date
def generate_lesson_description(lesson, start_datetime, end_datetime, current_day, current_week, overrides={}): def generate_lesson_description(lesson, start_datetime, end_datetime,
current_day, current_week, overrides = {}):
output_settings = {"name": True, "date": True, "teacher": True, "link": True} output_settings = {"name": True, "date": True, "teacher": True, "link": True}
output_settings.update(overrides) output_settings.update(overrides)
@ -64,19 +68,19 @@ def generate_lesson_description(lesson, start_datetime, end_datetime, current_da
result += f"Посилання на пару: {lesson['link']}" result += f"Посилання на пару: {lesson['link']}"
return result return result
def get_schedule_data_from(filename): def get_schedule_data_from(filename):
raw_schedule = json.loads(readfile(filename)) raw_schedule = json.loads(readfile(filename))
baked_schedule = {} baked_schedule = {}
for day_number, lesson_times in enumerate(raw_schedule): for daynum, lesson_times in enumerate(raw_schedule):
for lesson_time in lesson_times: for lesson_time in lesson_times:
timestamp = day_number * 86400 + int(lesson_time.split(":")[0]) * 3600 \ timestamp = daynum*86400 + int(lesson_time.split(":")[0])*3600 \
+ int(lesson_time.split(":")[1]) * 60 + int(lesson_time.split(":")[1])*60
new_record = dict(raw_schedule[day_number][lesson_time]) new_record = dict(raw_schedule[daynum][lesson_time])
new_record["source"] = filename.split(".json")[0] new_record["source"] = filename.split(".json")[0]
baked_schedule[timestamp] = new_record baked_schedule[timestamp] = new_record
@ -93,7 +97,7 @@ def process_arguments(args, base_day):
selected_day -= int(arg[1:]) selected_day -= int(arg[1:])
else: else:
preferences[arg[1:]] = False preferences[arg[1:]] = False
elif arg[0] == "+": elif arg[0] == "+":
if arg[1:].isdigit(): if arg[1:].isdigit():
selected_day += int(arg[1:]) selected_day += int(arg[1:])
@ -105,20 +109,21 @@ def process_arguments(args, base_day):
return preferences, selected_day return preferences, selected_day
def get_lesson_description(schedule, reference_time, lesson_time, current_day, current_week, overrides={}): def get_lesson_description(schedule, reference_time, lesson_time,
current_day, current_week, overrides = {}):
lesson_record = schedule[lesson_time] lesson_record = schedule[lesson_time]
lesson_start_datetime = datetime.fromtimestamp(reference_time + lesson_time) lesson_start_datetime = datetime.fromtimestamp(reference_time + lesson_time)
lesson_end_datetime = datetime.fromtimestamp(reference_time + lesson_time + 5400) lesson_end_datetime = datetime.fromtimestamp(reference_time + lesson_time + 5400)
return generate_lesson_description(lesson_record, lesson_start_datetime, lesson_end_datetime, current_day, return generate_lesson_description(lesson_record, lesson_start_datetime, lesson_end_datetime,
current_week, overrides=overrides) current_day, current_week, overrides = overrides)
def process(message, path): def process(message, path):
message_text = message["text"] message_text = message["text"]
full_command = message_text.split() full_command = message_text.split()
# there is no need to check if the full_command list if empty as it # there is no need to check if the full_command list if empty as it
# never will be - Telegram requires all messages to have at least # never will be - Telegram requires all messages to have at least
# one printable symbol, so this is already protected # one printable symbol, so this is already protected
@ -134,28 +139,30 @@ def process(message, path):
schedule.update(get_schedule_data_from("additions.json")) schedule.update(get_schedule_data_from("additions.json"))
current_time = datetime.now() current_time = datetime.now()
current_week = current_time.isocalendar()[1] % 2 current_week = current_time.isocalendar()[1] % 2
current_day = current_time.weekday() current_day = current_time.weekday()
current_seconds = current_week * 604800 + current_day * 86400 + current_time.hour * 3600 + current_time.minute \ current_seconds = current_week*604800 + current_day*86400 \
* 60 + current_time.second + current_time.hour*3600 + current_time.minute*60 \
+ current_time.second
reference_time = int(current_time.strftime("%s")) - current_seconds reference_time = int(current_time.strftime("%s")) - current_seconds
if base_command == "!пара": if base_command == "!пара":
upcoming_lessons = [timestamp for timestamp in schedule if timestamp > current_seconds - 5400] upcoming_lessons = [timestamp for timestamp in schedule if timestamp > current_seconds-5400]
if len(upcoming_lessons) > 0: if len(upcoming_lessons) > 0:
closest_lesson_time = min(upcoming_lessons) closest_lesson_time = min(upcoming_lessons)
else: else:
closest_lesson_time = min(schedule) closest_lesson_time = min(schedule)
return "Актуальна пара: " + get_lesson_description(schedule, reference_time, closest_lesson_time, current_day, return "Актуальна пара: " + get_lesson_description(schedule, reference_time,
closest_lesson_time, current_day,
current_week) current_week)
elif base_command == "!пари": elif base_command == "!пари":
base_day = current_week * 7 + current_day base_day = current_week*7 + current_day
if len(full_command) >= 2: if len(full_command) >= 2:
args = [i for i in full_command[1:] if len(i) > 1] args = [i for i in full_command[1:] if len(i) > 1]
@ -164,10 +171,12 @@ def process(message, path):
preferences = {} preferences = {}
selected_day = base_day selected_day = base_day
lesson_list = [i for i in schedule if selected_day * 86400 <= i < (selected_day + 1) * 86400] lesson_list = [i for i in schedule if selected_day*86400 <= i < (selected_day+1)*86400]
lesson_descriptions_list = ["Назва: " + get_lesson_description(schedule, reference_time, lesson_time, lesson_descriptions_list = ["Назва: " + get_lesson_description(schedule, reference_time,
current_day, current_week, overrides=preferences) lesson_time, current_day,
current_week, overrides = preferences)
for lesson_time in lesson_list] for lesson_time in lesson_list]
return f"Пари у {WEEKDAYS_ACCUSATIVE[selected_day % 7]}:\n" + "\n\n".join(lesson_descriptions_list) return f"Пари у {WEEKDAYS_ACCUSATIVE[selected_day % 7]}:\n" \
+ "\n\n".join(lesson_descriptions_list)

View File

@ -79,10 +79,10 @@ if self.MESSAGE["text"].lower() == "!пара-old2":
human_readable_date += " до " human_readable_date += " до "
human_readable_date += dt_lesson_finish.strftime("%H:%M") human_readable_date += dt_lesson_finish.strftime("%H:%M")
self.RESPONSE = "Актуальна пара: {}\nДата: {}\nВикладач: {}\nПосилання на пару: {}".format(p['name'], human_readable_date, p['teacher'], p['link']) self.RESPONCE = "Актуальна пара: {}\nДата: {}\nВикладач: {}\nПосилання на пару: {}".format(p['name'], human_readable_date, p['teacher'], p['link'])
print("test3.1.5") print("test3.1.5")
else: else:
self.RESPONSE = "Пар немає взагалі. Ми вільні!" self.RESPONCE = "Пар немає взагалі. Ми вільні!"
else: else:
print("test3.2") print("test3.2")
@ -106,7 +106,7 @@ if self.MESSAGE["text"].lower() == "!пара-old2":
human_readable_date += " до " human_readable_date += " до "
human_readable_date += dt_lesson_finish.strftime("%H:%M") human_readable_date += dt_lesson_finish.strftime("%H:%M")
self.RESPONSE = "Актуальна пара: {}\nДата: {}\nВикладач: {}\nПосилання на пару: {}".format(p['name'], human_readable_date, p['teacher'], p['link']) self.RESPONCE = "Актуальна пара: {}\nДата: {}\nВикладач: {}\nПосилання на пару: {}".format(p['name'], human_readable_date, p['teacher'], p['link'])
if self.MESSAGE["text"].lower().split()[0] == "!пари-old2": if self.MESSAGE["text"].lower().split()[0] == "!пари-old2":
command = self.MESSAGE["text"].lower().split() command = self.MESSAGE["text"].lower().split()
@ -183,4 +183,4 @@ if self.MESSAGE["text"].lower().split()[0] == "!пари-old2":
result_text += "\n" result_text += "\n"
self.RESPONSE = result_text self.RESPONCE = result_text

View File

@ -24,7 +24,7 @@ if self.MESSAGE["text"].lower() == "!пара-old":
pair_found = True pair_found = True
break break
self.RESPONSE = f"Сьогодні вихідний, тому пар немає)\n"\ self.RESPONCE = f"Сьогодні вихідний, тому пар немає)\n"\
f"Наступна пара - {next_pair['subject']} ({next_pair['lector']}) о {self.reverse_timetable[int(j)]} у {self.days_rod[day]}\n"\ f"Наступна пара - {next_pair['subject']} ({next_pair['lector']}) о {self.reverse_timetable[int(j)]} у {self.days_rod[day]}\n"\
f"Посилання (якщо воно чомусь треба): {next_pair['link']}" f"Посилання (якщо воно чомусь треба): {next_pair['link']}"
else: else:
@ -33,13 +33,13 @@ if self.MESSAGE["text"].lower() == "!пара-old":
print("[DEBUG] Looking up a relevant pair...") print("[DEBUG] Looking up a relevant pair...")
try: try:
relevant_pair = schedule[current_week][current_day][str(self.timetable[i])] relevant_pair = schedule[current_week][current_day][str(self.timetable[i])]
self.RESPONSE = f"Актуальна пара: {relevant_pair['subject']} ({relevant_pair['lector']}), початок о {self.reverse_timetable[self.timetable[i]]}\n"\ self.RESPONCE = f"Актуальна пара: {relevant_pair['subject']} ({relevant_pair['lector']}), початок о {self.reverse_timetable[self.timetable[i]]}\n"\
f"Посилання: {relevant_pair['link']}" f"Посилання: {relevant_pair['link']}"
break break
except Exception as e: except Exception as e:
print(f"[WARN] module: auto-schedule: exception {e} while looking up the pair") print(f"[WARN] module: auto-schedule: exception {e} while looking up the pair")
else: else:
self.RESPONSE = "Сьогодні більше немає пар" self.RESPONCE = "Сьогодні більше немає пар"
except Exception as e: except Exception as e:
print(f"[WARN] module: auto-schedule: failed to process schedule.json ({e})") print(f"[WARN] module: auto-schedule: failed to process schedule.json ({e})")

View File

@ -1,2 +1,2 @@
if msg.chat["type"] == "private": if msg.chat["type"] == "private":
self.RESPONSE = self.MESSAGE["text"] self.RESPONCE = self.MESSAGE["text"]

View File

@ -9,5 +9,4 @@ if "%" in self.MESSAGE["text"]:
tagged_users |= self.tag_sets[i] tagged_users |= self.tag_sets[i]
if tagging_issued: if tagging_issued:
self.RESPONSE = f"Користувач використав масовий тег з повідомленням: {self.MESSAGE['text']}\n\n" \ self.RESPONCE = "Користувач використав масовий тег з повідомленням: {}\n\n{}".format(self.MESSAGE["text"], " ".join(tagged_users))
f"{' '.join(tagged_users)}"

View File

@ -2,5 +2,5 @@
"trigger_lists": [ "trigger_lists": [
["коли", "тест", "обж"] ["коли", "тест", "обж"]
], ],
"response_text": "Тести з ОБЖ необхідно проходити лише тим студентам, які не були на практичному занятті. Якщо Ви були на практиці, але все одно пройдете тест, то ризикуєте отримати нижчу оцінку та знизити свій загальний бал" "responce_text": "Тести з ОБЖ необхідно проходити лише тим студентам, які не були на практичному занятті. Якщо Ви були на практиці, але все одно пройдете тест, то ризикуєте отримати нижчу оцінку та знизити свій загальний бал"
} }

View File

@ -1,24 +1,24 @@
msg = self.MESSAGE["text"].lower() msg = self.MESSAGE["text"].lower()
response_given = False responce_given = False
for file in os.listdir(self.path + "db/"): for file in os.listdir(self.path + "db/"):
if response_given: if responce_given:
break break
try: try:
criteria = json.loads(readfile(self.path + "db/" + file)) criteria = json.loads( readfile(self.path + "db/" + file) )
for word_set in criteria["trigger_lists"]: for wordset in criteria["trigger_lists"]:
all_words_in = True all_words_in = True
for word in word_set: for word in wordset:
if word not in msg: if word not in msg:
all_words_in = False all_words_in = False
break break
if all_words_in: if all_words_in:
self.RESPONSE = criteria["response_text"] self.RESPONCE = criteria["responce_text"]
response_given = True responce_given = True
break break
except Exception as e: except Exception as e:

View File

@ -17,15 +17,14 @@ if (command[0] in self.aliases) and (1 <= command_length <= 3):
decoded_text = text_to_decode decoded_text = text_to_decode
if chosen_model not in models: if chosen_model not in models:
self.RESPONSE = f"Такого варіанту транслітерації не існує. Доступні варіанти: " \ self.RESPONCE = f"Такого варіанту транслітерації не існує. Доступні варіанти: {', '.join(list(models.keys()))}"
f"{', '.join(list(models.keys()))}"
else: else:
for i in models[chosen_model]: for i in models[chosen_model]:
decoded_text = decoded_text.replace(i[0], i[1]) decoded_text = decoded_text.replace(i[0], i[1])
decoded_text = decoded_text.replace(i[0].capitalize(), i[1].capitalize()) decoded_text = decoded_text.replace(i[0].capitalize(), i[1].capitalize())
decoded_text = decoded_text.replace(i[0].upper(), i[1].upper()) decoded_text = decoded_text.replace(i[0].upper(), i[1].upper())
self.RESPONSE = f"Результат: {decoded_text}" self.RESPONCE = f"Результат: {decoded_text}"
except Exception as e: except Exception as e:
print(f"[translit-decoder] Got exception: {e}") print(f"[translit-decoder] Got exception: {e}")