Archived
some updates
This commit is contained in:
@@ -3,3 +3,4 @@ users.json
|
|||||||
schedule.json
|
schedule.json
|
||||||
settings.json
|
settings.json
|
||||||
json/students.json
|
json/students.json
|
||||||
|
json/schedule_2sem.json
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -24,5 +24,5 @@ def send_admin_message(text):
|
|||||||
bot.send_message(admin_id, '🛑 ' + text)
|
bot.send_message(admin_id, '🛑 ' + text)
|
||||||
|
|
||||||
def send_admin_document(file):
|
def send_admin_document(file):
|
||||||
bot.send_document(admin_id, file, '🛑 Admin file')
|
bot.send_document(admin_id, file, '🛑 Admin file')
|
||||||
|
|
||||||
+26
-10
@@ -4,7 +4,7 @@ import schedules as bot
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
path_users = "json/users.json" # Нужный путь до json файлов
|
path_users = "json/users.json" # Нужный путь до json файлов
|
||||||
path_schedule = "json/schedule.json"
|
path_schedule = "json/schedule_2sem.json"
|
||||||
path_students = "json/students.json"
|
path_students = "json/students.json"
|
||||||
# path_logs = "json/logs.json"
|
# path_logs = "json/logs.json"
|
||||||
|
|
||||||
@@ -52,10 +52,11 @@ def save_user(infos):
|
|||||||
\n<i>user:</i> @{}\
|
\n<i>user:</i> @{}\
|
||||||
\n<i>group:</i> <code>{}</code>\
|
\n<i>group:</i> <code>{}</code>\
|
||||||
\n<i>id:</i> <code>{}</code>\
|
\n<i>id:</i> <code>{}</code>\
|
||||||
\n<i>name:</i> <code>{}</code>".format(data[infos["id"]]["username"],
|
\n<i>name:</i> <code>{} {}</code>".format(data[infos["id"]]["username"],
|
||||||
data[infos["id"]]["group"],
|
data[infos["id"]]["group"],
|
||||||
infos["id"],
|
infos["id"],
|
||||||
data[infos["id"]]["first_name"] + data[infos["id"]]["last_name"]))
|
data[infos["id"]]["first_name"],
|
||||||
|
data[infos["id"]]["last_name"]))
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|
||||||
@@ -151,17 +152,17 @@ def create_schedule_tasks(manual=False):
|
|||||||
start_task = tmp[:-1]
|
start_task = tmp[:-1]
|
||||||
|
|
||||||
if day == 'mon':
|
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':
|
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':
|
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':
|
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':
|
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':
|
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
|
return
|
||||||
|
|
||||||
@@ -172,6 +173,21 @@ def groups_in_json():
|
|||||||
|
|
||||||
return schdl.keys()
|
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=""):
|
def students_in_json(id="", key=""):
|
||||||
id = str(id)
|
id = str(id)
|
||||||
|
|
||||||
@@ -199,7 +215,7 @@ def return_infos(id):
|
|||||||
data = {}
|
data = {}
|
||||||
with open(path_users, 'r', encoding='utf-8') as json_file:
|
with open(path_users, 'r', encoding='utf-8') as json_file:
|
||||||
data = json.load(json_file)
|
data = json.load(json_file)
|
||||||
data = data[id]
|
data = data.get(id)
|
||||||
|
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|||||||
@@ -3,8 +3,8 @@
|
|||||||
"username": "kr0sh_512",
|
"username": "kr0sh_512",
|
||||||
"first_name": "Дмитрий",
|
"first_name": "Дмитрий",
|
||||||
"last_name": "",
|
"last_name": "",
|
||||||
"group": "102",
|
"group": "117",
|
||||||
"timeout": "12",
|
"timeout": "10",
|
||||||
"allow_message": "yes",
|
"allow_message": "yes",
|
||||||
"thread": "General"
|
"thread": "General"
|
||||||
}
|
}
|
||||||
|
|||||||
+131
-11
@@ -8,8 +8,6 @@ import for_json
|
|||||||
from admin import admin_command, is_admin, send_admin_message, send_admin_document
|
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 log(func):
|
||||||
# def wrapper(*args, **kwargs):
|
# def wrapper(*args, **kwargs):
|
||||||
@@ -42,8 +40,10 @@ def help(message):
|
|||||||
admin_help_msg = 'И команды только для админа:\
|
admin_help_msg = 'И команды только для админа:\
|
||||||
\n/restart - перезапуск бота.\
|
\n/restart - перезапуск бота.\
|
||||||
\n/update - обновление schedule задач\
|
\n/update - обновление schedule задач\
|
||||||
|
\n/stats - получание статистики\
|
||||||
\n/json - получить файл пользователей и расписания\
|
\n/json - получить файл пользователей и расписания\
|
||||||
\n/info <i>id_пользователя</i> - узнать настройки пользователя\
|
\n/info <i>id_пользователя</i> - узнать настройки пользователя\
|
||||||
|
\n/spam - сделать рассылку\
|
||||||
\n/pause_all - приостановить бота для всех (каникулы/выходные)\
|
\n/pause_all - приостановить бота для всех (каникулы/выходные)\
|
||||||
\n/stop <i>id_пользователя</i> - приостановить отправку сообщений для пользователя'
|
\n/stop <i>id_пользователя</i> - приостановить отправку сообщений для пользователя'
|
||||||
send_admin_message(admin_help_msg)
|
send_admin_message(admin_help_msg)
|
||||||
@@ -57,6 +57,7 @@ def help(message):
|
|||||||
# def update_schedules(message):
|
# def update_schedules(message):
|
||||||
# text = '<b>Жирный текст</b>\
|
# text = '<b>Жирный текст</b>\
|
||||||
# \n<i>Курсивный текст</i>\
|
# \n<i>Курсивный текст</i>\
|
||||||
|
# \n<u>Подчёркнутый</u>\
|
||||||
# \n<s>Перечёркнутый текст</s>\
|
# \n<s>Перечёркнутый текст</s>\
|
||||||
# \n<a href="google.com">Ссылка</a>\
|
# \n<a href="google.com">Ссылка</a>\
|
||||||
# \n<code>Моноширинный текст</code>\
|
# \n<code>Моноширинный текст</code>\
|
||||||
@@ -79,7 +80,38 @@ def update_schedules(message):
|
|||||||
send_admin_message('Произошёл update')
|
send_admin_message('Произошёл update')
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|
||||||
|
@bot.message_handler(commands=['stats'])
|
||||||
|
@admin_command
|
||||||
|
def send_stat(message):
|
||||||
|
text = "<u>Статистика:</u>\
|
||||||
|
\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'])
|
@bot.message_handler(commands=['json'])
|
||||||
@admin_command
|
@admin_command
|
||||||
def send_json(message):
|
def send_json(message):
|
||||||
@@ -94,6 +126,85 @@ def send_json(message):
|
|||||||
|
|
||||||
# info user_id
|
# info user_id
|
||||||
|
|
||||||
|
@bot.message_handler(commands=['spam'])
|
||||||
|
@admin_command
|
||||||
|
def spam(message):
|
||||||
|
send_admin_message("Пришли мне id, номер группы или <i>all</i> для рассылки сообщения этим пользователям")
|
||||||
|
|
||||||
|
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 += "<u>всем</u>"
|
||||||
|
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'])
|
@bot.message_handler(commands=['pause_all'])
|
||||||
@admin_command
|
@admin_command
|
||||||
def pause_bot(message):
|
def pause_bot(message):
|
||||||
@@ -255,7 +366,7 @@ def random_people(message):
|
|||||||
l = []
|
l = []
|
||||||
for i, val in enumerate(list_of_var):
|
for i, val in enumerate(list_of_var):
|
||||||
text += '\n{} {}'.format(list_of_num[i], val)
|
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]
|
l = [l]
|
||||||
|
|
||||||
@@ -264,14 +375,13 @@ def random_people(message):
|
|||||||
|
|
||||||
return
|
return
|
||||||
|
|
||||||
@bot.callback_query_handler(func=lambda call: not(call.data in ['left', 'right']
|
@bot.callback_query_handler(func=lambda call: "rand#" in call.data)
|
||||||
or call.data in for_json.groups_in_json() or call.data == "other"))
|
|
||||||
def make_random(call):
|
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)
|
random.shuffle(list_of_stud)
|
||||||
|
|
||||||
text = '<b><u>{}</u></b>:\n\n'.format(call.data)
|
text = '<b><u>{}</u></b>:\n\n'.format(call.data.split("#")[1])
|
||||||
|
|
||||||
for i, name in enumerate(list_of_stud):
|
for i, name in enumerate(list_of_stud):
|
||||||
text += '<code>{}{}</code>. {}\n'.format(' ' if i + 1 < 10 else '', i + 1, name)
|
text += '<code>{}{}</code>. {}\n'.format(' ' if i + 1 < 10 else '', i + 1, name)
|
||||||
@@ -413,7 +523,7 @@ def send_source(message):
|
|||||||
|
|
||||||
@bot.message_handler(content_types=['text'])
|
@bot.message_handler(content_types=['text'])
|
||||||
def text_message(message):
|
def text_message(message):
|
||||||
if message.chat.type == "supergroup":
|
if message.chat.type == "supergroup" or is_admin(message):
|
||||||
return
|
return
|
||||||
send_message(message.from_user.id, 'К сожалению, я ещё не умею обрабатывать сообщения.\
|
send_message(message.from_user.id, 'К сожалению, я ещё не умею обрабатывать сообщения.\
|
||||||
\nИспользуй команды из меню или напиши /help.')
|
\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':
|
if thread_id == 'General':
|
||||||
thread_id = None
|
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:
|
try:
|
||||||
bot.send_message(chat_id=id, text=text, parse_mode=ParseMode.HTML, message_thread_id=thread_id,disable_web_page_preview=True)
|
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:
|
except Exception as e:
|
||||||
@@ -433,7 +553,7 @@ def send_message(id, text, thread_id='General'):
|
|||||||
try:
|
try:
|
||||||
bot.send_message(chat_id=id, text=text, parse_mode=ParseMode.HTML, message_thread_id=thread_id,disable_web_page_preview=True)
|
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:
|
except Exception as e:
|
||||||
text_error = 'Error from user: <code>{}</code>\n{}'.format(id, str(e))
|
text_error = 'Error from user: @{} <code>{}</code>\n{}'.format(for_json.return_infos(id)["username"], id, str(e))
|
||||||
send_admin_message(text_error)
|
send_admin_message(text_error)
|
||||||
print(id, e)
|
print(id, e)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user