Spaces:
Sleeping
Sleeping
Update telegram_bot.py
Browse files- telegram_bot.py +23 -11
telegram_bot.py
CHANGED
|
@@ -88,26 +88,38 @@ async def handle_streaming_message(update: Update, context: ContextTypes.DEFAULT
|
|
| 88 |
await status_msg.edit_text(f"❌ Error: {str(e)}")
|
| 89 |
|
| 90 |
def start_telegram_bot(shared_agent, token="8634464564:AAGL7FzFkMN-Uktf97NKtDs1RFPGxec-HFI"):
|
| 91 |
-
"""Start the Telegram bot with a shared agent instance
|
| 92 |
|
| 93 |
Args:
|
| 94 |
shared_agent: The CodeAgent instance to use for processing messages
|
| 95 |
token: Telegram bot token (default provided, can be overridden)
|
| 96 |
"""
|
|
|
|
|
|
|
| 97 |
global agent
|
| 98 |
agent = shared_agent
|
| 99 |
|
| 100 |
-
# Create
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
# Register handlers
|
| 104 |
-
application.add_handler(CommandHandler("start", start))
|
| 105 |
-
application.add_handler(CommandHandler("help", help_command))
|
| 106 |
-
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message))
|
| 107 |
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
def main():
|
| 113 |
"""Standalone entry point (for backward compatibility)"""
|
|
|
|
| 88 |
await status_msg.edit_text(f"❌ Error: {str(e)}")
|
| 89 |
|
| 90 |
def start_telegram_bot(shared_agent, token="8634464564:AAGL7FzFkMN-Uktf97NKtDs1RFPGxec-HFI"):
|
| 91 |
+
"""Start the Telegram bot with a shared agent instance in a thread-safe way
|
| 92 |
|
| 93 |
Args:
|
| 94 |
shared_agent: The CodeAgent instance to use for processing messages
|
| 95 |
token: Telegram bot token (default provided, can be overridden)
|
| 96 |
"""
|
| 97 |
+
import asyncio
|
| 98 |
+
|
| 99 |
global agent
|
| 100 |
agent = shared_agent
|
| 101 |
|
| 102 |
+
# Create a new event loop for this thread
|
| 103 |
+
loop = asyncio.new_event_loop()
|
| 104 |
+
asyncio.set_event_loop(loop)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
+
try:
|
| 107 |
+
# Create application
|
| 108 |
+
application = Application.builder().token(token).build()
|
| 109 |
+
|
| 110 |
+
# Register handlers
|
| 111 |
+
application.add_handler(CommandHandler("start", start))
|
| 112 |
+
application.add_handler(CommandHandler("help", help_command))
|
| 113 |
+
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message))
|
| 114 |
+
|
| 115 |
+
# Start bot with stop_signals=None to avoid signal handler issues in threads
|
| 116 |
+
logger.info("Telegram Bot started!")
|
| 117 |
+
application.run_polling(
|
| 118 |
+
allowed_updates=Update.ALL_TYPES,
|
| 119 |
+
stop_signals=None # Disable signal handlers for thread safety
|
| 120 |
+
)
|
| 121 |
+
finally:
|
| 122 |
+
loop.close()
|
| 123 |
|
| 124 |
def main():
|
| 125 |
"""Standalone entry point (for backward compatibility)"""
|