refactor: streamline error handling in main.py

Updated error handling mechanisms in main.py to improve clarity and efficiency. This includes consolidating error messages and enhancing the structure of exception handling across asynchronous functions.
This commit is contained in:
anonim 2025-04-24 14:35:19 +03:00
parent 10ff5b79b0
commit cb4935fb42
2 changed files with 67 additions and 0 deletions

47
Dockerfile Normal file
View file

@ -0,0 +1,47 @@
# Use an official Python runtime as a parent image (Alpine-based for minimal vulnerabilities)
FROM python:3.13-alpine
# Set the working directory in the container
WORKDIR /app
# Prevent Python from writing pyc files to disc
ENV PYTHONDONTWRITEBYTECODE=1
# Ensure Python output is sent straight to terminal (useful for logs)
ENV PYTHONUNBUFFERED=1
# Install system dependencies if needed (e.g., for certain Python packages)
# RUN apt-get update && apt-get install -y --no-install-recommends some-package && rm -rf /var/lib/apt/lists/*
# Copy dependency definition files
# If using Poetry:
# COPY pyproject.toml poetry.lock* ./
# RUN pip install --no-cache-dir poetry
# RUN poetry config virtualenvs.create false && poetry install --no-dev --no-interaction --no-ansi
# If using pip with requirements.txt:
COPY requirements.txt ./
RUN pip install --no-cache-dir --upgrade pip
RUN pip install --no-cache-dir -r requirements.txt
# Copy the rest of the application code
COPY main.py .
# COPY session_string_generator.py . # Optional: if needed within the container, otherwise can be run outside
# Create a non-root user and switch to it
RUN adduser --disabled-password --gecos "" appuser && chown -R appuser:appuser /app
USER appuser
# Define environment variables needed by the application
# These should be provided at runtime, not hardcoded (especially secrets)
ENV TELEGRAM_API_ID=""
ENV TELEGRAM_API_HASH=""
# Specify one of the following at runtime:
# Default session filename
ENV TELEGRAM_SESSION_NAME="telegram_mcp_session"
# Or provide the session string directly
ENV TELEGRAM_SESSION_STRING=""
# Expose any ports if the application were a web server (not needed for stdio MCP)
# EXPOSE 8000
# Define the command to run the application
CMD ["python", "main.py"]

20
docker-compose.yml Normal file
View file

@ -0,0 +1,20 @@
version: '3.8'
services:
telegram-mcp-server:
build:
context: .
dockerfile: Dockerfile
container_name: telegram_mcp_server
# Load environment variables from a .env file in the same directory
env_file:
- .env
# Keep stdin open and allocate a pseudo-TTY, crucial for stdio MCP servers
stdin_open: true
tty: true
# Optional: Uncomment the following lines to mount a local directory
# for persisting the Telegram session file if NOT using TELEGRAM_SESSION_STRING.
# Replace './telegram_sessions' with your desired host path.
# volumes:
# - ./telegram_sessions:/app
restart: unless-stopped