11using System . Diagnostics . CodeAnalysis ;
22using System . Reflection ;
3- using System . Runtime . InteropServices . JavaScript ;
43using Microsoft . Extensions . DependencyInjection ;
54using Microsoft . Extensions . Hosting ;
65using Microsoft . Extensions . Logging ;
1514
1615namespace Telegram . Net . Services ;
1716
17+ [ SuppressMessage ( "ReSharper" , "ReturnValueOfPureMethodIsNotUsed" ) ]
1818public class TelegramHostedService : IHostedService
1919{
20- private IServiceCollection isc { get ; }
21- internal TelegramBotClient Client { get ; set ; }
22- private ITelegramBotConfig Config { get ; }
23- internal Dictionary < string , Func < ITelegramBotClient , Message , CancellationToken , Task > > CommandHandler { get ; set ; } = new ( ) ;
24- internal List < Func < ITelegramBotClient , Message , CancellationToken , Task > > EditedMessageHandler { get ; set ; } = new ( ) ;
25- internal Dictionary < string , Func < ITelegramBotClient , CallbackQuery , CancellationToken , Task > > CallbackQueryHandler { get ; set ; } = new ( ) ;
26- internal Dictionary < string , Func < ITelegramBotClient , InlineQuery , CancellationToken , Task > > InlineHandler { get ; set ; } = new ( ) ;
20+ private IServiceCollection ServiceCollection { get ; } = null ! ;
21+ internal TelegramBotClient Client { get ; set ; } = null ! ;
22+ private ITelegramBotConfig Config { get ; } = null ! ;
23+ internal Dictionary < string , Func < ITelegramBotClient , Message , CancellationToken , Task > ? > CommandHandler { get ; set ; } = new ( ) ;
24+ internal List < Func < ITelegramBotClient , Message , CancellationToken , Task > ? > EditedMessageHandler { get ; set ; } = new ( ) ;
25+ internal Dictionary < string , Func < ITelegramBotClient , CallbackQuery , CancellationToken , Task > ? > CallbackQueryHandler { get ; set ; } = new ( ) ;
26+ internal Dictionary < string , Func < ITelegramBotClient , InlineQuery , CancellationToken , Task > ? > InlineHandler { get ; set ; } = new ( ) ;
2727 internal Func < ITelegramBotClient , PreCheckoutQuery , CancellationToken , Task > ? PreCheckoutHandler { get ; set ; }
28- internal List < Func < ITelegramBotClient , Update , CancellationToken , Task > > DefaultUpdateHandler { get ; set ; } = new ( ) ;
29- internal static ILogger < TelegramHostedService > _logger ;
28+ internal List < Func < ITelegramBotClient , Update , CancellationToken , Task > ? > DefaultUpdateHandler { get ; set ; } = new ( ) ;
29+ internal static ILogger < TelegramHostedService > Logger = null ! ;
3030
31- public TelegramHostedService ( ITelegramBotConfig config , IServiceCollection isc , ILogger < TelegramHostedService > logger )
31+ public TelegramHostedService ( ITelegramBotConfig config , IServiceCollection serviceCollection , ILogger < TelegramHostedService > logger )
3232 {
3333 try
3434 {
35- _logger = logger ;
35+ Logger = logger ;
3636 Client = new TelegramBotClient ( config . Token ) ;
3737 Config = config ;
38- this . isc = isc ;
38+ this . ServiceCollection = serviceCollection ;
3939 }
4040 catch ( Exception ex )
4141 {
42- _logger . Log ( LogLevel . Critical , new EventId ( ) , ex , "Catched exception when creating TelegramHostedService: " ) ;
42+ Logger . Log ( LogLevel . Critical , new EventId ( ) , ex , "Catched exception when creating TelegramHostedService: " ) ;
4343 }
4444 }
4545 internal static bool IsValidHandlerMethod ( MethodInfo method , Type parameterType )
@@ -55,12 +55,12 @@ internal static bool IsValidHandlerMethod(MethodInfo method, Type parameterType)
5555 }
5656 catch ( Exception ex )
5757 {
58- _logger . LogError ( ex , "Catched exception in parsing and checking params." ) ;
58+ Logger . LogError ( ex , "Catched exception in parsing and checking params." ) ;
5959 return false ;
6060 }
6161 }
6262
63- internal static Func < ITelegramBotClient , T , CancellationToken , Task > CreateDelegate < T > ( MethodInfo method )
63+ internal static Func < ITelegramBotClient , T , CancellationToken , Task > ? CreateDelegate < T > ( MethodInfo method )
6464 {
6565 try
6666 {
@@ -70,7 +70,7 @@ internal static Func<ITelegramBotClient, T, CancellationToken, Task> CreateDeleg
7070 }
7171 catch ( Exception ex )
7272 {
73- _logger . Log ( LogLevel . Critical , new EventId ( ) , ex , "Catched exception in CreateDelegate function: " ) ;
73+ Logger . Log ( LogLevel . Critical , new EventId ( ) , ex , "Catched exception in CreateDelegate function: " ) ;
7474 return null ;
7575 }
7676
@@ -106,10 +106,10 @@ await Task.Run(async () =>
106106
107107 if ( methods . Count == 0 )
108108 {
109- _logger . LogWarning ( "No methods found with required attributes" ) ;
109+ Logger . LogWarning ( "No methods found with required attributes" ) ;
110110 }
111111
112- var isp = isc . BuildServiceProvider ( ) ;
112+ var isp = ServiceCollection . BuildServiceProvider ( ) ;
113113 foreach ( var method in methods )
114114 {
115115 var declaringType = method . DeclaringType ! ;
@@ -156,7 +156,7 @@ await Task.Run(async () =>
156156 }
157157 catch ( Exception ex )
158158 {
159- _logger . Log ( LogLevel . Critical , new EventId ( ) , ex , "Catched new exception when added methods: " ) ;
159+ Logger . Log ( LogLevel . Critical , new EventId ( ) , ex , "Catched new exception when added methods: " ) ;
160160 }
161161 } , cancellationToken ) ;
162162 }
@@ -171,39 +171,44 @@ internal async Task UpdateHandler(ITelegramBotClient client, Update update, Canc
171171 case { Message : { } message } :
172172 CommandHandler . Where ( k => message . Text ! . StartsWith ( k . Key ) ) . Select ( async k =>
173173 {
174- await k . Value ( client , message , ctx ) ;
174+ await k . Value ! ( client , message , ctx ) ;
175175 return k ;
176176 } ) ;
177177 break ;
178178 case { EditedMessage : { } message } :
179- EditedMessageHandler . ForEach ( async k => await k ( client , message , ctx ) ) ;
179+ // ReSharper disable once AsyncVoidLambda
180+ EditedMessageHandler . ForEach ( async k => await k ! ( client , message , ctx ) ) ;
180181 break ;
181182 case { CallbackQuery : { } callbackQuery } :
182183 CallbackQueryHandler . Where ( k => callbackQuery . Data ! . StartsWith ( k . Key ) )
183184 . Select ( async k =>
184185 {
185- await k . Value ( client , callbackQuery , ctx ) ;
186+ await k . Value ! ( client , callbackQuery , ctx ) ;
186187 return k ;
187188 } ) ;
188189 break ;
189190 case { InlineQuery : { } inlineQuery } :
190191 InlineHandler . Where ( k => inlineQuery . Id . StartsWith ( k . Key ) ) . Select ( async k =>
191192 {
192- await k . Value ( client , inlineQuery , ctx ) ;
193+ await k . Value ! ( client , inlineQuery , ctx ) ;
193194 return k ;
194195 } ) ;
195196 break ;
196197 case { PreCheckoutQuery : { } preCheckoutQuery } :
197198 if ( PreCheckoutHandler != null ) await PreCheckoutHandler ( client , preCheckoutQuery , ctx ) ;
198199 break ;
199200 default :
200- DefaultUpdateHandler . ForEach ( async k => await k ( client , update , ctx ) ) ;
201+ // ReSharper disable once AsyncVoidLambda
202+ DefaultUpdateHandler . ForEach ( async k => await k ! ( client , update , ctx ) ) ;
201203 break ;
202204 }
203205 }
204206 catch ( Exception ex )
205207 {
206- _logger . Log ( LogLevel . Error , new EventId ( ) , ex , "Catched exception in UpdateHandler: " ) ;
208+ if ( ex is KeyNotFoundException )
209+ Logger . Log ( LogLevel . Warning , new EventId ( ) , ex , "Key not found: " ) ;
210+ else
211+ Logger . Log ( LogLevel . Error , new EventId ( ) , ex , "Caught exception in UpdateHandler: " ) ;
207212 }
208213 }
209214
@@ -220,15 +225,15 @@ public async Task StartAsync(CancellationToken cancellationToken)
220225 UpdateHandler ,
221226 Config . errorHandler ?? ( ( _ , ex , _ ) =>
222227 {
223- _logger . LogError ( ex , "Catched error in telegram bot working: " ) ;
228+ Logger . LogError ( ex , "Catched error in telegram bot working: " ) ;
224229 return Task . CompletedTask ;
225230 } ) ,
226231 Config . ReceiverOptions ,
227232 cancellationToken ) ;
228233 }
229234 catch ( Exception ex )
230235 {
231- _logger . Log ( LogLevel . Critical , new EventId ( ) , ex , "Failed to start. Catched exception: " ) ;
236+ Logger . Log ( LogLevel . Critical , new EventId ( ) , ex , "Failed to start. Catched exception: " ) ;
232237 }
233238 }
234239
@@ -240,7 +245,7 @@ public async Task StopAsync(CancellationToken cancellationToken)
240245 }
241246 catch ( Exception ex )
242247 {
243- _logger . LogCritical ( ex , "Failed to stop. Exception: " ) ;
248+ Logger . LogCritical ( ex , "Failed to stop. Exception: " ) ;
244249 }
245250 }
246251}
0 commit comments