Esmaill1 commited on
Commit ·
aec5ae9
1
Parent(s): 9af2655
Fix: Switch to full python image and add network wait loop to solve DNS race conditions
Browse files- Dockerfile +3 -2
- bot.py +23 -0
Dockerfile
CHANGED
|
@@ -1,11 +1,12 @@
|
|
| 1 |
-
FROM python:3.9
|
| 2 |
|
| 3 |
# Set working directory
|
| 4 |
WORKDIR /app
|
| 5 |
|
| 6 |
-
# Install system dependencies
|
| 7 |
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 8 |
gcc \
|
|
|
|
| 9 |
&& rm -rf /var/lib/apt/lists/*
|
| 10 |
|
| 11 |
# Copy requirements
|
|
|
|
| 1 |
+
FROM python:3.9
|
| 2 |
|
| 3 |
# Set working directory
|
| 4 |
WORKDIR /app
|
| 5 |
|
| 6 |
+
# Install system dependencies (including ca-certificates explicitly)
|
| 7 |
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 8 |
gcc \
|
| 9 |
+
ca-certificates \
|
| 10 |
&& rm -rf /var/lib/apt/lists/*
|
| 11 |
|
| 12 |
# Copy requirements
|
bot.py
CHANGED
|
@@ -2,6 +2,8 @@ import os
|
|
| 2 |
import json
|
| 3 |
import asyncio
|
| 4 |
import logging
|
|
|
|
|
|
|
| 5 |
from io import BytesIO
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
from telegram import Update
|
|
@@ -380,11 +382,32 @@ async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
|
| 380 |
await update.message.reply_text(help_message)
|
| 381 |
|
| 382 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 383 |
def main():
|
| 384 |
"""Start the bot."""
|
| 385 |
if not TELEGRAM_BOT_TOKEN:
|
| 386 |
raise ValueError("TELEGRAM_BOT_TOKEN environment variable is not set!")
|
| 387 |
|
|
|
|
|
|
|
|
|
|
| 388 |
# Configure connection options to be more resilient
|
| 389 |
request = HTTPXRequest(
|
| 390 |
connection_pool_size=8,
|
|
|
|
| 2 |
import json
|
| 3 |
import asyncio
|
| 4 |
import logging
|
| 5 |
+
import socket
|
| 6 |
+
import time
|
| 7 |
from io import BytesIO
|
| 8 |
from dotenv import load_dotenv
|
| 9 |
from telegram import Update
|
|
|
|
| 382 |
await update.message.reply_text(help_message)
|
| 383 |
|
| 384 |
|
| 385 |
+
def wait_for_network():
|
| 386 |
+
"""Wait for network connectivity/DNS resolution before starting."""
|
| 387 |
+
logger.info("Checking network connectivity...")
|
| 388 |
+
retries = 0
|
| 389 |
+
max_retries = 12 # Wait up to 60 seconds
|
| 390 |
+
while retries < max_retries:
|
| 391 |
+
try:
|
| 392 |
+
socket.gethostbyname("api.telegram.org")
|
| 393 |
+
logger.info("Network is ready! DNS resolution successful.")
|
| 394 |
+
return
|
| 395 |
+
except socket.gaierror:
|
| 396 |
+
retries += 1
|
| 397 |
+
logger.warning(f"DNS resolution failed for api.telegram.org. Retrying in 5 seconds... ({retries}/{max_retries})")
|
| 398 |
+
time.sleep(5)
|
| 399 |
+
logger.error("Could not resolve api.telegram.org after multiple attempts. Exiting.")
|
| 400 |
+
# We continue anyway, let the library fail if it must, but we warned the logs.
|
| 401 |
+
|
| 402 |
+
|
| 403 |
def main():
|
| 404 |
"""Start the bot."""
|
| 405 |
if not TELEGRAM_BOT_TOKEN:
|
| 406 |
raise ValueError("TELEGRAM_BOT_TOKEN environment variable is not set!")
|
| 407 |
|
| 408 |
+
# Wait for network to be ready
|
| 409 |
+
wait_for_network()
|
| 410 |
+
|
| 411 |
# Configure connection options to be more resilient
|
| 412 |
request = HTTPXRequest(
|
| 413 |
connection_pool_size=8,
|