Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion telegram_bot_project/.example.env
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
BOT_TOKEN=TELEGRAM_BOT_TOKEN
DATABASE_URL=db_driver://username:password@host:port/db_username
DATABASE_URL=db_driver+asyncpg://username:password@host:port/db_username
9 changes: 7 additions & 2 deletions telegram_bot_project/bot/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from aiogram.types import Message

from messages import *
from service.user import UserService

# Start Command Handler
async def start_command(message: Message)-> None:
Expand All @@ -10,8 +11,12 @@ async def start_command(message: Message)-> None:
user_name: str = message.from_user.username

print(f"--[INFO] - User {user_id} ({user_name}) - started the bot")

await message.answer(START_MSG)
user_find: int = await UserService.get_user_by_id(user_id)
if user_find:
await message.answer(START_MSG_AGAIN)
else:
await UserService.create_user(user_id, user_name)
await message.answer(START_MSG)

# Help Command Handler
async def help_command(message: Message) -> None:
Expand Down
17 changes: 16 additions & 1 deletion telegram_bot_project/config.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
import os
from dotenv import load_dotenv
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
from sqlalchemy.orm import sessionmaker

load_dotenv()

TOKEN: str = os.getenv("BOT_TOKEN")
DB_URL: str = os.getenv("DATABASE_URL")

engine = create_async_engine(DB_URL, echo=True)

async_session = sessionmaker(
engine,
expire_on_commit=False,
class_=AsyncSession
)

def get_token() -> str:
return TOKEN
return TOKEN

async def get_session() -> AsyncSession:
async with async_session() as session:
yield session
Empty file.
45 changes: 45 additions & 0 deletions telegram_bot_project/service/user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from sqlalchemy import text
from config import get_session

class UserService:
@staticmethod
async def create_user(user_id: int, user_name: str, language: str = "ENGLISH") -> int:
async for session in get_session():
result = await session.execute(
text("""
INSERT INTO users (id, user_name, language)
VALUES (:id, :user_name, :language)
RETURNING id
"""),
{"id": user_id, "user_name": user_name, "language": language}
)
await session.commit()
inserted_id = result.scalar_one()
return inserted_id
return None

@staticmethod
async def get_user_by_id(user_id: int):
async for session in get_session():
result = await session.execute(
text("SELECT * FROM users WHERE id = :id"),
{"id": user_id}
)
row = result.first()
return dict(row._mapping) if row else None
return None

@staticmethod
async def update_user_language(user_id: int, new_language: str) -> bool:
async for session in get_session():
result = await session.execute(
text("""
UPDATE users
SET language = :language
WHERE id = :id
"""),
{"language": new_language, "id": user_id}
)
await session.commit()
return result.rowcount == 1
return None