File tree Expand file tree Collapse file tree 1 file changed +19
-2
lines changed
Expand file tree Collapse file tree 1 file changed +19
-2
lines changed Original file line number Diff line number Diff line change 1+ import asyncio
12import logging
23
34from aiogram import Bot , Dispatcher , Router , types
1920 allow_headers = ["*" ],
2021)
2122webhook_api_router = APIRouter ()
22- app .include_router (webhook_api_router )
2323
2424bot = Bot (token = settings .BOT_TOKEN , parse_mode = "HTML" )
2525dp = Dispatcher ()
2626messages_handler = Router ()
2727TELEGRAM_WEBHOOK_URL = f"{ settings .WEBHOOK_DOMAIN } /webhook"
2828
2929
30- @webhook_api_router .post ("/webhook" )
30+ def retry_after (seconds : int ):
31+ def decorator (func ):
32+ async def wrapper (* args , ** kwargs ):
33+ result = False
34+ while not result :
35+ try :
36+ await func (* args , ** kwargs )
37+ result = True
38+ except Exception as e :
39+ logger .error (f"Error while processing message: { e } " )
40+ logger .info (f"Retrying after { seconds } seconds" )
41+ await asyncio .sleep (seconds )
42+ return wrapper
43+ return decorator
44+
45+
46+ @webhook_api_router .post ("/webhook" )
3147async def telegram_webhook (request : dict ):
3248 update = types .Update (** request )
3349 await dp .feed_update (bot = bot , update = update )
@@ -38,6 +54,7 @@ async def root():
3854 return {"status" : "ok" }
3955
4056
57+ @retry_after (settings .RETRY_TIMEOUT )
4158async def register_webhook ():
4259 await bot .set_webhook (url = f"{ settings .WEBHOOK_DOMAIN } /webhook" )
4360 webhook_info = await bot .get_webhook_info ()
You can’t perform that action at this time.
0 commit comments