diff --git a/src/actions/__init__.py b/src/actions/__init__.py index 147caf4fe4229ba8180a17528bd38c10bd994bb3..e7d88e2495451cfd15a39f706e98e62ff54cb64a 100644 --- a/src/actions/__init__.py +++ b/src/actions/__init__.py @@ -6,6 +6,7 @@ def finish(): importlib.import_module("src.actions.basic") importlib.import_module("src.actions.fun") importlib.import_module("src.actions.replies") + importlib.import_module("src.actions.converter") importlib.import_module("src.actions.package_indexes") importlib.import_module("src.actions.search_engines") logging.info("All actions loaded") diff --git a/src/actions/converter.py b/src/actions/converter.py new file mode 100644 index 0000000000000000000000000000000000000000..887647080b182d3c5d0139bb7202a22ef20c5ac2 --- /dev/null +++ b/src/actions/converter.py @@ -0,0 +1,56 @@ +import logging +import re + +import httpx +from src.config import config +from src.utils.responses import mark_unread +from telethon import events + +logger = logging.getLogger(__name__) + +SUPPORTED_CURRENCIES = [ + "GBP", + "HUF", + "USD", + "EUR", + "CNY", + "NOK", + "UAH", + "SEK", + "CHF", + "KRW", + "JPY", +] +CURRENCY_PATTERN = rf"\s*((\d+)(\.\d+)?)\s+({'|'.join(SUPPORTED_CURRENCIES)})" + + +@config.telegram_client.on(events.NewMessage(pattern=f".*{CURRENCY_PATTERN}.*")) +@mark_unread +async def replace_currency(event): + async with httpx.AsyncClient() as f: + current_currency = await f.get("https://www.cbr-xml-daily.ru/daily_json.js") + response_json = current_currency.json() + currencies = response_json["Valute"] + groups = re.findall(CURRENCY_PATTERN, event.message.message) + response = "**ПолагаÑÑÑŒ на текщий ÐºÑƒÑ€Ñ Ð²Ð°Ð»ÑŽÑ‚ могу Ñказать Ñледующее:**\n\n" + for group in groups: + name = group[3] + val = float(group[0]) + new_val = val * currencies[name]["Value"] + response += f"`{val:.2f} {name} = {new_val:.2f} RUB`\n" + + await event.reply(response) + + +@config.telegram_client.on(events.NewMessage(pattern="^.sc$")) +@mark_unread +async def supported_currencies(event): + async with httpx.AsyncClient() as f: + current_currency = await f.get("https://www.cbr-xml-daily.ru/daily_json.js") + response_json = current_currency.json() + currencies = response_json["Valute"] + response = "**Поддерживаемые курÑÑ‹ валют**:\n\n" + for curr in SUPPORTED_CURRENCIES: + response += f"{curr} ({currencies[curr]['Name']})\n" + + await event.reply(response) diff --git a/static_messages/help.txt b/static_messages/help.txt index 6d11802c5a1d79e167a612e867d8a8ac37855ae1..8a1852004d8ff9d5d93a61fdb33cebe6222c7ab4 100644 --- a/static_messages/help.txt +++ b/static_messages/help.txt @@ -13,6 +13,7 @@ * `.brew` -- Ð´Ð»Ñ MacOs Также умею ещё пару забавных штук. +`.sc` поÑмотреть поддерживаемые курÑÑ‹ валют Ð´Ð»Ñ Ð°Ð²Ñ‚Ð¾Ð¼Ð°Ñ‚Ð¸Ñ‡ÐµÑкой конвертации. `.i <запроÑ>` вернет пачку картинок Ñ Ð¸Ð½Ñ‚ÐµÑ€Ð½ÐµÑ‚Ð¸ÐºÐ¾Ð². * `.bl` <любой текÑÑ‚> - заменит вÑе запÑтые на вÑÑкие гадоÑти. * `.t` - показать текущее времÑ