Skip to content

Commit 2b81cff

Browse files
committed
ready files
1 parent 2bd7b3c commit 2b81cff

16 files changed

+223
-1
lines changed

data/__init__.py

Whitespace-only changes.

data/data.py

Whitespace-only changes.

logic/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
"""This package is used for a bot logic implementation."""
2+
from .start import start
3+
4+
routers = (start,)

logic/start.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from aiogram import Router, types
2+
from aiogram.filters import CommandStart
3+
4+
start = Router()
5+
6+
7+
@start.message(CommandStart())
8+
async def start_handler(message: types.Message):
9+
await message.answer(f'Hello {message.from_user.first_name}')

poetry.lock

Lines changed: 33 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ readme = "README.md"
88
[tool.poetry.dependencies]
99
python = "^3.10"
1010
aiogram = "^3.2.0"
11+
python-dotenv = "^1.0.0"
12+
redis = "^5.0.1"
1113

1214

1315
[build-system]

root/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Bot package."""

root/__main__.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""This file represent startup bot logic."""
2+
import asyncio
3+
import logging
4+
5+
from aiogram.fsm.storage.memory import MemoryStorage
6+
7+
from utils.set_bot_commands import set_default_commands
8+
from .dispatcher import get_dispatcher
9+
from .settings import settings, bot
10+
11+
12+
async def start_bot():
13+
"""This function will start bot with polling mode."""
14+
15+
# storage = get_redis_storage(
16+
# redis=Redis(
17+
# db=settings.redis.db,
18+
# host=settings.redis.host,
19+
# password=settings.redis.passwd,
20+
# username=settings.redis.username,
21+
# port=settings.redis.port,
22+
# )
23+
# )
24+
dp = get_dispatcher(storage=MemoryStorage())
25+
await set_default_commands(bot)
26+
await dp.start_polling(
27+
bot,
28+
allowed_updates=dp.resolve_used_update_types()
29+
)
30+
31+
32+
if __name__ == '__main__':
33+
logging.basicConfig(level=settings.logging_level)
34+
asyncio.run(start_bot())

root/dispatcher.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"""This file contains build dispatcher logic."""
2+
3+
from aiogram import Dispatcher
4+
from aiogram.fsm.storage.base import BaseEventIsolation, BaseStorage
5+
from aiogram.fsm.storage.memory import MemoryStorage
6+
from aiogram.fsm.storage.redis import RedisStorage
7+
from aiogram.fsm.strategy import FSMStrategy
8+
from redis.asyncio.client import Redis
9+
10+
from root import settings
11+
from .logic import routers
12+
13+
14+
def get_redis_storage(
15+
redis: Redis, state_ttl=settings.redis.state_ttl, data_ttl=settings.redis.data_ttl
16+
):
17+
"""This function create redis storage or get it forcely from configuration.
18+
19+
:param redis: Redis client instance
20+
:param state_ttl: FSM State Time-To-Delete timer in seconds (has effect only
21+
for Redis database)
22+
:param data_ttl: FSM Data Time-To-Delete timer in seconds (has effect only
23+
for Redis database)
24+
:return: Created Redis storage.
25+
"""
26+
return RedisStorage(redis=redis, state_ttl=state_ttl, data_ttl=data_ttl)
27+
28+
29+
def get_dispatcher(
30+
storage: BaseStorage = MemoryStorage(),
31+
fsm_strategy: FSMStrategy | None = FSMStrategy.CHAT,
32+
event_isolation: BaseEventIsolation | None = None,
33+
):
34+
"""This function set up dispatcher with routers, filters and middlewares."""
35+
dp = Dispatcher(
36+
storage=storage,
37+
fsm_strategy=fsm_strategy,
38+
events_isolation=event_isolation,
39+
)
40+
for router in routers:
41+
dp.include_router(router)
42+
43+
# Register middlewares
44+
45+
return dp

root/settings.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
"""This file represents configurations from files and environment."""
2+
import logging
3+
import os
4+
from dataclasses import dataclass
5+
6+
from aiogram import Bot
7+
from dotenv import load_dotenv
8+
9+
load_dotenv()
10+
11+
12+
@dataclass
13+
class RedisConfig:
14+
"""Redis connection variables."""
15+
16+
db: int = int(os.getenv('REDIS_DATABASE', 1))
17+
""" Redis Database ID """
18+
host: str = os.getenv('REDIS_HOST', 'redis')
19+
port: int = int(os.getenv('REDIS_PORT', 6379))
20+
passwd: str | None = os.getenv('REDIS_PASSWORD')
21+
username: str | None = os.getenv('REDIS_USERNAME')
22+
state_ttl: int | None = os.getenv('REDIS_TTL_STATE', None)
23+
data_ttl: int | None = os.getenv('REDIS_TTL_DATA', None)
24+
25+
26+
@dataclass
27+
class BotConfig:
28+
"""Bot configuration."""
29+
30+
token: str = os.getenv('BOT_TOKEN')
31+
base_path: str = os.getenv('BASE_PATH')
32+
BASE_URL: str = os.getenv('BASE_URL')
33+
34+
35+
@dataclass
36+
class Configuration:
37+
"""All in one configuration's class."""
38+
39+
debug = bool(os.getenv('DEBUG'))
40+
logging_level = int(os.getenv('LOGGING_LEVEL', logging.INFO))
41+
42+
redis = RedisConfig()
43+
bot = BotConfig()
44+
45+
46+
class Settings(Configuration):
47+
base_url: str = os.getenv('BASE_URL')
48+
49+
50+
settings = Settings()
51+
bot = Bot(token=settings.bot.token)

0 commit comments

Comments
 (0)