Fix Telegram Bot - disable job_queue and fix event loop
Changes: - Disable job_queue to avoid timezone/pytz error - Fix event loop in background thread (use new_event_loop) - Keep event loop running with run_forever() - Add error handling in thread Fixes: bot_thread was dying due to timezone error Now: Thread stays alive and processes commands
This commit is contained in:
@@ -252,8 +252,13 @@ class TelegramBotCommander:
|
||||
|
||||
async def start_async(self):
|
||||
"""Start bot asynchronously (new API)"""
|
||||
# Create application
|
||||
self.application = Application.builder().token(self.bot_token).build()
|
||||
# Create application (disable job queue to avoid timezone issues)
|
||||
self.application = (
|
||||
Application.builder()
|
||||
.token(self.bot_token)
|
||||
.job_queue(None) # Disable job queue - fixes timezone error
|
||||
.build()
|
||||
)
|
||||
|
||||
# Add command handlers
|
||||
self.application.add_handler(CommandHandler("start", self.cmd_start))
|
||||
@@ -289,7 +294,20 @@ class TelegramBotCommander:
|
||||
def start_background(self):
|
||||
"""Start bot in background thread"""
|
||||
def run_async_loop():
|
||||
asyncio.run(self.start_async())
|
||||
try:
|
||||
# Create new event loop for this thread
|
||||
loop = asyncio.new_event_loop()
|
||||
asyncio.set_event_loop(loop)
|
||||
|
||||
# Run bot
|
||||
loop.run_until_complete(self.start_async())
|
||||
|
||||
# Keep loop running
|
||||
loop.run_forever()
|
||||
except Exception as e:
|
||||
print(f"❌ Bot thread error: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
|
||||
thread = threading.Thread(target=run_async_loop, daemon=True)
|
||||
thread.start()
|
||||
|
||||
Reference in New Issue
Block a user