Compare commits

...

3 commits

3 changed files with 31 additions and 20 deletions

View file

@ -1,14 +1,12 @@
from utils.format_number import format_number
import yaml
import aiohttp
import json
import re
from datetime import datetime
from decimal import Decimal
from http import HTTPStatus
from decimal import Decimal, ROUND_DOWN
import aiohttp
import yaml
from utils.format_number import format_number
config = yaml.safe_load(open('config.yaml'))
@ -24,8 +22,7 @@ class Converter:
if not await self.kekkai():
await self.ddg()
number = Decimal(str(self.conv_amount))
self.conv_amount = format_number(number.quantize(Decimal('1.0000'), rounding=ROUND_DOWN))
self.conv_amount = format_number(Decimal(self.conv_amount))
async def kekkai(self) -> bool:

30
main.py
View file

@ -11,6 +11,7 @@ from aiogram import Bot, Dispatcher, Router, types
from aiogram.client.default import DefaultBotProperties
from aiogram.enums import ParseMode
from aiogram.webhook.aiohttp_server import SimpleRequestHandler, setup_application
from aiogram.filters import CommandStart
import hashlib
@ -19,6 +20,19 @@ bot = Bot(token=config['telegram_token'], default=DefaultBotProperties(parse_mod
router = Router()
@router.message(CommandStart())
async def start(message: types.Message):
get_bot = await bot.get_me()
await message.reply(
'Shirino is a telegram bot for converting fiat or cryptocurrency. '
'The example of use occurs via inline query:\n'
f'`@{get_bot.username} USD RUB` \n'
f'`@{get_bot.username} 12 USD RUB` \n\n'
'[Source Code](https://github.com/Redume/Shirino)',
parse_mode='markdown'
)
@router.inline_query()
async def currency(query: types.InlineQuery) -> None:
text = query.query.lower()
@ -28,8 +42,8 @@ async def currency(query: types.InlineQuery) -> None:
get_bot = await bot.get_me()
if len(args) < 2:
return await reply(result_id,
[("2 or 3 arguments are required.",
return await reply(result_id,
[("2 or 3 arguments are required.",
f'@{get_bot.username} USD RUB \n'
f'@{get_bot.username} 12 USD RUB',
None, None)],
@ -50,7 +64,7 @@ async def currency(query: types.InlineQuery) -> None:
f'@{get_bot.username} USD RUB \n'
f'@{get_bot.username} 12 USD RUB',
None, None)], query)
from_currency = args[1]
conv_currency = args[2]
elif len(args) == 2:
@ -74,12 +88,12 @@ async def currency(query: types.InlineQuery) -> None:
chart = await create_chart(from_currency, conv_currency)
message = f'{format_number(conv.amount)} {conv.from_currency} = {conv.conv_amount} {conv.conv_currency}'
results = [(message, None, None)]
if chart:
results.insert(0, (f'{message}\n[График]({chart})', None, chart))
await reply(result_id, results, query)
@ -87,7 +101,7 @@ async def on_startup(bot: Bot) -> None:
await bot.set_webhook(
f"{config['webhook']['base_url']}{config['webhook']['path']}",
secret_token=config['webhook']['secret_token'],
allowed_updates=['inline_query']
allowed_updates=['inline_query', 'message']
)
@ -96,7 +110,7 @@ def main() -> None:
dp.include_router(router)
dp.startup.register(on_startup)
app = web.Application()
webhook_requests_handler = SimpleRequestHandler(
dispatcher=dp,

View file

@ -2,11 +2,11 @@ from decimal import Decimal
def format_number(number):
number = Decimal(str(number))
formatted_integer_part = '{:,.0f}'.format(number).replace(',', ' ')
if '.' in str(number):
fractional_part = str(number).split('.')[1]
fractional_part = str(number).split('.')[1][:3]
return formatted_integer_part + '.' + fractional_part
else:
return formatted_integer_part