2024-08-25 18:04:34 +03:00
|
|
|
from aiogram import Bot, Dispatcher, types
|
|
|
|
|
|
|
|
import yaml
|
2024-11-13 17:35:23 +03:00
|
|
|
import asyncio
|
2024-08-25 22:16:23 +03:00
|
|
|
|
2023-12-31 12:43:45 +03:00
|
|
|
import hashlib
|
2024-10-29 20:05:50 +03:00
|
|
|
|
2024-11-28 21:50:52 +03:00
|
|
|
from function.get_chart import get_chart
|
|
|
|
from utils.convert import Converter
|
|
|
|
from utils.format_number import format_number
|
|
|
|
from function.inline_query import reply
|
2023-03-08 13:31:45 +03:00
|
|
|
|
2023-09-29 21:12:44 +03:00
|
|
|
dp = Dispatcher()
|
2024-11-13 17:35:23 +03:00
|
|
|
config = yaml.safe_load(open('config.yaml'))
|
2023-03-08 13:31:45 +03:00
|
|
|
|
2023-09-29 21:12:44 +03:00
|
|
|
@dp.inline_query()
|
2024-11-13 17:35:23 +03:00
|
|
|
async def currency(query: types.InlineQuery) -> None:
|
2024-10-29 18:14:28 +03:00
|
|
|
global result, from_currency, conv_currency
|
2023-03-08 13:31:45 +03:00
|
|
|
|
|
|
|
try:
|
2024-11-13 17:35:23 +03:00
|
|
|
text = query.query.lower()
|
2024-08-29 16:54:52 +03:00
|
|
|
args = text.split()
|
|
|
|
result_id = hashlib.md5(text.encode()).hexdigest()
|
2024-11-13 17:35:23 +03:00
|
|
|
|
2024-08-29 16:54:52 +03:00
|
|
|
conv = Converter()
|
|
|
|
|
2024-11-24 13:17:09 +03:00
|
|
|
if len(args) < 2:
|
2024-11-13 17:35:23 +03:00
|
|
|
return await reply(result_id,
|
|
|
|
[("2 or 3 arguments are required.",
|
|
|
|
'@shirino_bot USD RUB \n'
|
2024-11-15 21:34:50 +03:00
|
|
|
'@shirino_bot 12 USD RUB',
|
|
|
|
None, None)],
|
2024-11-13 17:35:23 +03:00
|
|
|
query)
|
|
|
|
|
|
|
|
if len(args) == 3:
|
2024-08-29 16:54:52 +03:00
|
|
|
conv.amount = float(args[0])
|
2024-11-13 17:35:23 +03:00
|
|
|
from_currency = args[1]
|
|
|
|
conv_currency = args[2]
|
2023-12-31 12:43:45 +03:00
|
|
|
elif len(args) == 2:
|
2024-11-13 17:35:23 +03:00
|
|
|
from_currency = args[0]
|
|
|
|
conv_currency = args[1]
|
2024-08-29 16:54:52 +03:00
|
|
|
else:
|
2024-11-28 21:50:52 +03:00
|
|
|
return await reply(result_id,
|
|
|
|
[
|
|
|
|
(
|
|
|
|
'The source and target currency could not be determined.',
|
|
|
|
None,
|
|
|
|
None
|
|
|
|
)
|
|
|
|
],
|
|
|
|
query)
|
2024-08-29 16:54:52 +03:00
|
|
|
|
|
|
|
if not from_currency or not conv_currency:
|
2024-11-15 21:34:50 +03:00
|
|
|
return await reply(result_id, [('The currency exchange rate could not be found.', None, None)], query)
|
2024-08-29 16:54:52 +03:00
|
|
|
|
|
|
|
conv.from_currency = from_currency.upper()
|
|
|
|
conv.conv_currency = conv_currency.upper()
|
|
|
|
conv.convert()
|
|
|
|
|
2024-11-28 21:50:52 +03:00
|
|
|
chart = get_chart(from_currency, conv_currency)
|
|
|
|
|
|
|
|
if not chart:
|
|
|
|
return await reply(result_id,
|
|
|
|
[
|
|
|
|
(
|
|
|
|
f'{format_number(conv.amount)} {conv.from_currency} '
|
|
|
|
f'= {conv.conv_amount} {conv.conv_currency}' \
|
|
|
|
f'\n{f'[График]({chart})' if chart else ''}',
|
|
|
|
None,
|
|
|
|
chart
|
|
|
|
)
|
|
|
|
],
|
|
|
|
query)
|
2024-10-29 20:05:50 +03:00
|
|
|
|
2024-11-13 17:35:23 +03:00
|
|
|
await reply(result_id,
|
|
|
|
[
|
|
|
|
(
|
2024-11-28 21:50:52 +03:00
|
|
|
f'{format_number(conv.amount)} {conv.from_currency} '
|
|
|
|
f'= {conv.conv_amount} {conv.conv_currency}' \
|
|
|
|
f'\n{f'[График]({chart})' if chart else ''}',
|
2024-11-13 17:35:23 +03:00
|
|
|
None,
|
2024-11-28 21:50:52 +03:00
|
|
|
chart
|
2024-11-13 17:35:23 +03:00
|
|
|
),
|
|
|
|
(
|
2024-11-28 21:50:52 +03:00
|
|
|
f'{format_number(conv.amount)} {conv.from_currency} '
|
|
|
|
f'= {conv.conv_amount} {conv.conv_currency}',
|
2024-11-13 17:35:23 +03:00
|
|
|
None,
|
|
|
|
None
|
|
|
|
)
|
|
|
|
],
|
|
|
|
query)
|
2024-08-29 16:54:52 +03:00
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
|
|
|
|
|
|
|
|
2023-03-08 13:31:45 +03:00
|
|
|
|
2023-09-29 21:12:44 +03:00
|
|
|
async def main() -> None:
|
2024-06-14 14:02:10 +03:00
|
|
|
bot = Bot(config['telegram_token'])
|
2023-09-29 21:12:44 +03:00
|
|
|
await dp.start_polling(bot)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
asyncio.run(main())
|