Skip to content

Commit 4db45a0

Browse files
committed
Fix FastTelethon 500 errors: PeerChannel entity resolution and temp_path UnboundLocalError
1 parent 684c233 commit 4db45a0

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

python_service/main.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from fastapi.responses import StreamingResponse, HTMLResponse
1515
from telethon import TelegramClient, utils
1616
from telethon.tl import types
17+
from telethon.tl.types import PeerChannel
1718
from telethon.errors import SessionPasswordNeededError, PhoneCodeInvalidError
1819
import uvicorn
1920

@@ -426,6 +427,8 @@ async def upload_large_file(
426427
if not target_channel:
427428
raise HTTPException(status_code=400, detail="Channel ID required")
428429

430+
temp_path = None # Initialize to avoid UnboundLocalError
431+
429432
try:
430433
# Save uploaded file temporarily
431434
temp_path = f"/tmp/{file.filename}"
@@ -481,7 +484,7 @@ async def progress_callback(current, total):
481484

482485
except Exception as e:
483486
logger.error(f"Upload failed: {e}", exc_info=True)
484-
if os.path.exists(temp_path):
487+
if temp_path and os.path.exists(temp_path):
485488
os.remove(temp_path)
486489
raise HTTPException(status_code=500, detail=f"Upload failed: {str(e)}")
487490

@@ -498,11 +501,18 @@ async def download_large_file(channel_id: str, message_id: int):
498501
if not client or not await client.is_user_authorized():
499502
raise HTTPException(status_code=503, detail="Telegram client not ready")
500503

504+
temp_path = None # Initialize to avoid UnboundLocalError
505+
501506
try:
502507
logger.info(f"Fetching message {message_id} from channel {channel_id}...")
503508

509+
# Convert channel_id string to integer and create PeerChannel
510+
# This helps Telethon resolve the entity correctly
511+
channel_id_int = int(channel_id)
512+
peer = PeerChannel(utils.resolve_id(channel_id_int)[0])
513+
504514
# Get the message
505-
message = await client.get_messages(channel_id, ids=message_id)
515+
message = await client.get_messages(peer, ids=message_id)
506516

507517
if not message or not message.document:
508518
raise HTTPException(status_code=404, detail="File not found")
@@ -554,7 +564,7 @@ def file_iterator():
554564
raise
555565
except Exception as e:
556566
logger.error(f"Download failed: {e}", exc_info=True)
557-
if os.path.exists(temp_path):
567+
if temp_path and os.path.exists(temp_path):
558568
os.remove(temp_path)
559569
raise HTTPException(status_code=500, detail=f"Download failed: {str(e)}")
560570

@@ -566,7 +576,11 @@ async def get_file_info(channel_id: str, message_id: int):
566576
raise HTTPException(status_code=503, detail="Telegram client not ready")
567577

568578
try:
569-
message = await client.get_messages(channel_id, ids=message_id)
579+
# Convert channel_id string to integer and create PeerChannel
580+
channel_id_int = int(channel_id)
581+
peer = PeerChannel(utils.resolve_id(channel_id_int)[0])
582+
583+
message = await client.get_messages(peer, ids=message_id)
570584

571585
if not message or not message.document:
572586
raise HTTPException(status_code=404, detail="File not found")

render.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ services:
1111
plan: free # Change to 'standard' or higher for production
1212
region: singapore # Choose your region
1313
branch: main
14+
autoDeploy: false # Disable auto-deploy (manual deploy only)
1415
envVars:
1516
- key: RUST_LOG
1617
value: info
1718
- key: FASTTELETHON_URL
18-
value: changeme_to_your-other_render_service_url
19+
value: https://fasttelethon.onrender.com # Update with your FastTelethon service URL
1920
- key: BOT_TOKEN
2021
sync: false # Set manually in Render dashboard
2122
- key: STORAGE_CHANNEL_ID

0 commit comments

Comments
 (0)