chore: the commands have been organized into files

This commit is contained in:
Danil 2025-05-15 10:53:55 +03:00
parent 4aa4742cd2
commit b4eba633b9
4 changed files with 137 additions and 121 deletions

10
bot.py Normal file
View file

@ -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)
)

102
commands/currency.py Normal file
View file

@ -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)

17
commands/start.py Normal file
View file

@ -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'
)

129
main.py
View file

@ -1,134 +1,18 @@
import hashlib
import yaml import yaml
from aiohttp import web from aiohttp import web
from aiogram import Bot, Dispatcher, Router, types from aiogram import Dispatcher
from aiogram.client.default import DefaultBotProperties
from aiogram.enums import ParseMode
from aiogram.webhook.aiohttp_server import SimpleRequestHandler, setup_application from aiogram.webhook.aiohttp_server import SimpleRequestHandler, setup_application
from aiogram.filters import CommandStart
from functions.convert import Converter from commands import currency, start, settings
from functions.create_chart import create_chart
from utils.format_number import format_number
from utils.inline_query import reply
from database.server import Database from database.server import Database
from bot import bot
config = yaml.safe_load(open('../config.yaml', 'r', encoding='utf-8')) 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') db = Database('shirino.db')
router = Router() async def on_startup(bot: bot) -> None:
@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:
await db.connect() await db.connect()
await db._create_table() await db._create_table()
await bot.set_webhook( await bot.set_webhook(
@ -145,7 +29,10 @@ async def on_shutdown():
def main() -> None: def main() -> None:
dp = Dispatcher() 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.startup.register(on_startup)
dp.shutdown.register(on_shutdown) dp.shutdown.register(on_shutdown)