33using System . ComponentModel ;
44using System . Net ;
55using System . Net . Http ;
6+ using System . Runtime . InteropServices ;
67using System . Threading ;
78using System . Threading . Tasks ;
89using Telegram . Bot ;
910using Telegram . Bot . Exceptions ;
1011using Telegram . Bot . Polling ;
1112using Telegram . Bot . Types ;
1213
14+
1315namespace TelegramBotBase . Base ;
1416
1517/// <summary>
1618/// Base class for message handling
1719/// </summary>
1820public class MessageClient
1921{
22+ private EventHandlerList Events { get ; } = new ( ) ;
23+
2024 private static readonly object EvOnMessageLoop = new ( ) ;
2125 private static readonly object EvOnReceiveError = new ( ) ;
2226
23- private static object __evOnMessage = new ( ) ;
27+ protected CancellationTokenSource _cancellationTokenSource ;
28+
29+ public string ApiKey { get ; }
2430
25- private static object __evOnMessageEdit = new ( ) ;
31+ public ITelegramBotClient TelegramClient { get ; set ; }
2632
27- private static object __evCallbackQuery = new ( ) ;
2833
29- private CancellationTokenSource _cancellationTokenSource ;
3034
3135 /// <summary>
3236 /// Indicates if all pending Telegram.Bot.Types.Updates should be thrown out before
@@ -41,16 +45,12 @@ public MessageClient(string apiKey)
4145 {
4246 ApiKey = apiKey ;
4347 TelegramClient = new TelegramBotClient ( apiKey ) ;
44-
45- Prepare ( ) ;
4648 }
4749
4850 public MessageClient ( string apiKey , HttpClient proxy )
4951 {
5052 ApiKey = apiKey ;
5153 TelegramClient = new TelegramBotClient ( apiKey , proxy ) ;
52-
53- Prepare ( ) ;
5454 }
5555
5656
@@ -68,8 +68,6 @@ public MessageClient(string apiKey, Uri proxyUrl, NetworkCredential credential =
6868 ) ;
6969
7070 TelegramClient = new TelegramBotClient ( apiKey , httpClient ) ;
71-
72- Prepare ( ) ;
7371 }
7472
7573 /// <summary>
@@ -89,62 +87,48 @@ public MessageClient(string apiKey, string proxyHost, int proxyPort)
8987 ) ;
9088
9189 TelegramClient = new TelegramBotClient ( apiKey , httpClient ) ;
92-
93- Prepare ( ) ;
9490 }
9591
9692
9793 public MessageClient ( string apiKey , TelegramBotClient client )
9894 {
9995 ApiKey = apiKey ;
10096 TelegramClient = client ;
101-
102- Prepare ( ) ;
103- }
104-
105-
106- public string ApiKey { get ; }
107-
108- public ITelegramBotClient TelegramClient { get ; set ; }
109-
110- private EventHandlerList Events { get ; } = new ( ) ;
111-
112-
113- public void Prepare ( )
114- {
115- TelegramClient . Timeout = new TimeSpan ( 0 , 0 , 30 ) ;
11697 }
11798
11899
119- public void StartReceiving ( )
100+ public virtual void StartReceiving ( )
120101 {
121102 _cancellationTokenSource = new CancellationTokenSource ( ) ;
122103
123104 var receiverOptions = new ReceiverOptions ( ) ;
124105
125106 receiverOptions . ThrowPendingUpdates = ThrowPendingUpdates ;
126107
127- TelegramClient . StartReceiving ( HandleUpdateAsync , HandleErrorAsync , receiverOptions ,
128- _cancellationTokenSource . Token ) ;
108+ TelegramClient . StartReceiving ( HandleUpdateAsync , HandleErrorAsync , receiverOptions , _cancellationTokenSource . Token ) ;
129109 }
130110
131- public void StopReceiving ( )
111+
112+ public virtual void StopReceiving ( )
132113 {
133114 _cancellationTokenSource . Cancel ( ) ;
134115 }
135116
136117
137- public async Task HandleUpdateAsync ( ITelegramBotClient botClient , Update update , CancellationToken cancellationToken )
118+ private async Task HandleUpdateAsync ( ITelegramBotClient botClient , Update update , CancellationToken cancellationToken )
138119 {
139120 await OnMessageLoop ( new UpdateResult ( update , null ) ) ;
140121 }
141122
142- public async Task HandleErrorAsync ( ITelegramBotClient botClient , Exception exception ,
123+
124+ private async Task HandleErrorAsync ( ITelegramBotClient botClient , Exception exception ,
143125 CancellationToken cancellationToken )
144126 {
145127 await OnReceiveError ( new ErrorResult ( exception ) ) ;
146128 }
147129
130+
131+ #region "BotCommands"
148132 /// <summary>
149133 /// This will return the current list of bot commands.
150134 /// </summary>
@@ -176,6 +160,8 @@ public async Task DeleteBotCommands(BotCommandScope scope = null, string languag
176160 await TelegramClient . DeleteMyCommandsAsync ( scope , languageCode ) ;
177161 }
178162
163+ #endregion
164+
179165
180166 #region "Events"
181167
@@ -185,6 +171,7 @@ public event Async.AsyncEventHandler<UpdateResult> MessageLoop
185171 remove => Events . RemoveHandler ( EvOnMessageLoop , value ) ;
186172 }
187173
174+
188175 public async Task OnMessageLoop ( UpdateResult update )
189176 {
190177 var eventHandlers = ( Events [ EvOnMessageLoop ] as Async . AsyncEventHandler < UpdateResult > ) ? . Invoke ( this , update ) ;
@@ -202,6 +189,7 @@ public event Async.AsyncEventHandler<ErrorResult> ReceiveError
202189 remove => Events . RemoveHandler ( EvOnReceiveError , value ) ;
203190 }
204191
192+
205193 public async Task OnReceiveError ( ErrorResult update )
206194 {
207195 var eventHandlers = ( Events [ EvOnReceiveError ] as Async . AsyncEventHandler < ErrorResult > ) ? . Invoke ( this , update ) ;
@@ -225,4 +213,5 @@ public async Task OnReceiveError(ErrorResult update)
225213 }
226214
227215 #endregion
216+
228217}
0 commit comments