Переделаны ответы, меньше говнокода

This commit is contained in:
Данил 2023-12-31 14:12:13 +03:00
parent f13ff07de7
commit bc9098cd14

63
main.py
View file

@ -13,6 +13,7 @@ import requests
from aiogram import Dispatcher, types, Bot from aiogram import Dispatcher, types, Bot
import aiohttp.client_exceptions import aiohttp.client_exceptions
from pydantic.v1 import BaseSettings from pydantic.v1 import BaseSettings
from selenium.webdriver.common.actions import interaction
# Constants # Constants
DDG_URL = 'https://duckduckgo.com/js/spice/currency' DDG_URL = 'https://duckduckgo.com/js/spice/currency'
@ -172,25 +173,14 @@ def rotate_token(lst: List[str], active: List[int]) -> None:
active[0] = (active[0] + 1) % len(lst) active[0] = (active[0] + 1) % len(lst)
@dp.inline_query() async def inline_reply(result_id: str, title: str, description: str or None, inline_query: types.InlineQuery) -> None:
async def currency(inline_query: types.InlineQuery) -> None:
query = inline_query.query
article: List[Optional[types.InlineQueryResultArticle]] = [None] article: List[Optional[types.InlineQueryResultArticle]] = [None]
args = query.split()
result_id = hashlib.md5(query.encode()).hexdigest()
conv = CurrencyConverter()
try:
log.debug(len(args))
if len(args) <= 1:
article[0] = types.InlineQueryResultArticle( article[0] = types.InlineQueryResultArticle(
id=result_id, id=result_id,
title="Требуется 2, либо 3 аргумента", title=title,
description=f"@shirino_bot USD RUB \n@shirino_bot 12 USD RUB", description=description,
input_message_content=types.InputTextMessageContent( input_message_content=types.InputTextMessageContent(
message_text="Требуется 2, либо 3 аргумента" message_text=title
) )
) )
@ -199,6 +189,23 @@ async def currency(inline_query: types.InlineQuery) -> None:
cache_time=1, cache_time=1,
is_personal=True, is_personal=True,
) )
@dp.inline_query()
async def currency(inline_query: types.InlineQuery) -> None:
query = inline_query.query
args = query.split()
result_id = hashlib.md5(query.encode()).hexdigest()
conv = CurrencyConverter()
try:
log.debug(len(args))
if len(args) <= 1:
await inline_reply(result_id,
"Требуется 2, либо 3 аргумента",
f"@shirino_bot USD RUB \n@shirino_bot 12 USD RUB", inline_query)
if len(args) == 3: if len(args) == 3:
conv.amount = float(args[0]) conv.amount = float(args[0])
conv.from_currency = args[1].upper() conv.from_currency = args[1].upper()
@ -215,13 +222,11 @@ async def currency(inline_query: types.InlineQuery) -> None:
) )
except aiohttp.client_exceptions.ClientError as ex: except aiohttp.client_exceptions.ClientError as ex:
article[0] = types.InlineQueryResultArticle( await inline_reply(result,
id=result_id, "Rate-limit от API Telegram, повторите запрос позже",
title="Rate-limit от API Telegram, повторите запрос позже.", None,
input_message_content=types.InputTextMessageContent( inline_query)
message_text="Rate-limit от API Telegram, повторите запрос позже.",
)
)
log.debug(ex) log.debug(ex)
await asyncio.sleep(1) await asyncio.sleep(1)
@ -229,19 +234,7 @@ async def currency(inline_query: types.InlineQuery) -> None:
log.debug(ex) log.debug(ex)
result = f'{type(ex).__name__}: {ex}' result = f'{type(ex).__name__}: {ex}'
article[0] = types.InlineQueryResultArticle( await inline_reply(result, result, None, inline_query)
id=result_id,
title=result,
input_message_content=types.InputTextMessageContent(
message_text=result,
),
)
await inline_query.answer(
article, # type: ignore
cache_time=1,
is_personal=True,
)
async def main() -> None: async def main() -> None: