bot/bot/iternal.py
2026-08-30 13:09:59 +00:00

83 lines
2.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import json
from telebot.util import quick_markup
from telebot.types import Message, CallbackQuery
import config
from models.user import User, validate, getAllUsers
from models.bot import Markup, bot, send
from models.schedule import generateDaySchedule
REMEMBER_MESSAGE_DATA = False
REMEMBER_JSON_DATA: dict | bool = False
REMEMBER_PHOTO_ID: str | bool = False
def downloadFile(out):
global REMEMBER_JSON_DATA
if REMEMBER_JSON_DATA: return REMEMBER_JSON_DATA
file_info = bot.get_file(out.document.file_id) # type: ignore
downloaded_file = bot.download_file(file_info.file_path) # type: ignore
with open(f'./temp/senf_all.json', 'wb') as new_file:
new_file.write(downloaded_file)
REMEMBER_JSON_DATA = json.load(open(f'./temp/senf_all.json', 'r', encoding='utf-8'))
return REMEMBER_JSON_DATA
def sendQueue(uid: int):
if not REMEMBER_PHOTO_ID:
send(
uid,
REMEMBER_JSON_DATA['text'],
quick_markup(REMEMBER_JSON_DATA['quick_markup'], REMEMBER_JSON_DATA['row_width']) if REMEMBER_JSON_DATA['quick_markup'] else None,
disable_web_page_preview=REMEMBER_JSON_DATA.get('hide_url', True)
)
else:
bot.send_photo(
uid, REMEMBER_PHOTO_ID,
caption=REMEMBER_JSON_DATA['text'],
reply_markup=quick_markup(REMEMBER_JSON_DATA['quick_markup'], REMEMBER_JSON_DATA['row_width']) if REMEMBER_JSON_DATA['quick_markup'] else None,
)
def sendConfirm(message: Message):
return send(message, '<b>Вы действительно хотите отправить сообщение всем?</b>', quick_markup({
'Нет': {'callback_data': 'ignore'},
'Да': {'callback_data': '$sendall'},
'Нeт': {'callback_data': 'ignore'}
}, row_width=1))
@bot.message_handler(
content_types=['document', 'file'],
func=lambda message: message.from_user.id in config.ADMINS
and bool(message.document)
and (message.document.file_name or '').lower().endswith('.json'),
)
def massSendHandle(message: Message):
global REMEMBER_MESSAGE_DATA, REMEMBER_JSON_DATA, REMEMBER_PHOTO_ID
REMEMBER_PHOTO_ID = False
REMEMBER_JSON_DATA = False
downloadFile(message)
sendQueue(message.from_user.id)
sendConfirm(message)
@bot.message_handler(content_types = ['photo'], func = lambda m: m.from_user.id in config.ADMINS)
def massPhotoHandle(message: Message):
global REMEMBER_PHOTO_ID
REMEMBER_PHOTO_ID = message.photo[-1].file_id
sendQueue(message.from_user.id)
sendConfirm(message)
@bot.callback_query_handler(func=lambda call: '$sendall' == call.data)
def sendAllCallback(call: CallbackQuery):
if call.from_user.id not in config.ADMINS: return # type: ignore
for u in getAllUsers():
if u.id in REMEMBER_JSON_DATA.get('except', []): continue
try: sendQueue(u.id)
except: pass