chore: Using webhooks

This commit is contained in:
Данил 2025-01-08 13:55:59 +03:00
parent df8fae1d7f
commit ed493b862e

125
main.py
View file

@ -1,104 +1,57 @@
from aiogram import Bot, Dispatcher, types import logging
import sys
import yaml import yaml
import asyncio
import hashlib from aiohttp import web
from function.get_chart import get_chart from aiogram import Bot, Dispatcher, Router
from utils.convert import Converter from aiogram.client.default import DefaultBotProperties
from utils.format_number import format_number from aiogram.enums import ParseMode
from function.inline_query import reply from aiogram.filters import CommandStart
from aiogram.types import Message
from aiogram.utils.markdown import hbold
from aiogram.webhook.aiohttp_server import SimpleRequestHandler, setup_application
dp = Dispatcher()
config = yaml.safe_load(open('config.yaml')) config = yaml.safe_load(open('config.yaml'))
@dp.inline_query() router = Router()
async def currency(query: types.InlineQuery) -> None:
global from_currency, conv_currency
@router.message()
async def echo_handler(message: Message) -> None:
try: try:
text = query.query.lower() # Send a copy of the received message
args = text.split() await message.send_copy(chat_id=message.chat.id)
result_id = hashlib.md5(text.encode()).hexdigest() except TypeError:
# But not all the types is supported to be copied so need to handle it
await message.answer("Nice try!")
conv = Converter() async def on_startup(bot: Bot) -> None:
# Убедитесь, что передаете HTTPS URL
await bot.set_webhook(f"{config['webhook']['base_url']}{config['webhook']['path']}", secret_token=config['webhook']['secret_token'])
if len(args) < 2:
return await reply(result_id,
[("2 or 3 arguments are required.",
'@shirino_bot USD RUB \n'
'@shirino_bot 12 USD RUB',
None, None)],
query)
if len(args) == 3: def main() -> None:
conv.amount = float(args[0].replace(',', '.')) dp = Dispatcher()
from_currency = args[1]
conv_currency = args[2] dp.include_router(router)
elif len(args) == 2: dp.startup.register(on_startup)
from_currency = args[0]
conv_currency = args[1] bot = Bot(token=config['telegram_token'], default=DefaultBotProperties(parse_mode=ParseMode.HTML))
else:
return await reply(result_id, app = web.Application()
[ webhook_requests_handler = SimpleRequestHandler(
( dispatcher=dp,
'The source and target currency could not be determined.', bot=bot,
None, secret_token=config['webhook']['secret_token']
None
) )
], webhook_requests_handler.register(app, path=config['webhook']['path'])
query)
if not from_currency or not conv_currency: setup_application(app, dp, bot=bot)
return await reply(result_id, [('The currency exchange rate could not be found.', None, None)], query)
conv.from_currency = from_currency.upper() web.run_app(app, host='127.0.0.1', port=8080)
conv.conv_currency = conv_currency.upper()
conv.convert()
chart = get_chart(from_currency, conv_currency)
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} '
f'= {conv.conv_amount} {conv.conv_currency}' \
f'\n{f'[График]({chart})' if chart else ''}',
None,
chart
),
(
f'{format_number(conv.amount)} {conv.from_currency} '
f'= {conv.conv_amount} {conv.conv_currency}',
None,
None
)
],
query)
except Exception as e:
print(e)
async def main() -> None:
bot = Bot(config['telegram_token'])
await dp.start_polling(bot)
if __name__ == '__main__': if __name__ == '__main__':
asyncio.run(main()) logging.basicConfig(level=logging.INFO, stream=sys.stdout)
main()