@@ -50,7 +50,24 @@ public TelegramService(TransactionInfoService tis, IDbService db, ILogger<IFileS
5050 {
5151 if ( client == null && HasValidCredentials ( ) )
5252 {
53- newClient ( ) ;
53+ try
54+ {
55+ newClient ( ) ;
56+ }
57+ catch ( WTelegram . WTException ex ) when ( ex . Message . Contains ( "session file" ) )
58+ {
59+ // Session file is corrupted - delete it and create a new one
60+ _logger . LogWarning ( "Session file corrupted during startup, deleting: {Message}" , ex . Message ) ;
61+ var sessionPath = UserService . USERDATAFOLDER + "/WTelegram.session" ;
62+ if ( File . Exists ( sessionPath ) )
63+ {
64+ File . Delete ( sessionPath ) ;
65+ _logger . LogInformation ( "Deleted corrupted session file: {Path}" , sessionPath ) ;
66+ }
67+ // Create new client with fresh session
68+ newClient ( ) ;
69+ _logger . LogInformation ( "Telegram client initialized with new session" ) ;
70+ }
5471 }
5572 else if ( ! HasValidCredentials ( ) )
5673 {
@@ -100,8 +117,25 @@ public void InitializeClient()
100117 if ( client == null ) // Double-check after acquiring lock
101118 {
102119 _logger . LogInformation ( "Initializing Telegram client after setup" ) ;
103- newClient ( ) ;
104- _logger . LogInformation ( "Telegram client initialized successfully" ) ;
120+ try
121+ {
122+ newClient ( ) ;
123+ _logger . LogInformation ( "Telegram client initialized successfully" ) ;
124+ }
125+ catch ( WTelegram . WTException ex ) when ( ex . Message . Contains ( "session file" ) )
126+ {
127+ // Session file is corrupted - delete it and create a new one
128+ _logger . LogWarning ( "Session file corrupted, deleting and creating new session: {Message}" , ex . Message ) ;
129+ var sessionPath = UserService . USERDATAFOLDER + "/WTelegram.session" ;
130+ if ( File . Exists ( sessionPath ) )
131+ {
132+ File . Delete ( sessionPath ) ;
133+ _logger . LogInformation ( "Deleted corrupted session file: {Path}" , sessionPath ) ;
134+ }
135+ // Create new client with fresh session
136+ newClient ( ) ;
137+ _logger . LogInformation ( "Telegram client initialized with new session" ) ;
138+ }
105139 }
106140 }
107141 catch ( Exception ex )
@@ -599,6 +633,43 @@ public async Task<ChatsWithFolders> getChatsWithFolders()
599633 return result ;
600634 }
601635
636+ /// <summary>
637+ /// Preloads channels and configuration at startup if user is logged in.
638+ /// This ensures channels are available for API calls without requiring UI access first.
639+ /// </summary>
640+ public async Task PreloadChannelsAsync ( )
641+ {
642+ if ( ! checkUserLogin ( ) )
643+ {
644+ _logger . LogInformation ( "PreloadChannelsAsync: User not logged in, skipping preload" ) ;
645+ return ;
646+ }
647+
648+ try
649+ {
650+ _logger . LogInformation ( "PreloadChannelsAsync: Starting channel preload..." ) ;
651+
652+ // Load all channels into cache
653+ var channels = await getAllChats ( ) ;
654+ _logger . LogInformation ( "PreloadChannelsAsync: Loaded {Count} channels" , channels . Count ) ;
655+
656+ // Also preload folders
657+ var folders = await getChatsWithFolders ( ) ;
658+ _logger . LogInformation ( "PreloadChannelsAsync: Loaded {FolderCount} folders with {UngroupedCount} ungrouped chats" ,
659+ folders . Folders . Count , folders . UngroupedChats . Count ) ;
660+
661+ // Preload favourites
662+ var favourites = await GetFouriteChannels ( true ) ;
663+ _logger . LogInformation ( "PreloadChannelsAsync: Loaded {Count} favourite channels" , favourites . Count ) ;
664+
665+ _logger . LogInformation ( "PreloadChannelsAsync: Channel preload completed successfully" ) ;
666+ }
667+ catch ( Exception ex )
668+ {
669+ _logger . LogError ( ex , "PreloadChannelsAsync: Error preloading channels" ) ;
670+ }
671+ }
672+
602673 public async Task < Message > uploadFile ( string chatId , Stream file , string fileName , string mimeType = null , UploadModel um = null , string caption = null )
603674 {
604675 _logger . LogInformation ( "Starting file upload - FileName: {FileName}, Size: {SizeMB:F2}MB, ChatId: {ChatId}" ,
0 commit comments