mirror of
https://github.com/aiogram/aiogram.git
synced 2026-04-08 16:37:47 +00:00
Refactor examples/webhook_example.py
This commit is contained in:
parent
251760a032
commit
58a5da925a
1 changed files with 30 additions and 6 deletions
|
|
@ -1,11 +1,13 @@
|
|||
import asyncio
|
||||
import logging
|
||||
|
||||
from aiogram import Bot, types
|
||||
from aiogram.contrib.middlewares.logging import LoggingMiddleware
|
||||
from aiogram.dispatcher import Dispatcher
|
||||
from aiogram.dispatcher.webhook import SendMessage
|
||||
from aiogram.utils.executor import start_webhook
|
||||
|
||||
API_TOKEN = 'BOT TOKEN HERE'
|
||||
|
||||
API_TOKEN = 'BOT_TOKEN_HERE'
|
||||
|
||||
# webhook settings
|
||||
WEBHOOK_HOST = 'https://your.domain'
|
||||
|
|
@ -20,11 +22,16 @@ logging.basicConfig(level=logging.INFO)
|
|||
|
||||
bot = Bot(token=API_TOKEN)
|
||||
dp = Dispatcher(bot)
|
||||
dp.middleware.setup(LoggingMiddleware())
|
||||
|
||||
|
||||
@dp.message_handler()
|
||||
async def echo(message: types.Message):
|
||||
await bot.send_message(message.chat.id, message.text)
|
||||
# Regular request
|
||||
# await bot.send_message(message.chat.id, message.text)
|
||||
|
||||
# or reply INTO webhook
|
||||
return SendMessage(message.chat.id, message.text)
|
||||
|
||||
|
||||
async def on_startup(dp):
|
||||
|
|
@ -33,10 +40,27 @@ async def on_startup(dp):
|
|||
|
||||
|
||||
async def on_shutdown(dp):
|
||||
logging.warning('Shutting down..')
|
||||
|
||||
# insert code here to run it before shutdown
|
||||
pass
|
||||
|
||||
# Remove webhook (not acceptable in some cases)
|
||||
await bot.delete_webhook()
|
||||
|
||||
# Close DB connection (if used)
|
||||
await dp.storage.close()
|
||||
await dp.storage.wait_closed()
|
||||
|
||||
logging.warning('Bye!')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
start_webhook(dispatcher=dp, webhook_path=WEBHOOK_PATH, on_startup=on_startup, on_shutdown=on_shutdown,
|
||||
skip_updates=True, host=WEBAPP_HOST, port=WEBAPP_PORT)
|
||||
start_webhook(
|
||||
dispatcher=dp,
|
||||
webhook_path=WEBHOOK_PATH,
|
||||
on_startup=on_startup,
|
||||
on_shutdown=on_shutdown,
|
||||
skip_updates=True,
|
||||
host=WEBAPP_HOST,
|
||||
port=WEBAPP_PORT,
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue