omar1232 commited on
Commit
19c5e3a
·
verified ·
1 Parent(s): 951d89b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -5
app.py CHANGED
@@ -28,8 +28,8 @@ async def error_handler(update: Update, context: ContextTypes.DEFAULT_TYPE):
28
  text="Bot conflict detected. Restarting..."
29
  )
30
 
31
- # Run the Telegram bot
32
- async def run_telegram_bot():
33
  if not TELEGRAM_BOT_TOKEN:
34
  print("Telegram bot token not found. Please set TELEGRAM_BOT_TOKEN in the Space secrets.")
35
  return
@@ -53,10 +53,14 @@ async def run_telegram_bot():
53
  except Exception as e:
54
  print(f"Error clearing updates: {e}")
55
 
56
- # Start the bot in the main thread
57
  print("Starting Telegram bot...")
58
  await application.run_polling(allowed_updates=Update.ALL_TYPES)
59
 
60
- # Run the bot
61
  if __name__ == "__main__":
62
- asyncio.run(run_telegram_bot())
 
 
 
 
 
28
  text="Bot conflict detected. Restarting..."
29
  )
30
 
31
+ # Set up and run the Telegram bot
32
+ async def setup_and_run_bot():
33
  if not TELEGRAM_BOT_TOKEN:
34
  print("Telegram bot token not found. Please set TELEGRAM_BOT_TOKEN in the Space secrets.")
35
  return
 
53
  except Exception as e:
54
  print(f"Error clearing updates: {e}")
55
 
56
+ # Start the bot
57
  print("Starting Telegram bot...")
58
  await application.run_polling(allowed_updates=Update.ALL_TYPES)
59
 
60
+ # Run the bot using the existing event loop
61
  if __name__ == "__main__":
62
+ loop = asyncio.get_event_loop()
63
+ try:
64
+ loop.run_until_complete(setup_and_run_bot())
65
+ finally:
66
+ loop.close()