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-08-25 22:16:23 +03:00
|
|
|
|
2023-12-31 12:43:45 +03:00
|
|
|
import hashlib
|
2024-06-14 14:02:10 +03:00
|
|
|
import aiohttp
|
2024-08-25 22:16:23 +03:00
|
|
|
|
2024-08-25 18:04:34 +03:00
|
|
|
import json
|
2024-08-25 22:16:23 +03:00
|
|
|
import re
|
2023-03-08 13:31:45 +03:00
|
|
|
|
2024-08-25 22:16:23 +03:00
|
|
|
from word2number import w2n
|
2024-08-25 18:04:34 +03:00
|
|
|
from function.convert import Converter
|
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()
|
|
|
|
async def message_conv(message: types.Message):
|
2024-08-25 22:16:23 +03:00
|
|
|
with open('currency.json', 'r', encoding='utf-8') as f:
|
|
|
|
currency_json = json.load(f)
|
|
|
|
|
|
|
|
text = message.text.lower()
|
|
|
|
args = text.split()
|
|
|
|
|
|
|
|
number_match = re.match(r'\d+\.?\d*|\w+', args[0])
|
|
|
|
if number_match:
|
|
|
|
number_text = number_match.group(0)
|
|
|
|
|
|
|
|
try:
|
|
|
|
amount = float(number_text)
|
|
|
|
except ValueError:
|
|
|
|
try:
|
|
|
|
amount = float(w2n.word_to_num(number_text))
|
|
|
|
except ValueError:
|
|
|
|
await message.reply("Не удалось распознать числовое значение.")
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
await message.reply("Не удалось найти числовое значение.")
|
|
|
|
return
|
|
|
|
|
|
|
|
if len(args) == 4:
|
|
|
|
source_currency_alias = args[1]
|
|
|
|
target_currency_alias = args[3]
|
|
|
|
elif len(args) == 3:
|
|
|
|
source_currency_alias = args[0]
|
|
|
|
target_currency_alias = args[2]
|
|
|
|
else:
|
|
|
|
await message.reply("Не удалось определить исходную и целевую валюту.")
|
|
|
|
return
|
2023-05-30 13:33:41 +03:00
|
|
|
|
2024-08-25 18:04:34 +03:00
|
|
|
source_currency_code = None
|
|
|
|
target_currency_code = None
|
2023-11-22 22:18:23 +03:00
|
|
|
|
2024-08-25 18:04:34 +03:00
|
|
|
for currency_code, aliases in currency_json.items():
|
|
|
|
if source_currency_alias in aliases:
|
|
|
|
source_currency_code = currency_code
|
2024-08-25 22:16:23 +03:00
|
|
|
if target_currency_alias in aliases:
|
2024-08-25 18:04:34 +03:00
|
|
|
target_currency_code = currency_code
|
2023-05-30 13:47:06 +03:00
|
|
|
|
2024-08-25 22:16:23 +03:00
|
|
|
if not source_currency_code or not target_currency_code or amount is None:
|
|
|
|
await message.reply("Не удалось найти сумму или валюты по указанным данным.")
|
|
|
|
return
|
2023-06-01 16:39:46 +03:00
|
|
|
|
2024-08-25 22:16:23 +03:00
|
|
|
conv = Converter()
|
|
|
|
conv.amount = amount
|
|
|
|
conv.from_currency = source_currency_code.upper()
|
|
|
|
conv.conv_currency = target_currency_code.upper()
|
|
|
|
conv.convert()
|
2023-03-08 13:31:45 +03:00
|
|
|
|
2024-08-25 22:16:23 +03:00
|
|
|
result = f'{conv.amount} {conv.from_currency} = {conv.conv_amount} {conv.conv_currency}'
|
2024-08-25 18:04:34 +03:00
|
|
|
await message.reply(result)
|
2023-06-01 16:39:46 +03:00
|
|
|
|
|
|
|
|
2024-08-25 18:04:34 +03:00
|
|
|
async def inline_reply(result_id: str, title: str, description: str or None, inline_query: types.InlineQuery) -> None:
|
2024-06-14 14:02:10 +03:00
|
|
|
article = [None]
|
2023-12-31 14:12:13 +03:00
|
|
|
article[0] = types.InlineQueryResultArticle(
|
|
|
|
id=result_id,
|
|
|
|
title=title,
|
|
|
|
description=description,
|
|
|
|
input_message_content=types.InputTextMessageContent(
|
|
|
|
message_text=title
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
await inline_query.answer(
|
|
|
|
article,
|
|
|
|
cache_time=1,
|
|
|
|
is_personal=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-09-29 21:12:44 +03:00
|
|
|
@dp.inline_query()
|
2024-08-25 18:04:34 +03:00
|
|
|
async def currency(inline_query: types.InlineQuery) -> None:
|
2024-08-25 20:01:25 +03:00
|
|
|
global result
|
2023-05-30 13:33:41 +03:00
|
|
|
query = inline_query.query
|
2023-11-22 22:18:23 +03:00
|
|
|
args = query.split()
|
2023-03-08 13:31:45 +03:00
|
|
|
|
2023-05-30 13:33:41 +03:00
|
|
|
result_id = hashlib.md5(query.encode()).hexdigest()
|
2024-08-25 18:04:34 +03:00
|
|
|
conv = Converter()
|
2023-03-08 13:31:45 +03:00
|
|
|
|
|
|
|
try:
|
2023-12-31 13:26:38 +03:00
|
|
|
if len(args) <= 1:
|
2023-12-31 14:12:13 +03:00
|
|
|
await inline_reply(result_id,
|
2024-08-25 18:04:34 +03:00
|
|
|
"2 or 3 arguments are required",
|
2023-12-31 14:12:13 +03:00
|
|
|
f"@shirino_bot USD RUB \n@shirino_bot 12 USD RUB", inline_query)
|
|
|
|
|
2023-12-31 12:43:45 +03:00
|
|
|
if len(args) == 3:
|
2024-08-25 20:01:25 +03:00
|
|
|
conv.amount = float(args[0].replace(',', '.'))
|
2023-11-22 22:18:23 +03:00
|
|
|
conv.from_currency = args[1].upper()
|
|
|
|
conv.conv_currency = args[2].upper()
|
2023-05-30 13:33:41 +03:00
|
|
|
conv.convert()
|
2023-12-31 12:43:45 +03:00
|
|
|
elif len(args) == 2:
|
2023-11-22 22:18:23 +03:00
|
|
|
conv.from_currency = args[0].upper()
|
|
|
|
conv.conv_currency = args[1].upper()
|
2023-05-30 13:33:41 +03:00
|
|
|
conv.convert()
|
2023-05-30 11:51:20 +03:00
|
|
|
|
2023-12-31 12:43:45 +03:00
|
|
|
result = (
|
|
|
|
f'{conv.amount} {conv.from_currency} = '
|
|
|
|
f'{conv.conv_amount} {conv.conv_currency}'
|
|
|
|
)
|
2023-05-30 11:51:20 +03:00
|
|
|
|
2024-08-25 18:04:34 +03:00
|
|
|
except aiohttp.client_exceptions.ClientError:
|
2023-12-31 14:19:28 +03:00
|
|
|
await inline_reply(result_id,
|
2024-08-25 18:04:34 +03:00
|
|
|
"Rate-limit from the Telegram API, repeat the request later",
|
2023-12-31 14:12:13 +03:00
|
|
|
None,
|
|
|
|
inline_query)
|
|
|
|
|
2023-11-22 22:18:23 +03:00
|
|
|
await asyncio.sleep(1)
|
2023-10-28 22:02:29 +03:00
|
|
|
|
2024-08-25 20:01:25 +03:00
|
|
|
except Exception:
|
2024-08-25 18:04:34 +03:00
|
|
|
await inline_reply(result_id, "Invalid data format",
|
2023-12-31 14:19:28 +03:00
|
|
|
"@shirino_bot USD RUB \n@shirino_bot 12 USD RUB",
|
|
|
|
inline_query)
|
2023-05-30 11:51:20 +03:00
|
|
|
|
2023-12-31 14:19:28 +03:00
|
|
|
await inline_reply(result_id, result, None, inline_query)
|
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())
|