Shirino/main.py

58 lines
1.7 KiB
Python
Raw Normal View History

2025-01-08 13:55:59 +03:00
import logging
import sys
import yaml
2024-08-25 22:16:23 +03:00
2025-01-08 13:55:59 +03:00
from aiohttp import web
2024-10-29 20:05:50 +03:00
2025-01-08 13:55:59 +03:00
from aiogram import Bot, Dispatcher, Router
from aiogram.client.default import DefaultBotProperties
from aiogram.enums import ParseMode
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
2023-03-08 13:31:45 +03:00
config = yaml.safe_load(open('config.yaml'))
2023-03-08 13:31:45 +03:00
2025-01-08 13:55:59 +03:00
router = Router()
2023-03-08 13:31:45 +03:00
2025-01-08 13:55:59 +03:00
@router.message()
async def echo_handler(message: Message) -> None:
2023-03-08 13:31:45 +03:00
try:
2025-01-08 13:55:59 +03:00
# Send a copy of the received message
await message.send_copy(chat_id=message.chat.id)
except TypeError:
# But not all the types is supported to be copied so need to handle it
await message.answer("Nice try!")
2025-01-08 13:55:59 +03:00
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'])
2025-01-08 13:55:59 +03:00
def main() -> None:
dp = Dispatcher()
2025-01-08 13:55:59 +03:00
dp.include_router(router)
dp.startup.register(on_startup)
bot = Bot(token=config['telegram_token'], default=DefaultBotProperties(parse_mode=ParseMode.HTML))
2025-01-08 13:55:59 +03:00
app = web.Application()
webhook_requests_handler = SimpleRequestHandler(
dispatcher=dp,
bot=bot,
secret_token=config['webhook']['secret_token']
)
webhook_requests_handler.register(app, path=config['webhook']['path'])
2025-01-08 13:55:59 +03:00
setup_application(app, dp, bot=bot)
web.run_app(app, host='0.0.0.0', port=443)
if __name__ == '__main__':
2025-01-08 13:55:59 +03:00
logging.basicConfig(level=logging.INFO, stream=sys.stdout)
main()