11import asyncio
22from asyncio import Lock
3+ from threading import Thread
34
45from aiogram import Bot , Dispatcher
56from aiogram .client .default import DefaultBotProperties
2021
2122
2223_bot = None
24+ _polling_task : asyncio .Task = None
2325_lock = Lock ()
2426_dp = Dispatcher ()
2527
@@ -36,6 +38,7 @@ async def startup_telegram_bot():
3638 restart = False
3739 global _bot
3840 global _dp
41+ global _polling_task
3942
4043 if _bot :
4144 await shutdown_telegram_bot ()
@@ -58,8 +61,8 @@ async def startup_telegram_bot():
5861 pass
5962
6063 try :
61- if settings .method == RunMethod .LONGPULLING :
62- asyncio .create_task (_dp .start_polling (_bot ))
64+ if settings .method == RunMethod .LONGPOLLING :
65+ _polling_task = asyncio .create_task (_dp .start_polling (_bot ))
6366 else :
6467 # register webhook
6568 webhook_address = f"{ settings .webhook_url } /api/tghook"
@@ -86,13 +89,31 @@ async def startup_telegram_bot():
8689async def shutdown_telegram_bot ():
8790 global _bot
8891 global _dp
92+ global _polling_task
8993
9094 async with _lock :
9195 if isinstance (_bot , Bot ):
9296 logger .info ("Shutting down telegram bot" )
9397 try :
94- await _bot .get_webhook_info (5 )
95- await _bot .delete_webhook (drop_pending_updates = True )
98+ if _polling_task is not None and not _polling_task .done ():
99+ logger .info ("stopping long polling" )
100+ if _dp is not None :
101+ await _dp .stop_polling ()
102+ logger .info ("Dispatcher polling stopped" )
103+ # Cancel the polling task
104+ _polling_task .cancel ()
105+ try :
106+ await asyncio .wait_for (_polling_task , timeout = 5.0 )
107+ logger .info ("Telegram bot polling task cancelled successfully" )
108+ except asyncio .TimeoutError :
109+ logger .warning ("Telegram bot polling task did not cancel in time, forcing shutdown" )
110+ except asyncio .CancelledError :
111+ logger .info ("Telegram bot polling task cancelled successfully" )
112+
113+ _polling_task = None
114+ else :
115+ await _bot .get_webhook_info (5 )
116+ await _bot .delete_webhook (drop_pending_updates = True )
96117 except (
97118 TelegramNetworkError ,
98119 TelegramRetryAfter ,
@@ -101,11 +122,11 @@ async def shutdown_telegram_bot():
101122 ) as err :
102123 if hasattr (err , "message" ):
103124 logger .error (err .message )
104- elif isinstance (err , TelegramUnauthorizedError ):
105- try :
106- asyncio .create_task (_dp .stop_polling ())
107- except Exception :
108- pass
125+ # elif isinstance(err, TelegramUnauthorizedError):
126+ # try:
127+ # asyncio.create_task(_dp.stop_polling())
128+ # except Exception:
129+ # pass
109130 else :
110131 logger .error (err )
111132
0 commit comments