mirror of
https://github.com/Redume/Shirino.git
synced 2024-11-05 16:23:59 +03:00
103 lines
3.2 KiB
Python
103 lines
3.2 KiB
Python
from aiogram import Bot, Dispatcher, types
|
|
|
|
import asyncio
|
|
import yaml
|
|
|
|
import hashlib
|
|
|
|
import json
|
|
from function.convert import Converter
|
|
from function.format_number import format_number
|
|
|
|
config = yaml.safe_load(open("config.yaml"))
|
|
dp = Dispatcher()
|
|
|
|
|
|
@dp.message()
|
|
@dp.inline_query()
|
|
async def currency(query: types.Message | types.InlineQuery) -> None:
|
|
global result
|
|
|
|
try:
|
|
currency_json = json.load(open('currency.json', 'r', encoding='utf-8'))
|
|
text = query.query if isinstance(query, types.InlineQuery) else query.text
|
|
args = text.split()
|
|
result_id = hashlib.md5(text.encode()).hexdigest()
|
|
conv = Converter()
|
|
|
|
if len(args) <= 1:
|
|
return await reply(result_id,
|
|
"2 or 3 arguments are required.",
|
|
"@shirino_bot USD RUB "
|
|
"\n@shirino_bot 12 USD RUB",
|
|
query)
|
|
if len(args) == 4:
|
|
conv.amount = float(args[0])
|
|
from_currency_alias = args[1].lower()
|
|
conv_currency_alias = args[3].lower()
|
|
elif len(args) == 3:
|
|
conv.amount = float(args[0])
|
|
from_currency_alias = args[1].lower()
|
|
conv_currency_alias = args[2].lower()
|
|
elif len(args) == 2:
|
|
from_currency_alias = args[0].lower()
|
|
conv_currency_alias = args[1].lower()
|
|
else:
|
|
return await reply(result_id, 'The source and target currency could not be determined.', None, query)
|
|
|
|
from_currency, conv_currency = None, None
|
|
|
|
for currency_code, aliases in currency_json.items():
|
|
if from_currency_alias in aliases:
|
|
from_currency = currency_code
|
|
if conv_currency_alias in aliases:
|
|
conv_currency = currency_code
|
|
|
|
if from_currency and conv_currency:
|
|
break
|
|
|
|
if not from_currency or not conv_currency:
|
|
return await reply(result_id,
|
|
'The currency exchange rate could not be found.',
|
|
None,
|
|
query)
|
|
|
|
conv.from_currency = from_currency.upper()
|
|
conv.conv_currency = conv_currency.upper()
|
|
conv.convert()
|
|
|
|
result = f'{format_number(conv.amount)} {conv.from_currency} = {conv.conv_amount} {conv.conv_currency}'
|
|
return await reply(result_id, result, None, query)
|
|
|
|
except Exception as e:
|
|
print(e)
|
|
|
|
|
|
async def reply(result_id: str | None, title: str | None, desc, query: types.InlineQuery | types.Message) -> None:
|
|
if isinstance(query, types.InlineQuery):
|
|
article = [None]
|
|
article[0] = types.InlineQueryResultArticle(
|
|
id=result_id,
|
|
title=title,
|
|
description=desc,
|
|
input_message_content=types.InputTextMessageContent(
|
|
message_text=title
|
|
)
|
|
)
|
|
|
|
await query.answer(
|
|
article,
|
|
cache_time=1,
|
|
is_personal=True,
|
|
)
|
|
else:
|
|
await query.answer(title)
|
|
|
|
|
|
async def main() -> None:
|
|
bot = Bot(config['telegram_token'])
|
|
await dp.start_polling(bot)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
asyncio.run(main())
|