Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -20,8 +20,16 @@ async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
|
| 20 |
response = generator(user_message, max_length=50, num_return_sequences=1, truncation=True)[0]['generated_text']
|
| 21 |
await update.message.reply_text(response)
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
# Run the Telegram bot
|
| 24 |
-
def run_telegram_bot():
|
| 25 |
if not TELEGRAM_BOT_TOKEN:
|
| 26 |
print("Telegram bot token not found. Please set TELEGRAM_BOT_TOKEN in the Space secrets.")
|
| 27 |
return
|
|
@@ -31,11 +39,24 @@ def run_telegram_bot():
|
|
| 31 |
# Add handlers
|
| 32 |
application.add_handler(CommandHandler("start", start))
|
| 33 |
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
# Start the bot in the main thread
|
| 36 |
print("Starting Telegram bot...")
|
| 37 |
-
|
| 38 |
|
| 39 |
# Run the bot
|
| 40 |
if __name__ == "__main__":
|
| 41 |
-
run_telegram_bot()
|
|
|
|
| 20 |
response = generator(user_message, max_length=50, num_return_sequences=1, truncation=True)[0]['generated_text']
|
| 21 |
await update.message.reply_text(response)
|
| 22 |
|
| 23 |
+
async def error_handler(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
| 24 |
+
print(f"Error occurred: {context.error}")
|
| 25 |
+
if "Conflict" in str(context.error):
|
| 26 |
+
await context.bot.send_message(
|
| 27 |
+
chat_id=update.effective_chat.id if update else None,
|
| 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
|
|
|
|
| 39 |
# Add handlers
|
| 40 |
application.add_handler(CommandHandler("start", start))
|
| 41 |
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message))
|
| 42 |
+
application.add_error_handler(error_handler)
|
| 43 |
+
|
| 44 |
+
# Clear any existing webhook and updates
|
| 45 |
+
print("Clearing any existing webhook and updates...")
|
| 46 |
+
try:
|
| 47 |
+
await application.bot.delete_webhook(drop_pending_updates=True)
|
| 48 |
+
# Fetch and discard any pending updates to clear the queue
|
| 49 |
+
updates = await application.bot.get_updates(timeout=1)
|
| 50 |
+
if updates:
|
| 51 |
+
last_update_id = updates[-1].update_id
|
| 52 |
+
await application.bot.get_updates(offset=last_update_id + 1, timeout=1)
|
| 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())
|