add first course

This commit is contained in:
kr0sh512
2024-09-03 00:40:08 +03:00
parent 3c0be433ae
commit 28b2640327
4 changed files with 100 additions and 72 deletions
+3
View File
@@ -3,5 +3,8 @@ users.json
schedule.json schedule.json
json/students.json json/students.json
json/schedule_2sem.json json/schedule_2sem.json
test_json
json_remake
script.py
nohup.out nohup.out
config.yaml config.yaml
+29 -49
View File
@@ -58,50 +58,20 @@ def change_user_param(id, key, value):
with open(path_users, "w", encoding="utf-8") as json_file: with open(path_users, "w", encoding="utf-8") as json_file:
json.dump(data, json_file, ensure_ascii=False, indent=4) json.dump(data, json_file, ensure_ascii=False, indent=4)
create_schedule_tasks() create_schedule_tasks()
bot.send_admin_message(
"Изменения:\ # bot.send_admin_message(
\n<i>user:</i> @{}\ # "Изменения:\
\n<i>id:</i> <code>{}</code>\ # \n<i>user:</i> @{}\
\n<i>key:</i> <code>{}</code>\ # \n<i>id:</i> <code>{}</code>\
\n<i>value:</i> <code>{}</code>".format( # \n<i>key:</i> <code>{}</code>\
data[id]["username"], id, key, value # \n<i>value:</i> <code>{}</code>".format(
) # data[id]["username"], id, key, value
) # )
# )
return return
def parse_lesson_tmp(time, lesson):
text = ""
start, end = time.split("-")
if len(lesson["infos"].split("|")) == 2:
teacher = lesson["infos"].split("|")[0]
room = lesson["infos"].split("|")[1]
text = "{}-{} | {}\
\n<b>{}</b>\
\n<i>{}</i>".format(
start, end, room, lesson["name"], teacher
)
elif len(lesson["infos"].split("|")) == 4:
infos = lesson["infos"].split("|")
text = "{}-{}\
\n<b>{}</b>\
\n({}) <i>{}</i>\
\n({}) <i>{}</i>".format(
start, end, lesson["name"], infos[1], infos[0], infos[3], infos[2]
)
elif len(lesson["infos"].split("|")) == 1:
text = "{}-{}\
\n<b>{}</b>".format(
start, end, lesson["name"]
)
else:
print("что-то не так")
print(lesson["name"])
return text
def parse_lesson(lesson: dict[str, str]) -> str: def parse_lesson(lesson: dict[str, str]) -> str:
text = "" text = ""
start, end = lesson["begin"], lesson["end"] start, end = lesson["begin"], lesson["end"]
@@ -128,6 +98,8 @@ def parse_lesson(lesson: dict[str, str]) -> str:
) )
for tr in lesson["teacher.room"]: for tr in lesson["teacher.room"]:
if not tr["t"] and not tr["r"]:
continue
text += "\n({}) <i>{}</i>".format(tr["r"], tr["t"]) text += "\n({}) <i>{}</i>".format(tr["r"], tr["t"])
return text return text
@@ -140,7 +112,7 @@ def create_schedule_tasks(manual=False):
if not allow_update: if not allow_update:
return return
bot.send_admin_message("Произошёл auto-update") # bot.send_admin_message("Произошёл auto-update")
allow_update = True allow_update = True
@@ -184,27 +156,27 @@ def create_schedule_tasks(manual=False):
if day == "mon": if day == "mon":
schedule.every().monday.at(start_task).do( schedule.every().monday.at(start_task).do(
bot.send_message, id, text, thread_id, lesson["partity"] bot.send_message, id, text, thread_id, lesson["parity"]
) )
elif day == "tue": elif day == "tue":
schedule.every().tuesday.at(start_task).do( schedule.every().tuesday.at(start_task).do(
bot.send_message, id, text, thread_id, lesson["partity"] bot.send_message, id, text, thread_id, lesson["parity"]
) )
elif day == "wed": elif day == "wed":
schedule.every().wednesday.at(start_task).do( schedule.every().wednesday.at(start_task).do(
bot.send_message, id, text, thread_id, lesson["partity"] bot.send_message, id, text, thread_id, lesson["parity"]
) )
elif day == "thu": elif day == "thu":
schedule.every().thursday.at(start_task).do( schedule.every().thursday.at(start_task).do(
bot.send_message, id, text, thread_id, lesson["partity"] bot.send_message, id, text, thread_id, lesson["parity"]
) )
elif day == "fri": elif day == "fri":
schedule.every().friday.at(start_task).do( schedule.every().friday.at(start_task).do(
bot.send_message, id, text, thread_id, lesson["partity"] bot.send_message, id, text, thread_id, lesson["parity"]
) )
elif day == "sat": elif day == "sat":
schedule.every().saturday.at(start_task).do( schedule.every().saturday.at(start_task).do(
bot.send_message, id, text, thread_id, lesson["partity"] bot.send_message, id, text, thread_id, lesson["parity"]
) )
return return
@@ -311,8 +283,16 @@ def get_schedule(id: str, day: str) -> str:
) )
for lesson in schdl_today: for lesson in schdl_today:
text += "" + parse_lesson(lesson) + "\n\n" str_lesson = parse_lesson(lesson)
if str_lesson:
text += "" + str_lesson + "\n\n"
except: except:
return "Расписания твоей группы нет" return "Расписания твоей группы нет"
return text return text
if __name__ == "__main__":
print(groups_in_json())
pass
+22
View File
@@ -1,4 +1,26 @@
[ [
"101",
"102",
"103",
"104",
"105",
"106",
"107",
"108",
"109",
"110",
"111",
"112",
"113",
"114",
"115",
"116",
"117",
"118",
"119",
"120",
"141",
"142",
"201", "201",
"202", "202",
"203", "203",
+46 -23
View File
@@ -10,7 +10,7 @@ from admin import admin_command, is_admin, send_admin_message, send_admin_docume
config = yaml.safe_load(open("config.yaml")) config = yaml.safe_load(open("config.yaml"))
bot = telebot.TeleBot(config["test_token"]) bot = telebot.TeleBot(config["test_token"])
PARTITY_FIRST = 1 parity_FIRST = 1
@bot.message_handler(commands=["help", "faq"]) @bot.message_handler(commands=["help", "faq"])
@@ -67,7 +67,7 @@ def test(message):
@admin_command @admin_command
def restart_bot(message): def restart_bot(message):
send_admin_message("bye") send_admin_message("bye")
os.execv(sys.executable, ["python"] + sys.argv) os.execv(sys.executable, ["python3", "-u"] + sys.argv)
@bot.message_handler(commands=["update"]) @bot.message_handler(commands=["update"])
@@ -147,7 +147,7 @@ def spam(message):
def spam_msg(message): def spam_msg(message):
if message.text == "stop" or message.text == "стоп" or message.text[0] == "/": if message.text == "stop" or message.text == "стоп" or message.text[0] == "/":
send_admin_message("Отмена отпраки") send_admin_message("Отмена отправки")
return return
send_admin_message("Пришли мне текст для рассылки сообщения этим пользователям") send_admin_message("Пришли мне текст для рассылки сообщения этим пользователям")
@@ -159,7 +159,7 @@ def spam_msg(message):
def spam_cnf(message, data): def spam_cnf(message, data):
if message.text == "stop" or message.text == "стоп" or message.text[0] == "/": if message.text == "stop" or message.text == "стоп" or message.text[0] == "/":
send_admin_message("Отмена отпраки") send_admin_message("Отмена отправки")
return return
global text_spam global text_spam
@@ -197,7 +197,7 @@ def spam_cnf(message, data):
@bot.callback_query_handler(func=lambda call: "spam" in call.data) @bot.callback_query_handler(func=lambda call: "spam" in call.data)
def spam_send(call): def spam_send(call):
if call.data == "spam_no": if call.data == "spam_no":
send_admin_message("Отмена отпраки") send_admin_message("Отмена отправки")
return return
tmp, data = call.data.split("#") tmp, data = call.data.split("#")
@@ -314,17 +314,14 @@ def start(message):
send_message(message.chat.id, start_txt) send_message(message.chat.id, start_txt)
markup = types.InlineKeyboardMarkup() markup = types.InlineKeyboardMarkup()
for i in for_json.groups_in_json(): # for i in for_json.groups_in_json():
markup.add(types.InlineKeyboardButton(text=i, callback_data=i)) # markup.add(types.InlineKeyboardButton(text=i, callback_data=i))
markup.add(types.InlineKeyboardButton(text="1 курс", callback_data="1course"))
markup.add(types.InlineKeyboardButton(text="2 курс", callback_data="2course"))
markup.add(
types.InlineKeyboardButton(
text="Моей группы нет в этом списке", callback_data="other"
)
)
bot.send_message( bot.send_message(
message.chat.id, message.chat.id,
"Пожалуйста, выбери свою группу", "Пожалуйста, выбери свой курс",
parse_mode=ParseMode.HTML, parse_mode=ParseMode.HTML,
reply_markup=markup, reply_markup=markup,
) )
@@ -333,9 +330,36 @@ def start(message):
@bot.callback_query_handler( @bot.callback_query_handler(
func=lambda call: call.data in for_json.groups_in_json() or call.data == "other" func=lambda call: call.data in for_json.groups_in_json()
or call.data in ["other", "1course", "2course"]
) )
def callback_inline(call): def callback_inline(call):
if "course" in call.data:
course = call.data[0]
markup = types.InlineKeyboardMarkup()
for i in [i for i in for_json.groups_in_json() if i[0] == course]:
markup.add(types.InlineKeyboardButton(text=i, callback_data=i))
markup.add(
types.InlineKeyboardButton(
text="Моей группы нет в этом списке", callback_data="other"
)
)
bot.edit_message_text(
text="Пожалуйста, выбери свою группу",
chat_id=call.message.chat.id,
message_id=call.message.message_id,
reply_markup=markup,
)
# bot.edit_message_reply_markup(
# call.message.chat.id, call.message.message_id, reply_markup=markup
# )
return
infos = [ infos = [
call.from_user.id, call.from_user.id,
call.from_user.first_name, call.from_user.first_name,
@@ -350,8 +374,7 @@ def callback_inline(call):
bot.edit_message_text( bot.edit_message_text(
chat_id=call.message.chat.id, chat_id=call.message.chat.id,
message_id=call.message.message_id, message_id=call.message.message_id,
text="Используй команду /request \ text="Используй команду /request и напиши номер группы, которую хочешь добавить\
и напиши номер группы, которую хочешь добавить\
\nПосле создания расписания для твоей группы, я пришлю тебе сообщение", \nПосле создания расписания для твоей группы, я пришлю тебе сообщение",
parse_mode=ParseMode.HTML, parse_mode=ParseMode.HTML,
) )
@@ -609,7 +632,7 @@ def request(message):
def send_request(message): def send_request(message):
if message.text == "stop" or message.text == "стоп" or message.text[0] == "/": if message.text == "stop" or message.text == "стоп" or message.text[0] == "/":
send_message(message.from_user.id, "Отмена отпраки") send_message(message.from_user.id, "Отмена отправки")
return return
infos = [ infos = [
@@ -658,15 +681,15 @@ def text_message(message):
return return
def send_message(id, text, thread_id="General", partity=None): def send_message(id, text, thread_id="General", parity=None):
if thread_id == "General": if thread_id == "General":
thread_id = None thread_id = None
if partity: if parity:
count_of_weeks = (datetime.now() - datetime(2024, 2, 5)).days // 7 count_of_weeks = (datetime.now() - datetime(2024, 2, 5)).days // 7
is_odd = (count_of_weeks + 1) % 2 == PARTITY_FIRST is_odd = (count_of_weeks + 1) % 2 == parity_FIRST
if is_odd != bool(partity): if is_odd != bool(parity):
return return
try: try:
@@ -691,8 +714,8 @@ def send_message(id, text, thread_id="General", partity=None):
text_error = "Error from user: @{} <code>{}</code>\n{}".format( text_error = "Error from user: @{} <code>{}</code>\n{}".format(
for_json.return_infos(id)["username"], id, str(e) for_json.return_infos(id)["username"], id, str(e)
) )
send_admin_message(text_error) # send_admin_message(text_error)
print(id, e) print(f"--- {text_error} ---")
return return