2024-08-25 18:04:34 +03:00
|
|
|
from aiogram import Bot, Dispatcher, types
|
|
|
|
|
2023-12-31 12:43:45 +03:00
|
|
|
import asyncio
|
2024-08-25 18:04:34 +03:00
|
|
|
import yaml
|
2024-10-29 20:05:50 +03:00
|
|
|
import requests
|
2024-08-25 22:16:23 +03:00
|
|
|
|
2023-12-31 12:43:45 +03:00
|
|
|
import hashlib
|
2024-08-25 18:04:34 +03:00
|
|
|
import json
|
2024-10-29 20:05:50 +03:00
|
|
|
|
|
|
|
from http import HTTPStatus
|
|
|
|
|
2024-08-25 18:04:34 +03:00
|
|
|
from function.convert import Converter
|
2024-08-26 11:18:54 +03:00
|
|
|
from function.format_number import format_number
|
2023-03-08 13:31:45 +03:00
|
|
|
|
2024-06-14 14:02:10 +03:00
|
|
|
config = yaml.safe_load(open("config.yaml"))
|
2023-09-29 21:12:44 +03:00
|
|
|
dp = Dispatcher()
|
2023-03-08 13:31:45 +03:00
|
|
|
|
|
|
|
|
2024-08-25 18:04:34 +03:00
|
|
|
@dp.message()
|
2023-09-29 21:12:44 +03:00
|
|
|
@dp.inline_query()
|
2024-08-29 16:54:52 +03:00
|
|
|
async def currency(query: types.Message | 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-08-29 16:54:52 +03:00
|
|
|
text = query.query if isinstance(query, types.InlineQuery) else query.text
|
|
|
|
args = text.split()
|
|
|
|
result_id = hashlib.md5(text.encode()).hexdigest()
|
|
|
|
conv = Converter()
|
|
|
|
|
2023-12-31 13:26:38 +03:00
|
|
|
if len(args) <= 1:
|
2024-09-28 15:02:02 +03:00
|
|
|
try:
|
|
|
|
if query.chat.type in ['supergroup', 'group']:
|
|
|
|
return
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
return await reply(result_id,
|
|
|
|
"2 or 3 arguments are required.",
|
|
|
|
"@shirino_bot USD RUB "
|
|
|
|
"\n@shirino_bot 12 USD RUB",
|
2024-10-29 20:05:50 +03:00
|
|
|
None,
|
2024-09-28 15:02:02 +03:00
|
|
|
query)
|
2024-08-29 16:54:52 +03:00
|
|
|
if len(args) == 4:
|
|
|
|
conv.amount = float(args[0])
|
2024-10-29 18:14:28 +03:00
|
|
|
from_currency = args[1].lower()
|
|
|
|
conv_currency = args[3].lower()
|
2024-08-29 16:54:52 +03:00
|
|
|
elif len(args) == 3:
|
|
|
|
conv.amount = float(args[0])
|
2024-10-29 18:14:28 +03:00
|
|
|
from_currency = args[1].lower()
|
|
|
|
conv_currency = args[2].lower()
|
2023-12-31 12:43:45 +03:00
|
|
|
elif len(args) == 2:
|
2024-10-29 18:14:28 +03:00
|
|
|
from_currency = args[0].lower()
|
|
|
|
conv_currency = args[1].lower()
|
2024-08-29 16:54:52 +03:00
|
|
|
else:
|
2024-09-28 15:02:02 +03:00
|
|
|
try:
|
|
|
|
if query.chat.type in ['supergroup', 'group']:
|
|
|
|
return
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
2024-10-29 20:05:50 +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:
|
|
|
|
return await reply(result_id,
|
|
|
|
'The currency exchange rate could not be found.',
|
|
|
|
None,
|
2024-10-29 20:05:50 +03:00
|
|
|
None,
|
2024-08-29 16:54:52 +03:00
|
|
|
query)
|
|
|
|
|
|
|
|
conv.from_currency = from_currency.upper()
|
|
|
|
conv.conv_currency = conv_currency.upper()
|
|
|
|
conv.convert()
|
|
|
|
|
2024-10-29 20:05:50 +03:00
|
|
|
res_chart = requests.get(f'{config['kekkai_instance']}/api/getChart/week/', {
|
|
|
|
'from_currency': from_currency,
|
|
|
|
'conv_currency': conv_currency
|
|
|
|
}, timeout=3)
|
|
|
|
|
|
|
|
if not HTTPStatus(res_chart.status_code).is_success:
|
|
|
|
res_chart = None
|
|
|
|
else:
|
|
|
|
res_chart = res_chart.json()['message']
|
|
|
|
|
|
|
|
result = f'{format_number(conv.amount)} {conv.from_currency} = {conv.conv_amount} {conv.conv_currency}' \
|
|
|
|
f'\n{f'[График]({res_chart})' if res_chart else ''}'
|
|
|
|
return await reply(result_id, result, None, res_chart, query)
|
2024-08-29 16:54:52 +03:00
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
|
|
|
|
|
|
|
|
2024-10-29 20:05:50 +03:00
|
|
|
async def reply(
|
|
|
|
result_id: str | None,
|
|
|
|
title: str,
|
|
|
|
desc: str | None,
|
|
|
|
img: str | None,
|
|
|
|
query: types.InlineQuery | types.Message,
|
|
|
|
) -> None:
|
|
|
|
|
2024-08-29 16:54:52 +03:00
|
|
|
if isinstance(query, types.InlineQuery):
|
|
|
|
article = [None]
|
|
|
|
article[0] = types.InlineQueryResultArticle(
|
|
|
|
id=result_id,
|
|
|
|
title=title,
|
2024-10-29 20:05:50 +03:00
|
|
|
thumbnail_url=img,
|
2024-08-29 16:54:52 +03:00
|
|
|
description=desc,
|
|
|
|
input_message_content=types.InputTextMessageContent(
|
2024-10-29 20:05:50 +03:00
|
|
|
message_text=title,
|
|
|
|
parse_mode='markdown'
|
2024-08-29 16:54:52 +03:00
|
|
|
)
|
2023-12-31 12:43:45 +03:00
|
|
|
)
|
2023-05-30 11:51:20 +03:00
|
|
|
|
2024-08-29 16:54:52 +03:00
|
|
|
await query.answer(
|
|
|
|
article,
|
2024-10-29 20:05:50 +03:00
|
|
|
parse_mode='markdown',
|
|
|
|
cache_time=0,
|
2024-08-29 16:54:52 +03:00
|
|
|
is_personal=True,
|
|
|
|
)
|
|
|
|
else:
|
2024-10-29 20:05:50 +03:00
|
|
|
await query.answer(f'{title} \n{'' if desc else ''}')
|
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())
|