Spaces:
Runtime error
Runtime error
Update telegram_bot.py
Browse files- telegram_bot.py +12 -2
telegram_bot.py
CHANGED
|
@@ -128,11 +128,12 @@ class TelegramHomeopathyBot:
|
|
| 128 |
api_url = "https://llm.chutes.ai/v1/chat/completions"
|
| 129 |
|
| 130 |
try:
|
|
|
|
| 131 |
response = requests.post(
|
| 132 |
api_url,
|
| 133 |
headers=headers,
|
| 134 |
json=data,
|
| 135 |
-
timeout=30
|
| 136 |
)
|
| 137 |
|
| 138 |
if response.status_code == 200:
|
|
@@ -147,6 +148,9 @@ class TelegramHomeopathyBot:
|
|
| 147 |
logger.error(f"Chute.ai Error {response.status_code}: {error_message}")
|
| 148 |
return f"β I'm having technical difficulties. Please try again later. (Error: {response.status_code})"
|
| 149 |
|
|
|
|
|
|
|
|
|
|
| 150 |
except Exception as e:
|
| 151 |
logger.error(f"Connection Error: {e}")
|
| 152 |
return f"β Connection error. Please try again. Error: {str(e)}"
|
|
@@ -378,9 +382,15 @@ def main():
|
|
| 378 |
print("π Starting Telegram Homeopathy Bot...")
|
| 379 |
|
| 380 |
# Create Application
|
|
|
|
| 381 |
application = (
|
| 382 |
Application.builder()
|
| 383 |
.token(TELEGRAM_TOKEN)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 384 |
.build()
|
| 385 |
)
|
| 386 |
|
|
@@ -409,7 +419,7 @@ def main():
|
|
| 409 |
|
| 410 |
# Start the Bot
|
| 411 |
print("β
Bot is running... Press Ctrl+C to stop")
|
| 412 |
-
#
|
| 413 |
application.run_polling(
|
| 414 |
allowed_updates=Update.ALL_TYPES,
|
| 415 |
bootstrap_retries=5,
|
|
|
|
| 128 |
api_url = "https://llm.chutes.ai/v1/chat/completions"
|
| 129 |
|
| 130 |
try:
|
| 131 |
+
# Set connection and read timeouts for the AI query request
|
| 132 |
response = requests.post(
|
| 133 |
api_url,
|
| 134 |
headers=headers,
|
| 135 |
json=data,
|
| 136 |
+
timeout=(5, 30) # 5s connect timeout, 30s read timeout
|
| 137 |
)
|
| 138 |
|
| 139 |
if response.status_code == 200:
|
|
|
|
| 148 |
logger.error(f"Chute.ai Error {response.status_code}: {error_message}")
|
| 149 |
return f"β I'm having technical difficulties. Please try again later. (Error: {response.status_code})"
|
| 150 |
|
| 151 |
+
except requests.exceptions.Timeout:
|
| 152 |
+
logger.error("Connection Error: Chute.ai request timed out.")
|
| 153 |
+
return "β Connection error: The AI service took too long to respond. Please try again."
|
| 154 |
except Exception as e:
|
| 155 |
logger.error(f"Connection Error: {e}")
|
| 156 |
return f"β Connection error. Please try again. Error: {str(e)}"
|
|
|
|
| 382 |
print("π Starting Telegram Homeopathy Bot...")
|
| 383 |
|
| 384 |
# Create Application
|
| 385 |
+
# CRITICAL FIX: Adding request_kwargs to explicitly set network timeouts for the underlying HTTPX client
|
| 386 |
application = (
|
| 387 |
Application.builder()
|
| 388 |
.token(TELEGRAM_TOKEN)
|
| 389 |
+
.request_kwargs({
|
| 390 |
+
# Explicitly set low timeouts to force failures and retries faster if DNS fails
|
| 391 |
+
'connect_timeout': 5.0, # 5 seconds to establish connection
|
| 392 |
+
'read_timeout': 15.0 # 15 seconds to receive the first byte
|
| 393 |
+
})
|
| 394 |
.build()
|
| 395 |
)
|
| 396 |
|
|
|
|
| 419 |
|
| 420 |
# Start the Bot
|
| 421 |
print("β
Bot is running... Press Ctrl+C to stop")
|
| 422 |
+
# Using bootstrap_retries to handle the recurrent network failure during startup
|
| 423 |
application.run_polling(
|
| 424 |
allowed_updates=Update.ALL_TYPES,
|
| 425 |
bootstrap_retries=5,
|