2025-01-09 00:19:00 +03:00
|
|
|
from functions.convert import Converter
|
|
|
|
from utils.format_number import format_number
|
|
|
|
from utils.inline_query import reply
|
|
|
|
from functions.create_chart import create_chart
|
2024-08-25 18:04:34 +03:00
|
|
|
|
|
|
|
import yaml
|
2024-08-25 22:16:23 +03:00
|
|
|
|
2025-01-08 13:55:59 +03:00
|
|
|
from aiohttp import web
|
2024-10-29 20:05:50 +03:00
|
|
|
|
2025-01-09 00:19:00 +03:00
|
|
|
from aiogram import Bot, Dispatcher, Router, types
|
2025-01-08 13:55:59 +03:00
|
|
|
from aiogram.client.default import DefaultBotProperties
|
|
|
|
from aiogram.enums import ParseMode
|
|
|
|
from aiogram.webhook.aiohttp_server import SimpleRequestHandler, setup_application
|
2023-03-08 13:31:45 +03:00
|
|
|
|
2025-01-09 00:19:00 +03:00
|
|
|
import hashlib
|
|
|
|
|
2024-11-13 17:35:23 +03:00
|
|
|
config = yaml.safe_load(open('config.yaml'))
|
2025-01-09 00:19:00 +03:00
|
|
|
bot = Bot(token=config['telegram_token'], default=DefaultBotProperties(parse_mode=ParseMode.HTML))
|
2023-03-08 13:31:45 +03:00
|
|
|
|
2025-01-08 13:55:59 +03:00
|
|
|
router = Router()
|
2023-03-08 13:31:45 +03:00
|
|
|
|
2025-01-09 00:19:00 +03:00
|
|
|
@router.inline_query()
|
|
|
|
async def currency(query: types.InlineQuery) -> None:
|
|
|
|
text = query.query.lower()
|
|
|
|
args = text.split()
|
|
|
|
result_id = hashlib.md5(text.encode()).hexdigest()
|
|
|
|
|
2025-01-09 00:55:50 +03:00
|
|
|
get_bot = await bot.get_me()
|
|
|
|
|
2025-01-09 00:19:00 +03:00
|
|
|
if len(args) < 2:
|
2025-01-09 00:55:50 +03:00
|
|
|
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)
|
2025-01-09 00:19:00 +03:00
|
|
|
|
|
|
|
conv = Converter()
|
|
|
|
|
|
|
|
from_currency, conv_currency = '', ''
|
|
|
|
|
|
|
|
if len(args) == 3:
|
2025-01-09 00:29:34 +03:00
|
|
|
try:
|
|
|
|
conv.amount = float(args[0].replace(',', '.'))
|
|
|
|
if conv.amount < 0:
|
2025-01-09 00:55:50 +03:00
|
|
|
return await reply(result_id, [("Negative amounts are not supported.", None, None)], query)
|
|
|
|
|
2025-01-09 00:29:34 +03:00
|
|
|
except ValueError:
|
2025-01-09 00:55:50 +03:00
|
|
|
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)
|
2025-01-09 00:29:34 +03:00
|
|
|
|
2025-01-09 00:19:00 +03:00
|
|
|
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)
|
|
|
|
|
|
|
|
if not conv_currency or not from_currency:
|
|
|
|
return await reply(result_id, [('The currency exchange rate could not be found.', None, None)], query)
|
|
|
|
|
|
|
|
conv.from_currency = from_currency.upper()
|
|
|
|
conv.conv_currency = conv_currency.upper()
|
|
|
|
await conv.convert()
|
|
|
|
|
|
|
|
chart = await create_chart(from_currency, conv_currency)
|
|
|
|
|
|
|
|
message = f'{format_number(conv.amount)} {conv.from_currency} = {conv.conv_amount} {conv.conv_currency}'
|
|
|
|
|
|
|
|
results = [(message, None, None)]
|
|
|
|
|
|
|
|
if chart:
|
|
|
|
results.insert(0, (f'{message}\n[График]({chart})', None, chart))
|
|
|
|
|
|
|
|
await reply(result_id, results, query)
|
|
|
|
|
2024-11-13 17:35:23 +03:00
|
|
|
|
2025-01-08 13:55:59 +03:00
|
|
|
async def on_startup(bot: Bot) -> None:
|
2025-01-09 00:19:00 +03:00
|
|
|
await bot.set_webhook(
|
|
|
|
f"{config['webhook']['base_url']}{config['webhook']['path']}",
|
|
|
|
secret_token=config['webhook']['secret_token'],
|
|
|
|
allowed_updates=['inline_query']
|
|
|
|
)
|
2024-08-29 16:54:52 +03:00
|
|
|
|
2024-11-13 17:35:23 +03:00
|
|
|
|
2025-01-08 13:55:59 +03:00
|
|
|
def main() -> None:
|
|
|
|
dp = Dispatcher()
|
2024-08-29 16:54:52 +03:00
|
|
|
|
2025-01-08 13:55:59 +03:00
|
|
|
dp.include_router(router)
|
|
|
|
dp.startup.register(on_startup)
|
|
|
|
|
|
|
|
app = web.Application()
|
|
|
|
webhook_requests_handler = SimpleRequestHandler(
|
|
|
|
dispatcher=dp,
|
|
|
|
bot=bot,
|
|
|
|
secret_token=config['webhook']['secret_token']
|
|
|
|
)
|
|
|
|
webhook_requests_handler.register(app, path=config['webhook']['path'])
|
2024-08-29 16:54:52 +03:00
|
|
|
|
2025-01-08 13:55:59 +03:00
|
|
|
setup_application(app, dp, bot=bot)
|
2024-11-28 21:50:52 +03:00
|
|
|
|
2025-01-08 15:34:06 +03:00
|
|
|
web.run_app(app, host='0.0.0.0', port=443)
|
2023-09-29 21:12:44 +03:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2025-01-08 13:55:59 +03:00
|
|
|
main()
|