From b4eba633b996b9dab0f3aeb2e92a95d653c4c6da Mon Sep 17 00:00:00 2001 From: Redume Date: Thu, 15 May 2025 10:53:55 +0300 Subject: [PATCH] chore: the commands have been organized into files --- bot.py | 10 ++++ commands/currency.py | 102 ++++++++++++++++++++++++++++++++++ commands/start.py | 17 ++++++ main.py | 129 +++---------------------------------------- 4 files changed, 137 insertions(+), 121 deletions(-) create mode 100644 bot.py create mode 100644 commands/currency.py create mode 100644 commands/start.py diff --git a/bot.py b/bot.py new file mode 100644 index 0000000..78ffb66 --- /dev/null +++ b/bot.py @@ -0,0 +1,10 @@ +from aiogram import Bot +from aiogram.client.default import DefaultBotProperties +from aiogram.enums import ParseMode +import yaml + +config = yaml.safe_load(open('../config.yaml', 'r', encoding='utf-8')) +bot = Bot( + token=config['telegram_token'], + default=DefaultBotProperties(parse_mode=ParseMode.HTML) + ) \ No newline at end of file diff --git a/commands/currency.py b/commands/currency.py new file mode 100644 index 0000000..c6882b1 --- /dev/null +++ b/commands/currency.py @@ -0,0 +1,102 @@ +import hashlib + +from aiogram import Command, types, Router + +from main import bot +from functions.convert import Converter +from functions.create_chart import create_chart +from utils.format_number import format_number +from utils.inline_query import reply + +router = Router() + + +@router.inline_query() +async def currency(query: types.InlineQuery) -> None: + text = query.query.lower() + args = text.split() + result_id = hashlib.md5(text.encode()).hexdigest() + + get_bot = await bot.get_me() + + if len(args) < 2: + return await reply(result_id, + [("2 or 3 arguments are required.", + f'@{get_bot.username} USD RUB \n' + f'@{get_bot.username} 12 USD RUB', + None, None)], + query) + + conv = Converter() + + from_currency, conv_currency = '', '' + + if len(args) == 3: + try: + conv.amount = float(args[0].replace(',', '.')) + if conv.amount < 0: + return await reply( + result_id, + [ + ("Negative amounts are not supported.", None, None) + ], + query + ) + + except ValueError: + return await reply( + result_id, + [ + ( + "Please enter a valid number for the amount.", + f'@{get_bot.username} USD RUB \n' + f'@{get_bot.username} 12 USD RUB', + None, None + ) + ], + query) + + from_currency = args[1] + conv_currency = args[2] + elif len(args) == 2: + from_currency = args[0] + conv_currency = args[1] + else: + return await reply( + result_id, + [ + ( + 'The source and target currency could not be determined.', + None, None + ) + ], + query + ) + + conv.from_currency = from_currency.upper() + conv.conv_currency = conv_currency.upper() + try: + await conv.convert() + except RuntimeError: + return await reply( + result_id, + [ + ( + 'The currency exchange rate could not be determined', + None, None + ) + ], + query + ) + + chart = await create_chart(from_currency, conv_currency) + + message = f'{format_number(conv.amount)} {conv.from_currency} ' \ + f'= {conv.conv_amount} {conv.conv_currency}' + + results = [(message, None, None)] + + if chart: + results.insert(0, (f'{message}\n[Chart]({chart})', None, chart)) + + await reply(result_id, results, query) \ No newline at end of file diff --git a/commands/start.py b/commands/start.py new file mode 100644 index 0000000..2e944ac --- /dev/null +++ b/commands/start.py @@ -0,0 +1,17 @@ +from aiogram import CommandStart, types, Router + +from main import bot + +router = Router() + +@router.message(CommandStart()) +async def start(message: types.Message) -> None: + get_bot = await bot.get_me() + await message.reply( + 'Shirino is a telegram bot for converting fiat or cryptocurrency. ' + 'The example of use occurs via inline query:\n' + f'`@{get_bot.username} USD RUB` \n' + f'`@{get_bot.username} 12 USD RUB` \n\n' + '[Source Code](https://github.com/Redume/Shirino)', + parse_mode='markdown' + ) diff --git a/main.py b/main.py index aa3ff14..76a854f 100644 --- a/main.py +++ b/main.py @@ -1,134 +1,18 @@ -import hashlib import yaml from aiohttp import web -from aiogram import Bot, Dispatcher, Router, types -from aiogram.client.default import DefaultBotProperties -from aiogram.enums import ParseMode +from aiogram import Dispatcher from aiogram.webhook.aiohttp_server import SimpleRequestHandler, setup_application -from aiogram.filters import CommandStart -from functions.convert import Converter -from functions.create_chart import create_chart -from utils.format_number import format_number -from utils.inline_query import reply +from commands import currency, start, settings from database.server import Database +from bot import bot config = yaml.safe_load(open('../config.yaml', 'r', encoding='utf-8')) -bot = Bot( - token=config['telegram_token'], - default=DefaultBotProperties(parse_mode=ParseMode.HTML) - ) db = Database('shirino.db') -router = Router() - -@router.message(CommandStart()) -async def start(message: types.Message) -> None: - get_bot = await bot.get_me() - await message.reply( - 'Shirino is a telegram bot for converting fiat or cryptocurrency. ' - 'The example of use occurs via inline query:\n' - f'`@{get_bot.username} USD RUB` \n' - f'`@{get_bot.username} 12 USD RUB` \n\n' - '[Source Code](https://github.com/Redume/Shirino)', - parse_mode='markdown' - ) - - -@router.inline_query() -async def currency(query: types.InlineQuery) -> None: - text = query.query.lower() - args = text.split() - result_id = hashlib.md5(text.encode()).hexdigest() - - get_bot = await bot.get_me() - - if len(args) < 2: - return await reply(result_id, - [("2 or 3 arguments are required.", - f'@{get_bot.username} USD RUB \n' - f'@{get_bot.username} 12 USD RUB', - None, None)], - query) - - conv = Converter() - - from_currency, conv_currency = '', '' - - if len(args) == 3: - try: - conv.amount = float(args[0].replace(',', '.')) - if conv.amount < 0: - return await reply( - result_id, - [ - ("Negative amounts are not supported.", None, None) - ], - query - ) - - except ValueError: - return await reply( - result_id, - [ - ( - "Please enter a valid number for the amount.", - f'@{get_bot.username} USD RUB \n' - f'@{get_bot.username} 12 USD RUB', - None, None - ) - ], - query) - - from_currency = args[1] - conv_currency = args[2] - elif len(args) == 2: - from_currency = args[0] - conv_currency = args[1] - else: - return await reply( - result_id, - [ - ( - 'The source and target currency could not be determined.', - None, None - ) - ], - query - ) - - conv.from_currency = from_currency.upper() - conv.conv_currency = conv_currency.upper() - try: - await conv.convert() - except RuntimeError: - return await reply( - result_id, - [ - ( - 'The currency exchange rate could not be determined', - None, None - ) - ], - query - ) - - chart = await create_chart(from_currency, conv_currency) - - message = f'{format_number(conv.amount)} {conv.from_currency} ' \ - f'= {conv.conv_amount} {conv.conv_currency}' - - results = [(message, None, None)] - - if chart: - results.insert(0, (f'{message}\n[Chart]({chart})', None, chart)) - - await reply(result_id, results, query) - - -async def on_startup(bot: Bot) -> None: +async def on_startup(bot: bot) -> None: await db.connect() await db._create_table() await bot.set_webhook( @@ -145,7 +29,10 @@ async def on_shutdown(): def main() -> None: dp = Dispatcher() - dp.include_router(router) + dp.include_router(currency.router) + dp.include_router(start.router) + dp.include_router(settings.router) + dp.startup.register(on_startup) dp.shutdown.register(on_shutdown)