diff --git "a/app.py" "b/app.py" --- "a/app.py" +++ "b/app.py" @@ -4,14 +4,13 @@ import os import sqlite3 import datetime from pathlib import Path -import imghdr # Used for image detection from flask import Flask, jsonify, request, render_template_string, send_from_directory, redirect, url_for, session from telethon.sync import TelegramClient from telethon.errors import SessionPasswordNeededError, FloodWaitError, UserNotParticipantError from telethon.tl.functions.messages import ImportChatInviteRequest from telethon.tl.functions.channels import JoinChannelRequest -from telethon.tl.types import User, Chat, Channel +from telethon.tl.types import User, Chat, Channel, MessageMediaPhoto app = Flask(__name__) @@ -49,13 +48,24 @@ async def get_user_client(user_id): return None, "User not found in database." session_file = result[0] + if not Path(session_file).exists(): + with sqlite3.connect(DB_PATH) as conn_inner: + conn_inner.execute('DELETE FROM users WHERE id = ?', (user_id,)) + conn_inner.commit() + return None, "Session file not found. Please log in again." + client = TelegramClient(session_file, API_ID, API_HASH) try: await client.connect() if not await client.is_user_authorized(): - return None, "Client not authorized. Please log in again." + with sqlite3.connect(DB_PATH) as conn_inner: + conn_inner.execute('DELETE FROM users WHERE id = ?', (user_id,)) + conn_inner.commit() + if Path(session_file).exists(): + os.remove(session_file) + return None, "Client not authorized. Session expired or invalid. Please log in again." except Exception as e: - return None, f"Failed to connect or authorize Telegram client: {e}" + return None, f"Failed to connect or authorize Telegram client: {e}. Try logging out and back in." return client, None LOGIN_TEMPLATE = ''' @@ -65,28 +75,119 @@ LOGIN_TEMPLATE = '''
{{ chat.type }} {% if chat.participants %}| Participants: {{ chat.participants }}{% endif %}
{% else %} -No chats found.
+No chats found.
{% endfor %} @@ -767,116 +1007,128 @@ ADMHOSTO_MANAGE_TEMPLATE = '''