diff --git a/.gitignore b/.gitignore index 40b2a5e..9a143ba 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ users.json schedule.json settings.json json/students.json +json/schedule_2sem.json diff --git a/__pycache__/admin.cpython-311.pyc b/__pycache__/admin.cpython-311.pyc deleted file mode 100644 index e9dd4ec..0000000 Binary files a/__pycache__/admin.cpython-311.pyc and /dev/null differ diff --git a/__pycache__/for_json.cpython-311.pyc b/__pycache__/for_json.cpython-311.pyc deleted file mode 100644 index b294c17..0000000 Binary files a/__pycache__/for_json.cpython-311.pyc and /dev/null differ diff --git a/__pycache__/schedules.cpython-311.pyc b/__pycache__/schedules.cpython-311.pyc deleted file mode 100644 index afed869..0000000 Binary files a/__pycache__/schedules.cpython-311.pyc and /dev/null differ diff --git a/admin.py b/admin.py index bffd9e4..6fd75a1 100644 --- a/admin.py +++ b/admin.py @@ -24,5 +24,5 @@ def send_admin_message(text): bot.send_message(admin_id, '🛑 ' + text) def send_admin_document(file): - bot.send_document(admin_id, file, '🛑 Admin file') + bot.send_document(admin_id, file, '🛑 Admin file') \ No newline at end of file diff --git a/for_json.py b/for_json.py index 965147f..ae442db 100644 --- a/for_json.py +++ b/for_json.py @@ -4,7 +4,7 @@ import schedules as bot from datetime import datetime path_users = "json/users.json" # Нужный путь до json файлов -path_schedule = "json/schedule.json" +path_schedule = "json/schedule_2sem.json" path_students = "json/students.json" # path_logs = "json/logs.json" @@ -52,10 +52,11 @@ def save_user(infos): \nuser: @{}\ \ngroup: {}\ \nid: {}\ - \nname: {}".format(data[infos["id"]]["username"], + \nname: {} {}".format(data[infos["id"]]["username"], data[infos["id"]]["group"], infos["id"], - data[infos["id"]]["first_name"] + data[infos["id"]]["last_name"])) + data[infos["id"]]["first_name"], + data[infos["id"]]["last_name"])) return @@ -151,17 +152,17 @@ def create_schedule_tasks(manual=False): start_task = tmp[:-1] if day == 'mon': - schedule.every().monday.at(start_task).do(bot.send_message, id, text, thread_id) + schedule.every().monday.at(start_task).do(bot.send_message, id, text, thread_id, True) elif day == 'tue': - schedule.every().tuesday.at(start_task).do(bot.send_message, id, text, thread_id) + schedule.every().tuesday.at(start_task).do(bot.send_message, id, text, thread_id, True) elif day == 'wed': - schedule.every().wednesday.at(start_task).do(bot.send_message, id, text, thread_id) + schedule.every().wednesday.at(start_task).do(bot.send_message, id, text, thread_id, True) elif day == 'thu': - schedule.every().thursday.at(start_task).do(bot.send_message, id, text, thread_id) + schedule.every().thursday.at(start_task).do(bot.send_message, id, text, thread_id, True) elif day == 'fri': - schedule.every().friday.at(start_task).do(bot.send_message, id, text, thread_id) + schedule.every().friday.at(start_task).do(bot.send_message, id, text, thread_id, True) elif day == 'sat': - schedule.every().saturday.at(start_task).do(bot.send_message, id, text, thread_id) + schedule.every().saturday.at(start_task).do(bot.send_message, id, text, thread_id, True) return @@ -172,6 +173,21 @@ def groups_in_json(): return schdl.keys() +def students_in_group(group): + group = str(group) + + data = {} + with open(path_users, 'r', encoding='utf-8') as json_file: + data = json.load(json_file) + + students = [] + + for i in data.keys(): + if data[i]["group"] == group: + students.append(i) + + return students + def students_in_json(id="", key=""): id = str(id) @@ -199,7 +215,7 @@ def return_infos(id): data = {} with open(path_users, 'r', encoding='utf-8') as json_file: data = json.load(json_file) - data = data[id] + data = data.get(id) return data diff --git a/json/users_expl.json b/json/users_expl.json index 6957ce4..16f7c68 100644 --- a/json/users_expl.json +++ b/json/users_expl.json @@ -3,8 +3,8 @@ "username": "kr0sh_512", "first_name": "Дмитрий", "last_name": "", - "group": "102", - "timeout": "12", + "group": "117", + "timeout": "10", "allow_message": "yes", "thread": "General" } diff --git a/schedules.py b/schedules.py index 2a19ef0..c532d7c 100644 --- a/schedules.py +++ b/schedules.py @@ -8,8 +8,6 @@ import for_json from admin import admin_command, is_admin, send_admin_message, send_admin_document # bot = telebot.TeleBot("TOKEN_API") -bot = telebot.TeleBot("6240513112:AAFccyClvWsSNtVsZgNWAKFaNTs2-0y__pw") -# bot = telebot.TeleBot("6355753103:AAFPzya37HNjBEKnv83fqWbyXX6Bhe52DR4") # def log(func): # def wrapper(*args, **kwargs): @@ -42,8 +40,10 @@ def help(message): admin_help_msg = 'И команды только для админа:\ \n/restart - перезапуск бота.\ \n/update - обновление schedule задач\ + \n/stats - получание статистики\ \n/json - получить файл пользователей и расписания\ \n/info id_пользователя - узнать настройки пользователя\ + \n/spam - сделать рассылку\ \n/pause_all - приостановить бота для всех (каникулы/выходные)\ \n/stop id_пользователя - приостановить отправку сообщений для пользователя' send_admin_message(admin_help_msg) @@ -57,6 +57,7 @@ def help(message): # def update_schedules(message): # text = 'Жирный текст\ # \nКурсивный текст\ +# \nПодчёркнутый\ # \nПеречёркнутый текст\ # \nСсылка\ # \nМоноширинный текст\ @@ -79,7 +80,38 @@ def update_schedules(message): send_admin_message('Произошёл update') return - + +@bot.message_handler(commands=['stats']) +@admin_command +def send_stat(message): + text = "Статистика:\ + \n(группа: всего/откл. увед./беседы)" + + sum_users = 0 + sum_dis_not = 0 + sum_chats = 0 + + groups = list(for_json.groups_in_json()) + groups.append("other") + + for i in groups: + users = for_json.students_in_group(i) + sum_users += len(users) + + dis_not = len([i for i, x in enumerate(users) if for_json.return_infos(x)["allow_message"] == "no"]) + sum_dis_not += dis_not + + chats = len([i for i, x in enumerate(users) if x[0] == "-"]) + sum_chats += chats + + text += "\n{}: {} / {} / {}".format(i, len(users), dis_not, chats) + + text += "\n\nВсего: {} / {} / {}".format(sum_users, sum_dis_not, sum_chats) + + send_admin_message(text) + + return + @bot.message_handler(commands=['json']) @admin_command def send_json(message): @@ -94,6 +126,85 @@ def send_json(message): # info user_id +@bot.message_handler(commands=['spam']) +@admin_command +def spam(message): + send_admin_message("Пришли мне id, номер группы или all для рассылки сообщения этим пользователям") + + bot.register_next_step_handler(message, spam_msg) + + return + +def spam_msg(message): + if message.text == "stop" or message.text == "стоп" or message.text[0] == '/': + send_admin_message('Отмена отпраки') + return + + send_admin_message("Пришли мне текст для рассылки сообщения этим пользователям") + + bot.register_next_step_handler(message, spam_cnf, message.text) + + return + +def spam_cnf(message, data): + if message.text == "stop" or message.text == "стоп" or message.text[0] == '/': + send_admin_message('Отмена отпраки') + return + + text = "Пожалуйста, проверь итоговое сообщение и подтверди отправку:\ + \n\nКому: " + + if data == "all": + text += "всем" + elif data in for_json.groups_in_json(): + text += "{} группе".format(data) + elif for_json.return_infos(data) != None: + text += "пользователю @{}".format(for_json.return_infos(data)["username"]) + else: + send_admin_message("Ошибка при выборе пользователей") + return + + text +="\n\nСообщение: " + message.text + + markup = types.InlineKeyboardMarkup() + + markup.add(types.InlineKeyboardButton(text="✅", callback_data="spam#{}#{}".format(data, message.text)), + types.InlineKeyboardButton(text="❌", callback_data="spam_no")) + + bot.send_message(message.from_user.id, text, parse_mode=ParseMode.HTML, reply_markup=markup) + + return + +@bot.callback_query_handler(func=lambda call: "spam" in call.data) +def spam_send(call): + if call.data == "spam_no": + send_admin_message('Отмена отпраки') + return + + tmp, data, text = call.data.split("#") + + users = [] + + if data == "all": + groups = for_json.groups_in_json() + for group in groups: + users += for_json.students_in_group(group) + users += for_json.students_in_group("other") + elif data in for_json.groups_in_json(): + users = for_json.students_in_group(data) + elif for_json.return_infos(data) != None: + users = [data] + + for i in users: + send_message(i, text) + + bot.edit_message_text(chat_id=call.message.chat.id, + message_id=call.message.message_id, + text='Успешно!', + parse_mode=ParseMode.HTML) + + return + @bot.message_handler(commands=['pause_all']) @admin_command def pause_bot(message): @@ -255,7 +366,7 @@ def random_people(message): l = [] for i, val in enumerate(list_of_var): text += '\n{} {}'.format(list_of_num[i], val) - l.append(types.InlineKeyboardButton(text=list_of_num[i], callback_data=val)) + l.append(types.InlineKeyboardButton(text=list_of_num[i], callback_data="rand#" + val)) l = [l] @@ -264,14 +375,13 @@ def random_people(message): return -@bot.callback_query_handler(func=lambda call: not(call.data in ['left', 'right'] - or call.data in for_json.groups_in_json() or call.data == "other")) +@bot.callback_query_handler(func=lambda call: "rand#" in call.data) def make_random(call): - list_of_stud = for_json.students_in_json(call.message.chat.id, call.data) + list_of_stud = for_json.students_in_json(call.message.chat.id, call.data.split("#")[1]) random.shuffle(list_of_stud) - text = '{}:\n\n'.format(call.data) + text = '{}:\n\n'.format(call.data.split("#")[1]) for i, name in enumerate(list_of_stud): text += '{}{}. {}\n'.format(' ' if i + 1 < 10 else '', i + 1, name) @@ -413,7 +523,7 @@ def send_source(message): @bot.message_handler(content_types=['text']) def text_message(message): - if message.chat.type == "supergroup": + if message.chat.type == "supergroup" or is_admin(message): return send_message(message.from_user.id, 'К сожалению, я ещё не умею обрабатывать сообщения.\ \nИспользуй команды из меню или напиши /help.') @@ -422,10 +532,20 @@ def text_message(message): -def send_message(id, text, thread_id='General'): +def send_message(id, text, thread_id='General', distribution=False): if thread_id == 'General': thread_id = None + if distribution: + count_of_weeks = (datetime.now() - datetime(2024, 2, 5)).days // 7 + is_odd = (count_of_weeks + 1) % 2 == 1 + + if is_odd and ("чёт" in text) and ("нечёт" not in text): + return + + if (not is_odd) and ("нечёт" in text): + return + try: bot.send_message(chat_id=id, text=text, parse_mode=ParseMode.HTML, message_thread_id=thread_id,disable_web_page_preview=True) except Exception as e: @@ -433,7 +553,7 @@ def send_message(id, text, thread_id='General'): try: bot.send_message(chat_id=id, text=text, parse_mode=ParseMode.HTML, message_thread_id=thread_id,disable_web_page_preview=True) except Exception as e: - text_error = 'Error from user: {}\n{}'.format(id, str(e)) + text_error = 'Error from user: @{} {}\n{}'.format(for_json.return_infos(id)["username"], id, str(e)) send_admin_message(text_error) print(id, e)