add random function

This commit is contained in:
kr0sh512
2023-12-23 20:02:42 +03:00
parent 6f53e4edc8
commit b9b6ec13c5
7 changed files with 141 additions and 20 deletions
+1
View File
@@ -2,3 +2,4 @@
users.json
schedule.json
settings.json
json/students.json
Binary file not shown.
Binary file not shown.
Binary file not shown.
+52 -1
View File
@@ -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("Новый пользователь:\
\n<i>user:</i> @{}\
\n<i>group:</i> <code>{}</code>\
\n<i>id:</i> <code>{}</code>\
\n<i>name:</i> <code>{}</code>".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("Изменения:\
\n<i>user:</i> @{}\
\n<i>id:</i> <code>{}</code>\
\n<i>key:</i> <code>{}</code>\
\n<i>value:</i> <code>{}</code>".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
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
+6
View File
@@ -0,0 +1,6 @@
{
"time": {
"func": "",
"params": ""
}
}
+80 -17
View File
@@ -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 = '<b>Жирный текст</b>\
\n<i>Курсивный текст</i>\
\n<s>Перечёркнутый текст</s>\
\n<a href="google.com">Ссылка</a>\
\n<code>Моноширинный текст</code>\
\n<pre>Форматированный с сохранением пробелов</pre>\
\n<blockquote>Цитата</blockquote>'
send_admin_message(text)
# @bot.message_handler(commands=['test'])
# @admin_command
# def update_schedules(message):
# text = '<b>Жирный текст</b>\
# \n<i>Курсивный текст</i>\
# \n<s>Перечёркнутый текст</s>\
# \n<a href="google.com">Ссылка</a>\
# \n<code>Моноширинный текст</code>\
# \n<pre>Форматированный с сохранением пробелов</pre>\
# \n<blockquote>Цитата</blockquote>'
# 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,7 +98,6 @@ def send_json(message):
@admin_command
def pause_bot(message):
for_json.pause_bot()
send_admin_message('Бот больше не отправляет расписание')
return
@@ -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 = '<b><u>{}</u></b>:\n\n'.format(call.data)
for i, name in enumerate(list_of_stud):
text += '<code>{}{}</code>. {}\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("-------------------------")