mirror of
https://github.com/Redume/Shirino.git
synced 2024-11-22 00:06:22 +03:00
feat: Added selection of currency rate with and without chart
This commit is contained in:
parent
6d9dfac41a
commit
75e7555e50
1 changed files with 72 additions and 68 deletions
132
main.py
132
main.py
|
@ -1,123 +1,127 @@
|
||||||
from aiogram import Bot, Dispatcher, types
|
from aiogram import Bot, Dispatcher, types
|
||||||
|
|
||||||
import asyncio
|
|
||||||
import yaml
|
import yaml
|
||||||
|
import asyncio
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
import hashlib
|
import hashlib
|
||||||
import json
|
import json
|
||||||
|
|
||||||
from http import HTTPStatus
|
from http import HTTPStatus
|
||||||
|
import re
|
||||||
|
|
||||||
from function.convert import Converter
|
from function.convert import Converter
|
||||||
from function.format_number import format_number
|
from function.format_number import format_number
|
||||||
|
|
||||||
config = yaml.safe_load(open("config.yaml"))
|
|
||||||
dp = Dispatcher()
|
dp = Dispatcher()
|
||||||
|
config = yaml.safe_load(open('config.yaml'))
|
||||||
|
|
||||||
|
|
||||||
@dp.message()
|
|
||||||
@dp.inline_query()
|
@dp.inline_query()
|
||||||
async def currency(query: types.Message | types.InlineQuery) -> None:
|
async def currency(query: types.InlineQuery) -> None:
|
||||||
global result, from_currency, conv_currency
|
global result, from_currency, conv_currency
|
||||||
|
|
||||||
try:
|
try:
|
||||||
text = query.query if isinstance(query, types.InlineQuery) else query.text
|
text = query.query.lower()
|
||||||
args = text.split()
|
args = text.split()
|
||||||
result_id = hashlib.md5(text.encode()).hexdigest()
|
result_id = hashlib.md5(text.encode()).hexdigest()
|
||||||
|
|
||||||
conv = Converter()
|
conv = Converter()
|
||||||
|
|
||||||
if len(args) <= 1:
|
if len(args) < 2:
|
||||||
try:
|
|
||||||
if query.chat.type in ['supergroup', 'group']:
|
|
||||||
return
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
|
|
||||||
return await reply(result_id,
|
return await reply(result_id,
|
||||||
"2 or 3 arguments are required.",
|
[("2 or 3 arguments are required.",
|
||||||
"@shirino_bot USD RUB "
|
'@shirino_bot USD RUB \n'
|
||||||
"\n@shirino_bot 12 USD RUB",
|
'@shirino_bot 12 USD RUB')],
|
||||||
None,
|
|
||||||
query)
|
query)
|
||||||
if len(args) == 4:
|
|
||||||
conv.amount = float(args[0])
|
|
||||||
from_currency = args[1].lower()
|
|
||||||
conv_currency = args[3].lower()
|
|
||||||
elif len(args) == 3:
|
|
||||||
conv.amount = float(args[0])
|
|
||||||
from_currency = args[1].lower()
|
|
||||||
conv_currency = args[2].lower()
|
|
||||||
elif len(args) == 2:
|
|
||||||
from_currency = args[0].lower()
|
|
||||||
conv_currency = args[1].lower()
|
|
||||||
else:
|
|
||||||
try:
|
|
||||||
if query.chat.type in ['supergroup', 'group']:
|
|
||||||
return
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
|
|
||||||
return await reply(result_id, 'The source and target currency could not be determined.', None, None, query)
|
if len(args) == 3:
|
||||||
|
conv.amount = float(args[0])
|
||||||
|
from_currency = args[1]
|
||||||
|
conv_currency = args[2]
|
||||||
|
elif len(args) == 2:
|
||||||
|
from_currency = args[0]
|
||||||
|
conv_currency = args[1]
|
||||||
|
else:
|
||||||
|
return await reply(result_id, [('The source and target currency could not be determined.')], query)
|
||||||
|
|
||||||
if not from_currency or not conv_currency:
|
if not from_currency or not conv_currency:
|
||||||
return await reply(result_id,
|
return await reply(result_id, [('The currency exchange rate could not be found.')], query)
|
||||||
'The currency exchange rate could not be found.',
|
|
||||||
None,
|
|
||||||
None,
|
|
||||||
query)
|
|
||||||
|
|
||||||
conv.from_currency = from_currency.upper()
|
conv.from_currency = from_currency.upper()
|
||||||
conv.conv_currency = conv_currency.upper()
|
conv.conv_currency = conv_currency.upper()
|
||||||
conv.convert()
|
conv.convert()
|
||||||
|
|
||||||
res_chart = requests.get(f'{config['kekkai_instance']}/api/getChart/week/', {
|
req_chart = requests.get(f'{config['kekkai_instance']}/api/getChart/week/', {
|
||||||
'from_currency': from_currency,
|
'from_currency': from_currency,
|
||||||
'conv_currency': conv_currency
|
'conv_currency': conv_currency
|
||||||
}, timeout=3)
|
}, timeout=3)
|
||||||
|
|
||||||
if not HTTPStatus(res_chart.status_code).is_success:
|
if not HTTPStatus(req_chart.status_code).is_success:
|
||||||
res_chart = None
|
req_chart = None
|
||||||
else:
|
else:
|
||||||
res_chart = res_chart.json()['message']
|
req_chart = req_chart.json().get('message', None)
|
||||||
|
|
||||||
result = f'{format_number(conv.amount)} {conv.from_currency} = {conv.conv_amount} {conv.conv_currency}' \
|
await reply(result_id,
|
||||||
f'\n{f'[График]({res_chart})' if res_chart else ''}'
|
[
|
||||||
return await reply(result_id, result, None, res_chart, query)
|
(
|
||||||
|
f'{format_number(conv.amount)} {conv.from_currency} = {conv.conv_amount} {conv.conv_currency}' \
|
||||||
|
f'\n{f'[График]({req_chart})' if req_chart else ''}',
|
||||||
|
None,
|
||||||
|
req_chart
|
||||||
|
),
|
||||||
|
(
|
||||||
|
f'{format_number(conv.amount)} {conv.from_currency} = {conv.conv_amount} {conv.conv_currency}',
|
||||||
|
None,
|
||||||
|
None
|
||||||
|
)
|
||||||
|
],
|
||||||
|
query)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(e)
|
print(e)
|
||||||
|
|
||||||
|
|
||||||
async def reply(
|
async def reply(result_id: str, args: list, query: types.InlineQuery) -> None:
|
||||||
result_id: str | None,
|
if not args:
|
||||||
title: str,
|
return
|
||||||
desc: str | None,
|
|
||||||
img: str | None,
|
|
||||||
query: types.InlineQuery | types.Message,
|
|
||||||
) -> None:
|
|
||||||
|
|
||||||
if isinstance(query, types.InlineQuery):
|
articles = []
|
||||||
article = [None]
|
|
||||||
article[0] = types.InlineQueryResultArticle(
|
for idx, arg in enumerate(args):
|
||||||
id=result_id,
|
title = arg[0]
|
||||||
title=title,
|
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,
|
thumbnail_url=img,
|
||||||
description=desc,
|
description=description,
|
||||||
input_message_content=types.InputTextMessageContent(
|
input_message_content=types.InputTextMessageContent(
|
||||||
message_text=title,
|
message_text=title,
|
||||||
parse_mode='markdown'
|
parse_mode='markdown'
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
articles.append(article)
|
||||||
|
|
||||||
await query.answer(
|
await query.answer(
|
||||||
article,
|
results=articles,
|
||||||
parse_mode='markdown',
|
parse_mode='markdown',
|
||||||
cache_time=0,
|
cache_time=0,
|
||||||
is_personal=True,
|
is_personal=True
|
||||||
)
|
)
|
||||||
else:
|
|
||||||
await query.answer(f'{title} \n{'' if desc else ''}')
|
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:
|
async def main() -> None:
|
||||||
bot = Bot(config['telegram_token'])
|
bot = Bot(config['telegram_token'])
|
||||||
|
|
Loading…
Add table
Reference in a new issue