diff --git a/Examples/EFCoreBot/StartForm.cs b/Examples/EFCoreBot/StartForm.cs index 7081c1a..1bfee77 100644 --- a/Examples/EFCoreBot/StartForm.cs +++ b/Examples/EFCoreBot/StartForm.cs @@ -33,4 +33,4 @@ public class StartForm : FormBase user.LastMessage = string.IsNullOrWhiteSpace(message.MessageText) ? "" : message.MessageText; await _dbContext.SaveChangesAsync(); } -} \ No newline at end of file +} diff --git a/TelegramBotBase/Base/Async.cs b/TelegramBotBase/Base/Async.cs index 9efe5be..260c7a1 100644 --- a/TelegramBotBase/Base/Async.cs +++ b/TelegramBotBase/Base/Async.cs @@ -7,7 +7,7 @@ namespace TelegramBotBase.Base; public static class Async { - public delegate Task AsyncEventHandler(object sender, TEventArgs e) where TEventArgs : EventArgs; + public delegate Task AsyncEventHandler(object sender, TEventArgs e) where TEventArgs : EventArgs; public static IEnumerable> GetHandlers( this AsyncEventHandler handler) @@ -24,4 +24,4 @@ public static class Async handler.GetHandlers() .Select(handleAsync => handleAsync(sender, e))); } -} \ No newline at end of file +} diff --git a/TelegramBotBase/Base/MessageClient.cs b/TelegramBotBase/Base/MessageClient.cs index a47ea16..99c45db 100644 --- a/TelegramBotBase/Base/MessageClient.cs +++ b/TelegramBotBase/Base/MessageClient.cs @@ -41,7 +41,6 @@ public class MessageClient ApiKey = apiKey; TelegramClient = new TelegramBotClient(apiKey, proxy); - Prepare(); } @@ -124,11 +123,9 @@ public class MessageClient } - public Task HandleUpdateAsync(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken) + public async Task HandleUpdateAsync(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken) { - OnMessageLoop(new UpdateResult(update, null)); - - return Task.CompletedTask; + await OnMessageLoop(new UpdateResult(update, null)); } public Task HandleErrorAsync(ITelegramBotClient botClient, Exception exception, @@ -161,6 +158,7 @@ public class MessageClient /// This will set your bot commands to the given list. /// /// + /// /// public async Task SetBotCommands(List botcommands, BotCommandScope scope = null, string languageCode = null) @@ -186,10 +184,10 @@ public class MessageClient remove => Events.RemoveHandler(EvOnMessageLoop, value); } - public void OnMessageLoop(UpdateResult update) + public async Task OnMessageLoop(UpdateResult update) { - (Events[EvOnMessageLoop] as Async.AsyncEventHandler)?.Invoke(this, update); + await (Events[EvOnMessageLoop] as Async.AsyncEventHandler)?.Invoke(this, update); } #endregion -} \ No newline at end of file +} diff --git a/TelegramBotBase/BotBase.cs b/TelegramBotBase/BotBase.cs index 3ac4f52..8e6fcad 100644 --- a/TelegramBotBase/BotBase.cs +++ b/TelegramBotBase/BotBase.cs @@ -108,32 +108,40 @@ public class BotBase private async Task Client_MessageLoop(object sender, UpdateResult e) { - var ds = Sessions.GetSession(e.DeviceId); - if (ds == null) + try { - ds = Sessions.StartSession(e.DeviceId).GetAwaiter().GetResult(); - e.Device = ds; - ds.LastMessage = e.RawData.Message; + var ds = Sessions.GetSession(e.DeviceId); + if (ds == null) + { + ds = await Sessions.StartSession(e.DeviceId); + e.Device = ds; + ds.LastMessage = e.RawData.Message; - OnSessionBegins(new SessionBeginEventArgs(e.DeviceId, ds)); + OnSessionBegins(new SessionBeginEventArgs(e.DeviceId, ds)); + } + + var mr = new MessageResult(e.RawData); + + var i = 0; + + //Should formulars get navigated (allow maximum of 10, to dont get loops) + do + { + i++; + + //Reset navigation + ds.FormSwitched = false; + + await MessageLoopFactory.MessageLoop(this, ds, e, mr); + + mr.IsFirstHandler = false; + } while (ds.FormSwitched && i < GetSetting(ESettings.NavigationMaximum, 10)); } - - var mr = new MessageResult(e.RawData); - - var i = 0; - - //Should formulars get navigated (allow maximum of 10, to dont get loops) - do + catch (Exception ex) { - i++; - - //Reset navigation - ds.FormSwitched = false; - - await MessageLoopFactory.MessageLoop(this, ds, e, mr); - - mr.IsFirstHandler = false; - } while (ds.FormSwitched && i < GetSetting(ESettings.NavigationMaximum, 10)); + var ds = Sessions.GetSession(e.DeviceId); + OnException(new SystemExceptionEventArgs(e.Message.Text, e.DeviceId, ds, ex)); + } } @@ -390,7 +398,7 @@ public class BotBase } /// - /// Will be called if no form handeled this call + /// Will be called if no form handled this call /// public event EventHandler UnhandledCall { diff --git a/TelegramBotBase/Builder/BotBaseBuilder.cs b/TelegramBotBase/Builder/BotBaseBuilder.cs index ffd2d58..c20368e 100644 --- a/TelegramBotBase/Builder/BotBaseBuilder.cs +++ b/TelegramBotBase/Builder/BotBaseBuilder.cs @@ -41,24 +41,24 @@ public class BotBaseBuilder : IAPIKeySelectionStage, IMessageLoopSelectionStage, public BotBase Build() { - var bb = new BotBase + var bot = new BotBase { ApiKey = _apiKey, StartFormFactory = _factory, Client = _client }; - bb.Sessions.Client = bb.Client; + bot.Sessions.Client = bot.Client; - bb.BotCommandScopes = BotCommandScopes; + bot.BotCommandScopes = BotCommandScopes; - bb.StateMachine = _statemachine; + bot.StateMachine = _statemachine; - bb.MessageLoopFactory = _messageLoopFactory; + bot.MessageLoopFactory = _messageLoopFactory; - bb.MessageLoopFactory.UnhandledCall += bb.MessageLoopFactory_UnhandledCall; + bot.MessageLoopFactory.UnhandledCall += bot.MessageLoopFactory_UnhandledCall; - return bb; + return bot; } public static IAPIKeySelectionStage Create() @@ -75,10 +75,10 @@ public class BotBaseBuilder : IAPIKeySelectionStage, IMessageLoopSelectionStage, } - public IBuildingStage QuickStart(string apiKey, Type StartForm) + public IBuildingStage QuickStart(string apiKey, Type startForm) { _apiKey = apiKey; - _factory = new DefaultStartFormFactory(StartForm); + _factory = new DefaultStartFormFactory(startForm); DefaultMessageLoop(); @@ -113,10 +113,10 @@ public class BotBaseBuilder : IAPIKeySelectionStage, IMessageLoopSelectionStage, return this; } - public IBuildingStage QuickStart(string apiKey, IStartFormFactory StartFormFactory) + public IBuildingStage QuickStart(string apiKey, IStartFormFactory startFormFactory) { _apiKey = apiKey; - _factory = StartFormFactory; + _factory = startFormFactory; DefaultMessageLoop(); @@ -218,7 +218,7 @@ public class BotBaseBuilder : IAPIKeySelectionStage, IMessageLoopSelectionStage, TelegramClient = { Timeout = new TimeSpan(0, 1, 0) - } + }, }; return this; } @@ -369,4 +369,4 @@ public class BotBaseBuilder : IAPIKeySelectionStage, IMessageLoopSelectionStage, } #endregion -} \ No newline at end of file +} diff --git a/TelegramBotBase/Form/AutoCleanForm.cs b/TelegramBotBase/Form/AutoCleanForm.cs index d920488..e994ef3 100644 --- a/TelegramBotBase/Form/AutoCleanForm.cs +++ b/TelegramBotBase/Form/AutoCleanForm.cs @@ -219,7 +219,7 @@ public class AutoCleanForm : FormBase var retryAfterSeconds = ex.InnerExceptions .Where(e => e is ApiRequestException apiEx && apiEx.ErrorCode == 429) .Max(e => ((ApiRequestException)e).Parameters.RetryAfter) ?? 0; - retryAfterTask = Task.Delay(retryAfterSeconds * 1000); + retryAfterTask = Task.Delay(retryAfterSeconds * 1000, cts.Token); } //deletedMessages.AsParallel().ForAll(i => Device.OnMessageDeleted(new MessageDeletedEventArgs(i))); @@ -236,4 +236,4 @@ public class AutoCleanForm : FormBase OldMessages.Clear(); } -} \ No newline at end of file +} diff --git a/TelegramBotBase/MessageLoops/FormBaseMessageLoop.cs b/TelegramBotBase/MessageLoops/FormBaseMessageLoop.cs index 7a06466..f1a2d39 100644 --- a/TelegramBotBase/MessageLoops/FormBaseMessageLoop.cs +++ b/TelegramBotBase/MessageLoops/FormBaseMessageLoop.cs @@ -107,7 +107,7 @@ public class FormBaseMessageLoop : IMessageLoopFactory } /// - /// Will be called if no form handeled this call + /// Will be called if no form handled this call /// public event EventHandler UnhandledCall { @@ -119,4 +119,4 @@ public class FormBaseMessageLoop : IMessageLoopFactory { (_events[EvUnhandledCall] as EventHandler)?.Invoke(this, e); } -} \ No newline at end of file +} diff --git a/TelegramBotBase/MessageLoops/FullMessageLoop.cs b/TelegramBotBase/MessageLoops/FullMessageLoop.cs index 69c2105..b7855c6 100644 --- a/TelegramBotBase/MessageLoops/FullMessageLoop.cs +++ b/TelegramBotBase/MessageLoops/FullMessageLoop.cs @@ -100,7 +100,7 @@ public class FullMessageLoop : IMessageLoopFactory } /// - /// Will be called if no form handeled this call + /// Will be called if no form handled this call /// public event EventHandler UnhandledCall { @@ -112,4 +112,4 @@ public class FullMessageLoop : IMessageLoopFactory { (_events[EvUnhandledCall] as EventHandler)?.Invoke(this, e); } -} \ No newline at end of file +} diff --git a/TelegramBotBase/MessageLoops/MinimalMessageLoop.cs b/TelegramBotBase/MessageLoops/MinimalMessageLoop.cs index 74b5c83..8f72f15 100644 --- a/TelegramBotBase/MessageLoops/MinimalMessageLoop.cs +++ b/TelegramBotBase/MessageLoops/MinimalMessageLoop.cs @@ -31,7 +31,7 @@ public class MinimalMessageLoop : IMessageLoopFactory } /// - /// Will be called if no form handeled this call + /// Will be called if no form handled this call /// public event EventHandler UnhandledCall { @@ -43,4 +43,4 @@ public class MinimalMessageLoop : IMessageLoopFactory { (_events[EvUnhandledCall] as EventHandler)?.Invoke(this, e); } -} \ No newline at end of file +}