-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(tg_bot): reorganize bot structure
- Loading branch information
1 parent
9a52a15
commit c02050b
Showing
27 changed files
with
139 additions
and
90 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,9 @@ | ||
#!/bin/sh | ||
|
||
if [ "$DEV_MODE" = "False" ]; then | ||
echo "RELEASE MODE" | ||
else | ||
echo "DEBUG MODE" | ||
fi | ||
|
||
python main.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .main import run |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .main import register_all_handlers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .main import register_business_handlers |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from aiogram import Dispatcher | ||
|
||
from . import geography_coordinates, micrometer, middle_values, nomenclature_title | ||
|
||
|
||
def register_business_handlers(dp: Dispatcher) -> None: | ||
dp.include_routers( | ||
geography_coordinates.router, | ||
micrometer.router, | ||
middle_values.router, | ||
nomenclature_title.router, | ||
) |
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .main import register_callback_handlers |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from aiogram import Dispatcher | ||
|
||
from . import help | ||
|
||
|
||
def register_callback_handlers(dp: Dispatcher) -> None: | ||
dp.include_routers(help.router) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .main import register_utils_handlers |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from aiogram import Dispatcher | ||
|
||
from . import dev_handlers, util_handlers | ||
|
||
|
||
def register_utils_handlers(dp: Dispatcher) -> None: | ||
dp.include_routers( | ||
util_handlers.router, | ||
dev_handlers.router, | ||
) |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from aiogram import Dispatcher | ||
|
||
from . import business, callbacks, dev | ||
|
||
|
||
def register_all_handlers(dp: Dispatcher, dev_mode: bool) -> None: | ||
if dev_mode: | ||
dev.register_utils_handlers(dp) | ||
business.register_business_handlers(dp) | ||
callbacks.register_callback_handlers(dp) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from aiogram import Bot, Dispatcher | ||
from container import AppContainer | ||
from dependency_injector.wiring import Provide, inject | ||
|
||
from . import commands, handlers, middlewares | ||
|
||
|
||
@inject | ||
async def run( | ||
bot_token: str = Provide[AppContainer.settings.bot_token], | ||
dev_mode: bool = Provide[AppContainer.settings.dev_mode], | ||
admin_id: bool = Provide[AppContainer.settings.admin_id], | ||
) -> None: | ||
bot = Bot(bot_token, parse_mode="HTML") | ||
dp = Dispatcher() | ||
|
||
await commands.set_commands(bot=bot, dev_mode=dev_mode, admin_id=admin_id) | ||
handlers.register_all_handlers(dp, dev_mode) | ||
middlewares.register_all_middlewares(dp, dev_mode, admin_id) | ||
|
||
await dp.start_polling(bot) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .main import register_all_middlewares |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from typing import Any, Awaitable, Callable, Dict | ||
|
||
import structlog | ||
from aiogram import BaseMiddleware | ||
from aiogram.types import TelegramObject | ||
|
||
logger = structlog.get_logger() | ||
|
||
|
||
class BlockNonAdminMiddleware(BaseMiddleware): | ||
def __init__(self, admin_id: int) -> None: | ||
self.admin_id = admin_id | ||
|
||
def _is_admin( | ||
self, | ||
user_id: int, | ||
) -> bool: | ||
if user_id == self.admin_id: | ||
return True | ||
return False | ||
|
||
async def __call__( | ||
self, | ||
handler: Callable[[TelegramObject, Dict[str, Any]], Awaitable[Any]], | ||
event: TelegramObject, | ||
data: Dict[str, Any], | ||
) -> Any: | ||
if self._is_admin(event.chat.id): # type: ignore | ||
return await handler(event, data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from typing import Any, Awaitable, Callable, Dict | ||
|
||
import structlog | ||
from aiogram import BaseMiddleware | ||
from aiogram.types import TelegramObject | ||
|
||
logger = structlog.get_logger() | ||
|
||
|
||
class LogCommandsMiddleware(BaseMiddleware): | ||
async def __call__( | ||
self, | ||
handler: Callable[[TelegramObject, Dict[str, Any]], Awaitable[Any]], | ||
event: TelegramObject, | ||
data: Dict[str, Any], | ||
) -> Any: | ||
logger.info(event.text, username=event.chat.username, chat_id=event.chat.id) # type: ignore | ||
return await handler(event, data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from aiogram import Dispatcher | ||
from aiogram.utils.chat_action import ChatActionMiddleware | ||
|
||
from .error_handler import ErrorHandlerMiddleware | ||
from .is_admin import BlockNonAdminMiddleware | ||
from .logging import LogCommandsMiddleware | ||
|
||
|
||
def register_all_middlewares(dp: Dispatcher, dev_mode: bool, admin_id: int) -> None: | ||
if dev_mode: | ||
dp.message.middleware(BlockNonAdminMiddleware(admin_id)) | ||
dp.message.middleware(ChatActionMiddleware()) | ||
dp.message.middleware(LogCommandsMiddleware()) | ||
dp.message.middleware(ErrorHandlerMiddleware()) |