mirror of
https://github.com/Redume/Shirino.git
synced 2024-11-22 00:06:22 +03:00
Compare commits
No commits in common. "6d9dfac41a285f3265458ef4144c1f8aedb35a5c" and "69bd88787831e976afe6ec30e308725c5cd45e24" have entirely different histories.
6d9dfac41a
...
69bd887878
4 changed files with 37 additions and 48 deletions
|
@ -1,11 +1,6 @@
|
||||||
.git/
|
.git/
|
||||||
.vscode/
|
.vscode/
|
||||||
.idea/
|
.idea/
|
||||||
.github/
|
|
||||||
|
|
||||||
.DS_Store
|
.DS_Store
|
||||||
__pycache__
|
__pycache__
|
||||||
|
|
||||||
.mypy_cache
|
|
||||||
mypy.ini
|
|
||||||
pylintrc
|
|
|
@ -4,4 +4,3 @@ coinapi_keys:
|
||||||
- key
|
- key
|
||||||
- key2 # coinapi keys list
|
- key2 # coinapi keys list
|
||||||
telegram_token: # telegram bot token
|
telegram_token: # telegram bot token
|
||||||
kekkai_instance: 'https://kekkai-api.redume.su/'
|
|
8
currency.json
Normal file
8
currency.json
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
{
|
||||||
|
"RUB": ["руб", "рубли", "рубля", "рублей", "рубль", "российский рубль", "rub", "rouble", "roubles", "russian rouble"],
|
||||||
|
"USD": ["доллары", "доллар", "доллара", "долларах", "долларов", "зеленых", "бакса", "баксов", "usd", "dollar", "dollars"],
|
||||||
|
"UAH": ["гривны", "гривен", "гривен", "hryvnia"],
|
||||||
|
"EUR": ["евро", "eur", "euro", "euros"],
|
||||||
|
"KZT": ["тенге", "казахстанский тенге", "kazakhstani tenge", "tenge"],
|
||||||
|
"TRY": ["турецкая лира", "турецких лир", "try", "tl", "Turkish lira", "lira"]
|
||||||
|
}
|
69
main.py
69
main.py
|
@ -2,13 +2,10 @@ from aiogram import Bot, Dispatcher, types
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
import yaml
|
import yaml
|
||||||
import requests
|
|
||||||
|
|
||||||
import hashlib
|
import hashlib
|
||||||
|
|
||||||
import json
|
import json
|
||||||
|
|
||||||
from http import HTTPStatus
|
|
||||||
|
|
||||||
from function.convert import Converter
|
from function.convert import Converter
|
||||||
from function.format_number import format_number
|
from function.format_number import format_number
|
||||||
|
|
||||||
|
@ -19,9 +16,10 @@ dp = Dispatcher()
|
||||||
@dp.message()
|
@dp.message()
|
||||||
@dp.inline_query()
|
@dp.inline_query()
|
||||||
async def currency(query: types.Message | types.InlineQuery) -> None:
|
async def currency(query: types.Message | types.InlineQuery) -> None:
|
||||||
global result, from_currency, conv_currency
|
global result, from_currency_alias, conv_currency_alias
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
currency_json = json.load(open('currency.json', 'r', encoding='utf-8'))
|
||||||
text = query.query if isinstance(query, types.InlineQuery) else query.text
|
text = query.query if isinstance(query, types.InlineQuery) else query.text
|
||||||
args = text.split()
|
args = text.split()
|
||||||
result_id = hashlib.md5(text.encode()).hexdigest()
|
result_id = hashlib.md5(text.encode()).hexdigest()
|
||||||
|
@ -38,19 +36,18 @@ async def currency(query: types.Message | types.InlineQuery) -> None:
|
||||||
"2 or 3 arguments are required.",
|
"2 or 3 arguments are required.",
|
||||||
"@shirino_bot USD RUB "
|
"@shirino_bot USD RUB "
|
||||||
"\n@shirino_bot 12 USD RUB",
|
"\n@shirino_bot 12 USD RUB",
|
||||||
None,
|
|
||||||
query)
|
query)
|
||||||
if len(args) == 4:
|
if len(args) == 4:
|
||||||
conv.amount = float(args[0])
|
conv.amount = float(args[0])
|
||||||
from_currency = args[1].lower()
|
from_currency_alias = args[1].lower()
|
||||||
conv_currency = args[3].lower()
|
conv_currency_alias = args[3].lower()
|
||||||
elif len(args) == 3:
|
elif len(args) == 3:
|
||||||
conv.amount = float(args[0])
|
conv.amount = float(args[0])
|
||||||
from_currency = args[1].lower()
|
from_currency_alias = args[1].lower()
|
||||||
conv_currency = args[2].lower()
|
conv_currency_alias = args[2].lower()
|
||||||
elif len(args) == 2:
|
elif len(args) == 2:
|
||||||
from_currency = args[0].lower()
|
from_currency_alias = args[0].lower()
|
||||||
conv_currency = args[1].lower()
|
conv_currency_alias = args[1].lower()
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
if query.chat.type in ['supergroup', 'group']:
|
if query.chat.type in ['supergroup', 'group']:
|
||||||
|
@ -58,66 +55,56 @@ async def currency(query: types.Message | types.InlineQuery) -> None:
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
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, 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:
|
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.',
|
'The currency exchange rate could not be found.',
|
||||||
None,
|
None,
|
||||||
None,
|
|
||||||
query)
|
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/', {
|
result = f'{format_number(conv.amount)} {conv.from_currency} = {conv.conv_amount} {conv.conv_currency}'
|
||||||
'from_currency': from_currency,
|
return await reply(result_id, result, None, query)
|
||||||
'conv_currency': conv_currency
|
|
||||||
}, timeout=3)
|
|
||||||
|
|
||||||
if not HTTPStatus(res_chart.status_code).is_success:
|
|
||||||
res_chart = None
|
|
||||||
else:
|
|
||||||
res_chart = res_chart.json()['message']
|
|
||||||
|
|
||||||
result = f'{format_number(conv.amount)} {conv.from_currency} = {conv.conv_amount} {conv.conv_currency}' \
|
|
||||||
f'\n{f'[График]({res_chart})' if res_chart else ''}'
|
|
||||||
return await reply(result_id, result, None, res_chart, query)
|
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(e)
|
print(e)
|
||||||
|
|
||||||
|
|
||||||
async def reply(
|
async def reply(result_id: str | None, title: str | None, desc, query: types.InlineQuery | types.Message) -> None:
|
||||||
result_id: str | None,
|
|
||||||
title: str,
|
|
||||||
desc: str | None,
|
|
||||||
img: str | None,
|
|
||||||
query: types.InlineQuery | types.Message,
|
|
||||||
) -> None:
|
|
||||||
|
|
||||||
if isinstance(query, types.InlineQuery):
|
if isinstance(query, types.InlineQuery):
|
||||||
article = [None]
|
article = [None]
|
||||||
article[0] = types.InlineQueryResultArticle(
|
article[0] = types.InlineQueryResultArticle(
|
||||||
id=result_id,
|
id=result_id,
|
||||||
title=title,
|
title=title,
|
||||||
thumbnail_url=img,
|
|
||||||
description=desc,
|
description=desc,
|
||||||
input_message_content=types.InputTextMessageContent(
|
input_message_content=types.InputTextMessageContent(
|
||||||
message_text=title,
|
message_text=title
|
||||||
parse_mode='markdown'
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
await query.answer(
|
await query.answer(
|
||||||
article,
|
article,
|
||||||
parse_mode='markdown',
|
cache_time=1,
|
||||||
cache_time=0,
|
|
||||||
is_personal=True,
|
is_personal=True,
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
await query.answer(f'{title} \n{'' if desc else ''}')
|
await query.answer(f'{title} \n{desc}')
|
||||||
|
|
||||||
|
|
||||||
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