From 454adb4557d25092d9257fa0fd136b8cd97ec548 Mon Sep 17 00:00:00 2001 From: Redume Date: Thu, 29 Aug 2024 16:54:52 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9E=D1=82=D0=BA=D0=B0=D0=B7=D0=B0=D0=BB?= =?UTF-8?q?=D1=81=D1=8F=20=D0=BE=D1=82=20w2n.=20=D0=9E=D0=B1=D1=8C=D0=B5?= =?UTF-8?q?=D0=B4=D0=B5=D0=BD=D0=B8=D0=BB=20=D0=B4=D0=B2=D0=B5=20=D1=84?= =?UTF-8?q?=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D0=B8=20(message=20=D0=B8=20Inli?= =?UTF-8?q?neQuery),=20=D1=80=D0=B5=D1=84=D0=B0=D0=BA=D1=82=D0=BE=D1=80=20?= =?UTF-8?q?=D0=BA=D0=BE=D0=B4=D0=B0.=20=D0=9F=D0=B5=D1=80=D0=B5=D0=B2?= =?UTF-8?q?=D0=BE=D0=B4=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BE=D0=BA=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=B0=D0=BD=D0=B3=D0=BB=D0=B8=D0=B9=D1=81=D0=BA=D0=B8?= =?UTF-8?q?=D0=B9=20=D1=8F=D0=B7=D1=8B=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 182 +++++++++++++++++++++----------------------------------- 1 file changed, 69 insertions(+), 113 deletions(-) diff --git a/main.py b/main.py index 0032419..a5f3434 100644 --- a/main.py +++ b/main.py @@ -4,12 +4,8 @@ import asyncio import yaml import hashlib -import aiohttp import json -import re - -from word2number import w2n from function.convert import Converter from function.format_number import format_number @@ -18,124 +14,84 @@ dp = Dispatcher() @dp.message() -async def message_conv(message: types.Message): - 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 - - source_currency_code = None - target_currency_code = None - - for currency_code, aliases in currency_json.items(): - if source_currency_alias in aliases: - source_currency_code = currency_code - if target_currency_alias in aliases: - target_currency_code = currency_code - - if not source_currency_code or not target_currency_code or amount is None: - await message.reply("Не удалось найти сумму или валюты по указанным данным.") - return - - conv = Converter() - conv.amount = amount - conv.from_currency = source_currency_code.upper() - conv.conv_currency = target_currency_code.upper() - conv.convert() - - result = f'{format_number(conv.amount)} {conv.from_currency} = {conv.conv_amount} {conv.conv_currency}' - await message.reply(result) - - -async def inline_reply(result_id: str, title: str, description: str or None, inline_query: types.InlineQuery) -> None: - article = [None] - 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, - ) - - @dp.inline_query() -async def currency(inline_query: types.InlineQuery) -> None: +async def currency(query: types.Message | types.InlineQuery) -> None: global result - query = inline_query.query - args = query.split() - - result_id = hashlib.md5(query.encode()).hexdigest() - conv = Converter() try: + currency_json = json.load(open('currency.json', 'r', encoding='utf-8')) + text = query.query if isinstance(query, types.InlineQuery) else query.text + args = text.split() + result_id = hashlib.md5(text.encode()).hexdigest() + conv = Converter() + if len(args) <= 1: - await inline_reply(result_id, - "2 or 3 arguments are required", - f"@shirino_bot USD RUB \n@shirino_bot 12 USD RUB", inline_query) - - if len(args) == 3: - conv.amount = float(args[0].replace(',', '.')) - conv.from_currency = args[1].upper() - conv.conv_currency = args[2].upper() - conv.convert() + return await reply(result_id, + "2 or 3 arguments are required.", + "@shirino_bot USD RUB " + "\n@shirino_bot 12 USD RUB", + query) + if len(args) == 4: + conv.amount = float(args[0]) + from_currency_alias = args[1].lower() + conv_currency_alias = args[3].lower() + elif len(args) == 3: + conv.amount = float(args[0]) + from_currency_alias = args[1].lower() + conv_currency_alias = args[2].lower() elif len(args) == 2: - conv.from_currency = args[0].upper() - conv.conv_currency = args[1].upper() - conv.convert() + from_currency_alias = args[0].lower() + conv_currency_alias = args[1].lower() + else: + return await reply(result_id, 'The source and target currency could not be determined.', None, query) - result = ( - f'{format_number(conv.amount)} {conv.from_currency} = ' - f'{conv.conv_amount} {conv.conv_currency}' + from_currency, conv_currency = None, None + + for currency_code, aliases in currency_json.items(): + if from_currency_alias in aliases: + from_currency = currency_code + if conv_currency_alias in aliases: + conv_currency = currency_code + + if from_currency and conv_currency: + break + + if not from_currency or not conv_currency: + return await reply(result_id, + 'The currency exchange rate could not be found.', + None, + query) + + conv.from_currency = from_currency.upper() + conv.conv_currency = conv_currency.upper() + conv.convert() + + result = f'{format_number(conv.amount)} {conv.from_currency} = {conv.conv_amount} {conv.conv_currency}' + return await reply(result_id, result, None, query) + + except Exception as e: + print(e) + + +async def reply(result_id: str | None, title: str | None, desc, query: types.InlineQuery | types.Message) -> None: + if isinstance(query, types.InlineQuery): + article = [None] + article[0] = types.InlineQueryResultArticle( + id=result_id, + title=title, + description=desc, + input_message_content=types.InputTextMessageContent( + message_text=title + ) ) - except aiohttp.client_exceptions.ClientError: - await inline_reply(result_id, - "Rate-limit from the Telegram API, repeat the request later", - None, - inline_query) - - await asyncio.sleep(1) - - except Exception: - await inline_reply(result_id, "Invalid data format", - "@shirino_bot USD RUB \n@shirino_bot 12 USD RUB", - inline_query) - - await inline_reply(result_id, result, None, inline_query) + await query.answer( + article, + cache_time=1, + is_personal=True, + ) + else: + await query.answer(title) async def main() -> None: