diff --git a/.gitignore b/.gitignore
index 71e586a..40b2a5e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,3 +2,4 @@
users.json
schedule.json
settings.json
+json/students.json
diff --git a/__pycache__/admin.cpython-311.pyc b/__pycache__/admin.cpython-311.pyc
new file mode 100644
index 0000000..e9dd4ec
Binary files /dev/null and b/__pycache__/admin.cpython-311.pyc differ
diff --git a/__pycache__/for_json.cpython-311.pyc b/__pycache__/for_json.cpython-311.pyc
new file mode 100644
index 0000000..b294c17
Binary files /dev/null and b/__pycache__/for_json.cpython-311.pyc differ
diff --git a/__pycache__/schedules.cpython-311.pyc b/__pycache__/schedules.cpython-311.pyc
new file mode 100644
index 0000000..afed869
Binary files /dev/null and b/__pycache__/schedules.cpython-311.pyc differ
diff --git a/for_json.py b/for_json.py
index 03c3e5f..965147f 100644
--- a/for_json.py
+++ b/for_json.py
@@ -5,9 +5,18 @@ from datetime import datetime
path_users = "json/users.json" # Нужный путь до json файлов
path_schedule = "json/schedule.json"
+path_students = "json/students.json"
+# path_logs = "json/logs.json"
allow_update = True
+# Возможно разбиение добавление логов на 2 функции: Распарсирование message/call
+# 2) добавление самого текста в json
+
+# def add_logs(logs): # TODO: реализация добавления логов
+# pass
+# return
+
def save_user(infos):
for i in range(len(infos)):
if type(infos[i]) == type(None):
@@ -39,6 +48,15 @@ def save_user(infos):
with open(path_users, 'w', encoding='utf-8') as json_file:
json.dump(data, json_file, ensure_ascii=False, indent=4)
create_schedule_tasks()
+ bot.send_admin_message("Новый пользователь:\
+ \nuser: @{}\
+ \ngroup: {}\
+ \nid: {}\
+ \nname: {}".format(data[infos["id"]]["username"],
+ data[infos["id"]]["group"],
+ infos["id"],
+ data[infos["id"]]["first_name"] + data[infos["id"]]["last_name"]))
+
return
def change_user_param(id, key, value):
@@ -52,6 +70,11 @@ def change_user_param(id, key, value):
with open(path_users, 'w', encoding='utf-8') as json_file:
json.dump(data, json_file, ensure_ascii=False, indent=4)
create_schedule_tasks()
+ bot.send_admin_message("Изменения:\
+ \nuser: @{}\
+ \nid: {}\
+ \nkey: {}\
+ \nvalue: {}".format(data[id]["username"], id, key, value))
return
def parse_lesson(time, lesson):
@@ -149,6 +172,28 @@ def groups_in_json():
return schdl.keys()
+def students_in_json(id="", key=""):
+ id = str(id)
+
+ group = {}
+ with open(path_users, 'r', encoding='utf-8') as json_file:
+ group = json.load(json_file)
+ group = group[id]["group"]
+
+ stud = {}
+ with open(path_students, 'r', encoding='utf-8') as json_file:
+ stud = json.load(json_file)
+ stud = stud.get(group)
+
+ if stud == None:
+ return None
+
+ if key == "":
+ return list(stud.keys())
+
+ return stud[key]
+
+
def return_infos(id):
id = str(id)
data = {}
@@ -159,9 +204,15 @@ def return_infos(id):
return data
def pause_bot():
- schedule.clear()
global allow_update
- allow_update = False
+ if allow_update:
+ schedule.clear()
+ allow_update = False
+ bot.send_admin_message('Бот больше не отправляет расписание')
+ else:
+ create_schedule_tasks(True)
+ allow_update = True
+ bot.send_admin_message('Бот возобновил рассылку!')
return
diff --git a/json/logs.json b/json/logs.json
new file mode 100644
index 0000000..56bc9fa
--- /dev/null
+++ b/json/logs.json
@@ -0,0 +1,6 @@
+{
+ "time": {
+ "func": "",
+ "params": ""
+ }
+}
\ No newline at end of file
diff --git a/schedules.py b/schedules.py
index c77a709..2a19ef0 100644
--- a/schedules.py
+++ b/schedules.py
@@ -1,5 +1,5 @@
#!/usr/bin/python3.3
-import threading, telebot, schedule, time
+import threading, telebot, schedule, time, random
from datetime import datetime
import os, sys
from telebot import types
@@ -7,15 +7,27 @@ from telegram.constants import ParseMode
import for_json
from admin import admin_command, is_admin, send_admin_message, send_admin_document
-bot = telebot.TeleBot("TOKEN_API")
+# bot = telebot.TeleBot("TOKEN_API")
+bot = telebot.TeleBot("6240513112:AAFccyClvWsSNtVsZgNWAKFaNTs2-0y__pw")
+# bot = telebot.TeleBot("6355753103:AAFPzya37HNjBEKnv83fqWbyXX6Bhe52DR4")
+# def log(func):
+# def wrapper(*args, **kwargs):
+# message = args[0]
+
+# for_json.add_log(message)
+# func(*args, **kwargs)
+# return
+
+# return wrapper
@bot.message_handler(commands=['help', 'faq'])
def help(message):
help_msg = 'Мои команды:\
\n/start - используй, чтобы сменить номер группы.\
\n/schedule - используй, чтобы получить расписание на сегодня.\
+ \n/random - сделай очередь из людей\
\n/info - используй, чтобы узнать твои настройки бота\
\n/pause - используй, чтобы прекратить получать сообщения от бота\
\n/thread - используй в нужном чате канала, чтобы бот отправлял сообщения именно туда\
@@ -40,19 +52,19 @@ def help(message):
### --//--
-@bot.message_handler(commands=['test'])
-@admin_command
-def update_schedules(message):
- text = 'Жирный текст\
- \nКурсивный текст\
- \nПеречёркнутый текст\
- \nСсылка\
- \nМоноширинный текст\
- \n
Форматированный с сохранением пробелов\ - \n
Цитата' - send_admin_message(text) +# @bot.message_handler(commands=['test']) +# @admin_command +# def update_schedules(message): +# text = 'Жирный текст\ +# \nКурсивный текст\ +# \n
Моноширинный текст\
+# \nФорматированный с сохранением пробелов\ +# \n
Цитата' +# send_admin_message(text) - return +# return @bot.message_handler(commands=['restart']) @admin_command @@ -75,6 +87,8 @@ def send_json(message): send_admin_document(json_file) with open(for_json.path_schedule, 'rb') as json_file: send_admin_document(json_file) + with open(for_json.path_students, 'rb') as json_file: + send_admin_document(json_file) return @@ -84,8 +98,7 @@ def send_json(message): @admin_command def pause_bot(message): for_json.pause_bot() - send_admin_message('Бот больше не отправляет расписание') - + return @bot.message_handler(commands=['stop']) @@ -145,12 +158,13 @@ def start(message): markup = types.InlineKeyboardMarkup() for i in for_json.groups_in_json(): markup.add(types.InlineKeyboardButton(text=i, callback_data=i)) + markup.add(types.InlineKeyboardButton(text="Моей группы нет в этом списке", callback_data="other")) bot.send_message(message.chat.id, 'Пожалуйста, выбери свою группу', parse_mode=ParseMode.HTML, reply_markup=markup) return -@bot.callback_query_handler(func=lambda call: not(call.data in ['left', 'right'])) +@bot.callback_query_handler(func=lambda call: call.data in for_json.groups_in_json() or call.data == "other") def callback_inline(call): infos = [ call.from_user.id, @@ -221,6 +235,55 @@ def change_schedule(call): +@bot.message_handler(commands=['random']) +def random_people(message): + list_of_num = ["1️⃣", "2️⃣", "3️⃣", "4️⃣", "5️⃣", "6", "7", "8", "9"] + + list_of_var = for_json.students_in_json(message.chat.id) + + text = 'Для вашей группы эта команда не настроена.\n\nНапиши запрос через /request' + + if list_of_var == None: + send_message(message.chat.id, text) + send_admin_message('Запрос на random команду от {}'.format( + for_json.return_infos(message.chat.id)["group"])) + + return + + text = '• Выбери как именно мне перемешать вас:\n' + + 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 = [l] + + markup = types.InlineKeyboardMarkup(l) + bot.send_message(message.chat.id, text, ParseMode.HTML, reply_markup=markup) + + 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")) +def make_random(call): + list_of_stud = for_json.students_in_json(call.message.chat.id, call.data) + + random.shuffle(list_of_stud) + + text = '{}:\n\n'.format(call.data) + + for i, name in enumerate(list_of_stud): + text += '
{}{}. {}\n'.format(' ' if i + 1 < 10 else '', i + 1, name)
+
+ bot.edit_message_text('Хорошо, вот ваше распределение!', call.message.chat.id,
+ call.message.message_id, parse_mode=ParseMode.HTML)
+
+ send_message(call.message.chat.id, text)
+
+ return
+
+
@bot.message_handler(commands=['info'])
def send_info(message):
if len(message.text.split(' ')) == 2 and is_admin(message):
@@ -393,7 +456,7 @@ def send_document(id, file, text = ''):
if __name__ == '__main__':
- for_json.create_schedule_tasks()
+ for_json.create_schedule_tasks(True)
send_admin_message('Я перезапустился!')
print("-------------------------")