From 1c1dd0191f13d0165ffc516589a37aed784d7936 Mon Sep 17 00:00:00 2001 From: ITQ Date: Wed, 19 Feb 2025 14:37:00 +0300 Subject: [PATCH] feat(telegram_bot): added commands folder --- .../telegram_bot/commands/__init__.py | 0 .../telegram_bot/commands/campaigns.py | 19 +++++++++++ .../services/telegram_bot/commands/logout.py | 20 +++++++++++ .../services/telegram_bot/commands/start.py | 27 +++++++++++++++ .../services/telegram_bot/commands/stats.py | 33 +++++++++++++++++++ 5 files changed, 99 insertions(+) create mode 100644 solution/services/telegram_bot/commands/__init__.py create mode 100644 solution/services/telegram_bot/commands/campaigns.py create mode 100644 solution/services/telegram_bot/commands/logout.py create mode 100644 solution/services/telegram_bot/commands/start.py create mode 100644 solution/services/telegram_bot/commands/stats.py diff --git a/solution/services/telegram_bot/commands/__init__.py b/solution/services/telegram_bot/commands/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/solution/services/telegram_bot/commands/campaigns.py b/solution/services/telegram_bot/commands/campaigns.py new file mode 100644 index 0000000..8759cf1 --- /dev/null +++ b/solution/services/telegram_bot/commands/campaigns.py @@ -0,0 +1,19 @@ +from aiogram import Router +from aiogram.filters import Command +from aiogram.fsm.context import FSMContext +from aiogram.types import Message +from aiogram_dialog import DialogManager, StartMode + +from filters.auth import AuthenticatedFilter +from states.campaigns import CampaignsDailogState + +campaigns_router = Router() + + +@campaigns_router.message(Command("campaigns"), AuthenticatedFilter()) +async def stats_command( + message: Message, dialog_manager: DialogManager, state: FSMContext +) -> None: + await dialog_manager.start( + state=CampaignsDailogState.campaigns, mode=StartMode.RESET_STACK + ) diff --git a/solution/services/telegram_bot/commands/logout.py b/solution/services/telegram_bot/commands/logout.py new file mode 100644 index 0000000..37447b1 --- /dev/null +++ b/solution/services/telegram_bot/commands/logout.py @@ -0,0 +1,20 @@ +from aiogram import Router +from aiogram.filters import Command +from aiogram.fsm.context import FSMContext +from aiogram.types import Message +from aiogram_dialog import DialogManager + +logout_router = Router() + + +@logout_router.message(Command("logout")) +async def logout_command( + message: Message, dialog_manager: DialogManager, state: FSMContext +) -> None: + state_data = await state.get_data() + + if state_data["authenticated"]: + await dialog_manager.reset_stack() + del state_data["advertiser_id"] + await state.set_data(state_data) + await message.answer("Successfully logged out.") diff --git a/solution/services/telegram_bot/commands/start.py b/solution/services/telegram_bot/commands/start.py new file mode 100644 index 0000000..62da163 --- /dev/null +++ b/solution/services/telegram_bot/commands/start.py @@ -0,0 +1,27 @@ +from aiogram import Router +from aiogram.filters import CommandStart +from aiogram.fsm.context import FSMContext +from aiogram.types import Message +from aiogram_dialog import DialogManager, StartMode + +from states.start import StartDialogState + +start_router = Router() + + +@start_router.message(CommandStart()) +async def start_command( + message: Message, dialog_manager: DialogManager, state: FSMContext +) -> None: + state_data = await state.get_data() + + if state_data["authenticated"]: + await message.answer( + "Already authenticated as" + f" {state_data['advertiser']['name']}" + ) + return + + await dialog_manager.start( + state=StartDialogState.start, mode=StartMode.RESET_STACK + ) diff --git a/solution/services/telegram_bot/commands/stats.py b/solution/services/telegram_bot/commands/stats.py new file mode 100644 index 0000000..b3cef11 --- /dev/null +++ b/solution/services/telegram_bot/commands/stats.py @@ -0,0 +1,33 @@ +from aiogram import Router +from aiogram.filters import Command +from aiogram.fsm.context import FSMContext +from aiogram.types import Message +from aiogram_dialog import DialogManager + +from api.client import AdNovaClient +from filters.auth import AuthenticatedFilter + +stats_router = Router() + + +@stats_router.message(Command("stats"), AuthenticatedFilter()) +async def stats_command( + message: Message, dialog_manager: DialogManager, state: FSMContext +) -> None: + state_data = await state.get_data() + advertiser_id = state_data["advertiser_id"] + + async with AdNovaClient() as client: + stats = await client.get_advertiser_statistics(advertiser_id) + + response = ( + f"📊 Overall {state_data['advertiser']['name']}" + " statistics:\n\n" + f"\t• Impressions: {stats.impressions_count}\n" + f"\t• Clicks: {stats.clicks_count}\n" + f"\t• Conversion: {stats.conversion:.2f}%\n" + f"\t• Spent on impressions: ${stats.spent_impressions:.2f}\n" + f"\t• Spent on clicks: ${stats.spent_clicks:.2f}\n" + f"\t• Spent total: ${stats.spent_total:.2f}" + ) + await message.answer(response)