Skip to content

Commit 309f893

Browse files
fix: Error when setting up long polling
1 parent 178e893 commit 309f893

File tree

2 files changed

+39
-12
lines changed

2 files changed

+39
-12
lines changed

app/models/settings.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
class RunMethod(StrEnum):
1414
WEBHOOK = "webhook"
15-
LONGPULLING = "long-polling"
15+
LONGPOLLING = "long-polling"
1616

1717

1818
class Telegram(BaseModel):
@@ -55,8 +55,14 @@ def token_validation(cls, v):
5555

5656
@model_validator(mode="after")
5757
def check_enable_requires_token_and_url(self):
58-
if self.enable and (not self.token or not self.webhook_url or not self.webhook_secret):
59-
raise ValueError("Telegram bot cannot be enabled without token, webhook_url and webhook_secret.")
58+
if self.enable and (
59+
(self.method == RunMethod.WEBHOOK and (not self.token or not self.webhook_url or not self.webhook_secret))
60+
or (self.method == RunMethod.LONGPOLLING and not self.token)
61+
):
62+
if self.method == RunMethod.WEBHOOK:
63+
raise ValueError("Telegram bot cannot be enabled without token, webhook_url and webhook_secret.")
64+
elif self.method == RunMethod.LONGPOLLING:
65+
raise ValueError("Telegram bot cannot be enabled without token.")
6066
return self
6167

6268

app/telegram/__init__.py

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import asyncio
22
from asyncio import Lock
3+
from threading import Thread
34

45
from aiogram import Bot, Dispatcher
56
from aiogram.client.default import DefaultBotProperties
@@ -20,6 +21,7 @@
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():
8689
async 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

Comments
 (0)