Skip to content

Commit

Permalink
правки
Browse files Browse the repository at this point in the history
  • Loading branch information
HamletSargsyan committed Aug 28, 2024
1 parent 20f4dd5 commit 530d243
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 25 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Добавлен скрипт `tools/create_config.py` для генерации конфига бота
- Новые страницы в документации для разработчиков
- Логирование исключений которые возникают в `telebot` с использованием класса `ExceptionHandler`.

### Изменено

Expand Down
62 changes: 37 additions & 25 deletions src/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@
from semver import Version

import telebot
from telebot import ExceptionHandler
from telebot.storage import StateRedisStorage
from telebot.custom_filters import StateFilter, IsDigitFilter


TELEGRAM_ID: Final = 777000


@dataclass
class GeneralConfig:
debug: bool = False
Expand Down Expand Up @@ -102,31 +106,6 @@ def from_toml(file_path: str) -> "Config":
version: Final = Version.parse(f.read())


class RedisStorage(StateRedisStorage):
def set_record(self, key, value):
connection = Redis(connection_pool=self.redis)
connection.setex(self.prefix + str(key), 120, json.dumps(value))
connection.close()
return True


state_storage = RedisStorage(redis_url=config.redis.url)

bot = telebot.TeleBot(
config.telegram.token,
parse_mode="html",
skip_pending=True,
num_threads=10,
disable_web_page_preview=True,
use_class_middlewares=True,
state_storage=state_storage,
)

bot.add_custom_filter(StateFilter(bot))
bot.add_custom_filter(IsDigitFilter())

TELEGRAM_ID: Final = 777000

logger = logging.Logger("Bot")


Expand All @@ -152,3 +131,36 @@ def emit(self, record):

telebot.logger.addHandler(TelegramLogsHandler())
telebot.logger.setLevel(20)


class RedisStorage(StateRedisStorage):
def set_record(self, key, value):
connection = Redis(connection_pool=self.redis)
connection.setex(self.prefix + str(key), 120, json.dumps(value))
connection.close()
return True


state_storage = RedisStorage(redis_url=config.redis.url)


class ErrorHandler(ExceptionHandler):
def handle(self, exception: Exception):
logger.exception(exception)
return True


bot = telebot.TeleBot(
config.telegram.token,
parse_mode="html",
skip_pending=True,
num_threads=10,
disable_web_page_preview=True,
use_class_middlewares=True,
state_storage=state_storage,
exception_handler=ErrorHandler(),
)


bot.add_custom_filter(StateFilter(bot))
bot.add_custom_filter(IsDigitFilter())

0 comments on commit 530d243

Please sign in to comment.