bot/models/bot.py
2026-08-30 13:48:59 +03:00

191 lines
No EOL
6.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
import random
import telebot
import datetime
from telebot.types import ReplyKeyboardMarkup, InlineQueryResultArticle, InputTextMessageContent, Message, ReplyKeyboardRemove, WebAppInfo, KeyboardButton
from telebot import apihelper
import config
import utils.schedule_client as sc
if config.TELEGRAM_PROXY:
apihelper.proxy = {
'http': config.TELEGRAM_PROXY,
'https': config.TELEGRAM_PROXY
}
bot = telebot.TeleBot(config.TOKEN, parse_mode='html')
from . import logger, user
def send(target: Message | int | str, text: str, reply_markup = None, disable_web_page_preview: bool = True, **kwargs) -> Message: # type: ignore
try:
_ = target.from_user.id # type: ignore
try: return bot.reply_to(target, text, reply_markup = reply_markup, disable_web_page_preview = True, **kwargs) # type: ignore
except Exception as e: logger.error('Bot', f'{e}')
except:
try: return bot.send_message(target, text, reply_markup = reply_markup, disable_web_page_preview = True, **kwargs) # type: ignore
except Exception as e: logger.error('Bot', f'{e}')
class Inlines:
def __init__(self, id: str, cache: int = 60) -> None:
self.list = []
self.cache = cache
self.id = id
def add(self, title: str, description: str, output: str) -> None:
self.list.append(
InlineQueryResultArticle(
id = str(random.randint(999,99999)),
title = title,
input_message_content = InputTextMessageContent(
message_text = output,
parse_mode = 'html',
disable_web_page_preview = True
),
description = description
)
)
def send(self) -> bool:
return bot.answer_inline_query(
self.id,
self.list,
self.cache,
is_personal = True
)
class Strings:
back_to_home = 'На главную 🏠'
back_to_search = 'К поиску 🔎'
# home
map = 'Карта 🗺️'
schedule = 'Расписание 📅'
search = 'Поиск 🔎'
settings = 'Настройки ⚙️'
support = 'Поддержать 💞'
current_schedule_name_F = '{} 🔄'
# register
register_not_found_T = 'Моего {} нету ⚠'
# schedule
today = 'Сегодня 🔥'
tomorrow = 'Завтра 🍀'
weekdays: list[str] = ['ПН', 'ВТ', 'СР', 'ЧТ', 'ПТ']
even = 'Четная 📅'
odd = 'Нечетная 📅'
delete = 'Удалить 🗑'
save = 'Сохранить 🔆'
@staticmethod
def format(string: str, *args, **kwargs) -> str: return string.format(*args, **kwargs)
class Markup_v2:
@staticmethod
def remove() -> ReplyKeyboardRemove:
return ReplyKeyboardRemove()
@staticmethod
def default(user: user.User) -> ReplyKeyboardMarkup:
markup = ReplyKeyboardMarkup(resize_keyboard=True)
markup.add(
KeyboardButton(
text=Strings.map,
web_app=WebAppInfo(f'{config.WEB_BASE_URL}/map/?v13')
),
Strings.schedule,
row_width=2
)
markup.add(Strings.search, Strings.current_schedule_name_F.format(user.group), row_width=2)
markup.add(Strings.settings, Strings.support, row_width=2)
return markup
@staticmethod
def schedule(is_added: bool, from_search: bool = False) -> ReplyKeyboardMarkup:
odd_even = datetime.datetime.now().isocalendar().week%2 == 0 #! ??????
markup = ReplyKeyboardMarkup(resize_keyboard=True)
markup.add(Strings.back_to_home, row_width=1) if not from_search else markup.add(Strings.back_to_search,Strings.back_to_home, row_width=2)
markup.add(Strings.today, Strings.tomorrow, row_width=2)
markup.add(*Strings.weekdays, row_width=5)
markup.add(f'{"" if odd_even else ""} {Strings.odd}', f'{"" if odd_even else ""} {Strings.even}', row_width=2)
markup.add(Strings.delete if is_added else Strings.save, row_width=1)
return markup
class Markup_v1:
@staticmethod
def remove() -> ReplyKeyboardRemove:
return ReplyKeyboardRemove()
@staticmethod
def default(user: user.User) -> ReplyKeyboardMarkup:
markup = ReplyKeyboardMarkup(resize_keyboard=True)
markup.add(
KeyboardButton(
text='Карта 🗺️',
web_app=WebAppInfo(f'{config.WEB_BASE_URL}/map/?v12')
), row_width=1
)
markup.add('Сегодня 🔥', 'Завтра 🍀', row_width=2)
markup.add('На неделю 📅', 'Другие дни 📁', row_width=2)
# markup.add('Уведомления ⏰', 'Помощь 🆘', row_width=2)
markup.add('Уведомления ⏰', 'Поддержать 💞', f'{user.group} 🔄', row_width=3)
return markup
@staticmethod
def other() -> ReplyKeyboardMarkup:
markup = ReplyKeyboardMarkup(resize_keyboard=True)
markup.add('На главную 🏠', row_width=1)
markup.add('ПН', 'ВТ', 'СР', 'ЧТ', 'ПТ', row_width=5)
markup.add('Нечетная 📅', 'Четная 📅', row_width=2)
# markup.add('ЧТ', 'ПТ', row_width=2)
return markup
@staticmethod
def notifications() -> ReplyKeyboardMarkup:
markup = ReplyKeyboardMarkup(resize_keyboard=True)
markup.add('На главную 🏠', row_width=1)
markup.add('Перед парой ⌛', row_width=1)
markup.add('Вечером 🌆', row_width=1)
markup.add('Файлом на неделю 📅', row_width=1)
return markup
@staticmethod
def saved_groups(u: user.User) -> ReplyKeyboardMarkup:
markup = ReplyKeyboardMarkup(resize_keyboard=True)
markup.add('На главную 🏠', row_width=1)
markup.add(*u.saved_groups, row_width=4)
markup.add('Добавить группу 🔆', 'Удалить группы 🗑', row_width=2)
return markup
@staticmethod
def selectGroup(query: str) -> ReplyKeyboardMarkup:
#EDIT
schedule = sc.ScheduleDB(api_url=config.SCHEDULE_BASE_URL)
groupList = schedule.query(sc.Group).filter_by(name=query)
markup = ReplyKeyboardMarkup(resize_keyboard=True)
for group in groupList:
markup.add(group, row_width=1)
markup.add('Моей группы нет ⚠', row_width=1)
return markup
def Receive(state: str, text: str | None = None, strict: bool = True):
return bot.message_handler(
content_types = ['text'],
func = lambda m: user.validate(m).state == state and (
(m.text in text if not strict else m.text == text) if text != None else True
)
)
def Default(text: str, strict: bool = True):
return Receive('default', text, strict)