feat: If the charts API does not work, the suggestion will be removed from the responses

This commit is contained in:
Данил 2024-11-28 21:50:52 +03:00
parent a89362205a
commit 4fbc2c6571

93
main.py
View file

@ -2,16 +2,13 @@ from aiogram import Bot, Dispatcher, types
import yaml
import asyncio
import requests
import hashlib
import json
from http import HTTPStatus
import re
from function.convert import Converter
from function.format_number import format_number
from function.get_chart import get_chart
from utils.convert import Converter
from utils.format_number import format_number
from function.inline_query import reply
dp = Dispatcher()
config = yaml.safe_load(open('config.yaml'))
@ -43,7 +40,15 @@ async def currency(query: types.InlineQuery) -> None:
from_currency = args[0]
conv_currency = args[1]
else:
return await reply(result_id, [('The source and target currency could not be determined.', None, None)], query)
return await reply(result_id,
[
(
'The source and target currency could not be determined.',
None,
None
)
],
query)
if not from_currency or not conv_currency:
return await reply(result_id, [('The currency exchange rate could not be found.', None, None)], query)
@ -52,26 +57,33 @@ async def currency(query: types.InlineQuery) -> None:
conv.conv_currency = conv_currency.upper()
conv.convert()
req_chart = requests.get(f'{config['kekkai_instance']}/api/getChart/week/', {
'from_currency': from_currency,
'conv_currency': conv_currency
}, timeout=3)
chart = get_chart(from_currency, conv_currency)
if not HTTPStatus(req_chart.status_code).is_success:
req_chart = None
else:
req_chart = req_chart.json().get('message', None)
if not chart:
return await reply(result_id,
[
(
f'{format_number(conv.amount)} {conv.from_currency} '
f'= {conv.conv_amount} {conv.conv_currency}' \
f'\n{f'[График]({chart})' if chart else ''}',
None,
chart
)
],
query)
await reply(result_id,
[
(
f'{format_number(conv.amount)} {conv.from_currency} = {conv.conv_amount} {conv.conv_currency}' \
f'\n{f'[График]({req_chart})' if req_chart else ''}',
f'{format_number(conv.amount)} {conv.from_currency} '
f'= {conv.conv_amount} {conv.conv_currency}' \
f'\n{f'[График]({chart})' if chart else ''}',
None,
req_chart
chart
),
(
f'{format_number(conv.amount)} {conv.from_currency} = {conv.conv_amount} {conv.conv_currency}',
f'{format_number(conv.amount)} {conv.from_currency} '
f'= {conv.conv_amount} {conv.conv_currency}',
None,
None
)
@ -82,47 +94,6 @@ async def currency(query: types.InlineQuery) -> None:
print(e)
async def reply(result_id: str, args: list, query: types.InlineQuery) -> None:
if not args:
return
articles = []
for idx, arg in enumerate(args):
title = arg[0]
description = arg[1] if arg[1] else None
img = arg[2] if arg[2] else None
article = types.InlineQueryResultArticle(
id=f"{result_id}_{idx}",
title=remove_markdown(title).replace('График', ''),
thumbnail_url=img,
description=description,
input_message_content=types.InputTextMessageContent(
message_text=title,
parse_mode='markdown'
)
)
articles.append(article)
await query.answer(
results=articles,
parse_mode='markdown',
cache_time=0,
is_personal=True
)
def remove_markdown(text: str) -> str:
text = re.sub(r'\*([^\*]+)\*', r'\1', text)
text = re.sub(r'\_([^\_]+)\_', r'\1', text)
text = re.sub(r'\[([^\[]+)\]\([^\)]+\)', r'\1', text)
text = re.sub(r'[`]+([^`]+)`+', r'\1', text)
text = re.sub(r'~~([^~]+)~~', r'\1', text)
text = re.sub(r'\[([^\[]+)\]\([^\)]+\)', '', text)
return text
async def main() -> None:
bot = Bot(config['telegram_token'])