From 212d30d867ccb9d9669574dedc490b453734e712 Mon Sep 17 00:00:00 2001 From: Redume Date: Fri, 29 Sep 2023 21:12:44 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9C=D0=B8=D0=B3=D1=80=D0=B0=D1=86=D0=B8?= =?UTF-8?q?=D1=8F=20aiogram=20=D1=81=20v2=20=D0=BD=D0=B0=20v3,=20=D0=B8?= =?UTF-8?q?=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20?= =?UTF-8?q?=D0=BF=D0=B0=D1=80=D1=81=D0=B8=D0=BD=D0=B3=D0=B0=20API=20=D0=BE?= =?UTF-8?q?=D1=82=20DuckDuckGo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/main.py b/main.py index b432045..03bfb9c 100644 --- a/main.py +++ b/main.py @@ -9,15 +9,11 @@ from typing import Any, Dict, List, Optional import requests -from pydantic import BaseSettings +from pydantic.v1 import BaseSettings -from aiogram import Bot # type: ignore -from aiogram.dispatcher import Dispatcher # type: ignore -from aiogram.utils import executor # type: ignore +from aiogram import Bot, Dispatcher, types # type: ignore -from aiogram.types import InlineQuery # type: ignore -from aiogram.types import InlineQueryResultArticle -from aiogram.types import InputTextMessageContent +import asyncio # Constants @@ -59,8 +55,7 @@ if settings.debug: coinapi_len = len(settings.coinapi_keys) coinapi_active = [0] # API key index -bot = Bot(token=settings.telegram_token) -dp = Dispatcher(bot) +dp = Dispatcher() class CurrencyConverter: @@ -79,7 +74,7 @@ class CurrencyConverter: self.coinapi() str_amount = f'{self.conv_amount}' - point = str_amount.find(".") + point = str_amount.find('.') after_point = str_amount[point + 1:] fnz = min( # index of first non-zero digit @@ -130,15 +125,16 @@ class CurrencyConverter: log.debug(data) # If the currency does not exist - descr = data.get('headers', {}).get('description', '') - if descr.find('ERROR') != -1: + error = data.get('to')[0].get('quotecurrency') + if error is None: return False # Otherwise - conv: Dict[str, str] = data.get('conversion', {}) - conv_amount = conv.get('converted-amount') + conv: Dict[str, str] = data.get('to')[0] + conv_amount = conv.get('mid') if conv_amount is None: raise RuntimeError('Ошибка при конвертации через DDG') + self.conv_amount = float(conv_amount) log.debug(conv) @@ -189,11 +185,11 @@ def rotate_token(lst: List[str], active: List[int]) -> None: active[0] = (active[0] + 1) % len(lst) -@dp.inline_handler() -async def currency(inline_query: InlineQuery) -> None: +@dp.inline_query() +async def currency(inline_query: types.InlineQuery) -> None: query = inline_query.query - article: List[Optional[InlineQueryResultArticle]] = [None] + article: List[Optional[types.InlineQueryResultArticle]] = [None] text = query.split() len_ = len(text) @@ -222,10 +218,10 @@ async def currency(inline_query: InlineQuery) -> None: except Exception as ex: result = f'{type(ex).__name__}: {ex}' - article[0] = InlineQueryResultArticle( + article[0] = types.InlineQueryResultArticle( id=result_id, title=result, - input_message_content=InputTextMessageContent( + input_message_content=types.InputTextMessageContent( message_text=result, ), ) @@ -237,4 +233,10 @@ async def currency(inline_query: InlineQuery) -> None: ) -executor.start_polling(dp, skip_updates=True) +async def main() -> None: + bot = Bot(settings.telegram_token) + await dp.start_polling(bot) + + +if __name__ == '__main__': + asyncio.run(main())