Update 2025-12-19 16:23:03
This commit is contained in:
@@ -129,20 +129,36 @@ def run_polling(flask_app: Flask) -> None:
|
||||
if flask_app is None:
|
||||
raise ValueError("flask_app is required for run_polling")
|
||||
|
||||
bot_token: Optional[str] = None
|
||||
bot_name: Optional[str] = None
|
||||
bot_id: Optional[int] = None
|
||||
|
||||
# DB에서 활성 봇 조회
|
||||
with flask_app.app_context():
|
||||
bots = TelegramBot.query.filter_by(is_active=True).all()
|
||||
|
||||
if not bots:
|
||||
logger.warning("No active bots found")
|
||||
logger.warning("No active bots found for polling service.")
|
||||
return
|
||||
|
||||
# 첫 번째 활성 봇만 사용 (여러 봇이 동시에 폴링하면 충돌 가능)
|
||||
if len(bots) > 1:
|
||||
logger.warning("Multiple active bots found. Only the first one (%s) will be used.", bots[0].name)
|
||||
|
||||
# 첫 번째 활성 봇 사용
|
||||
bot = bots[0]
|
||||
flask_app.logger.info("Starting polling for bot: %s (ID: %s)", bot.name, bot.id)
|
||||
# DB 세션 밖에서도 사용할 수 있도록 필요한 정보만 추출 (Detached Instance 에러 방지)
|
||||
bot_token = bot.token
|
||||
bot_name = bot.name
|
||||
bot_id = bot.id
|
||||
|
||||
logger.info("Starting polling for bot: %s (ID: %s)", bot_name, bot_id)
|
||||
|
||||
if not bot_token:
|
||||
logger.error("Bot token not found.")
|
||||
return
|
||||
|
||||
# Application 생성
|
||||
application = Application.builder().token(bot.token).build()
|
||||
application = Application.builder().token(bot_token).build()
|
||||
|
||||
# Flask app을 bot_data에 넣어서 핸들러에서 사용할 수 있게 함
|
||||
application.bot_data["flask_app"] = flask_app
|
||||
@@ -154,4 +170,4 @@ def run_polling(flask_app: Flask) -> None:
|
||||
# v20 스타일: run_polling 은 동기 함수이고, 내부에서 이벤트 루프를 직접 관리함
|
||||
application.run_polling(drop_pending_updates=True)
|
||||
except Exception as e:
|
||||
flask_app.logger.exception("Error in bot polling: %s", e)
|
||||
logger.exception("Error in bot polling: %s", e)
|
||||
Reference in New Issue
Block a user