Joining both message loops for easier maintainability

This commit is contained in:
FlorianDahn 2021-02-28 17:44:35 +01:00
parent adc18891e0
commit 884aa60fac

View File

@ -230,7 +230,7 @@ namespace TelegramBotBase
ds?.OnMessageReceived(new MessageReceivedEventArgs(e.Message)); ds?.OnMessageReceived(new MessageReceivedEventArgs(e.Message));
await Client_TryMessage(sender, e); await Client_Loop(sender, e);
} }
catch (Telegram.Bot.Exceptions.ApiRequestException ex) catch (Telegram.Bot.Exceptions.ApiRequestException ex)
{ {
@ -243,14 +243,86 @@ namespace TelegramBotBase
} }
} }
private async Task Client_TryMessage(object sender, MessageResult e)
//private async Task Client_TryMessage(object sender, MessageResult e)
//{
// DeviceSession ds = e.Device;
// if (ds == null)
// {
// ds = await this.Sessions.StartSession<T>(e.DeviceId);
// e.Device = ds;
// ds.LastMessage = e.Message;
// OnSessionBegins(new SessionBeginEventArgs(e.DeviceId, ds));
// }
// ds.LastAction = DateTime.Now;
// ds.LastMessage = e.Message;
// //Is this a bot command ?
// if (e.IsBotCommand && this.BotCommands.Count(a => "/" + a.Command == e.BotCommand) > 0)
// {
// var sce = new BotCommandEventArgs(e.BotCommand, e.BotCommandParameters, e.Message, ds.DeviceId, ds);
// await OnBotCommand(sce);
// if (sce.Handled)
// return;
// }
// FormBase activeForm = null;
// int i = 0;
// //Should formulars get navigated (allow maximum of 10, to dont get loops)
// do
// {
// i++;
// //Reset navigation
// ds.FormSwitched = false;
// activeForm = ds.ActiveForm;
// //Pre Loading Event
// await activeForm.PreLoad(e);
// //Send Load event to controls
// await activeForm.LoadControls(e);
// //Loading Event
// await activeForm.Load(e);
// //Is Attachment ? (Photo, Audio, Video, Contact, Location, Document)
// if (e.Message.Type == Telegram.Bot.Types.Enums.MessageType.Contact | e.Message.Type == Telegram.Bot.Types.Enums.MessageType.Document | e.Message.Type == Telegram.Bot.Types.Enums.MessageType.Location |
// e.Message.Type == Telegram.Bot.Types.Enums.MessageType.Photo | e.Message.Type == Telegram.Bot.Types.Enums.MessageType.Video | e.Message.Type == Telegram.Bot.Types.Enums.MessageType.Audio)
// {
// await activeForm.SentData(new DataResult(e));
// }
// //Render Event
// if (!ds.FormSwitched)
// {
// await activeForm.RenderControls(e);
// await activeForm.Render(e);
// }
// e.IsFirstHandler = false;
// } while (ds.FormSwitched && i < this.GetSetting(eSettings.NavigationMaximum, 10));
//}
private async Task Client_Loop(object sender, MessageResult e)
{ {
DeviceSession ds = e.Device; DeviceSession ds = e.Device;
if (ds == null) if (ds == null)
{ {
ds = await this.Sessions.StartSession<T>(e.DeviceId); ds = await this.Sessions.StartSession<T>(e.DeviceId);
e.Device = ds; e.Device = ds;
ds.LastMessage = e.Message; ds.LastMessage = e.Message;
OnSessionBegins(new SessionBeginEventArgs(e.DeviceId, ds)); OnSessionBegins(new SessionBeginEventArgs(e.DeviceId, ds));
@ -299,9 +371,34 @@ namespace TelegramBotBase
await activeForm.SentData(new DataResult(e)); await activeForm.SentData(new DataResult(e));
} }
//Render Event //Action Event
if (!ds.FormSwitched) if (!ds.FormSwitched)
{ {
if (e.IsAction)
{
//Send Action event to controls
await activeForm.ActionControls(e);
//Send Action event to form itself
await activeForm.Action(e);
if (!e.Handled)
{
var uhc = new UnhandledCallEventArgs(e.Message.Text, e.RawData, ds.DeviceId, e.MessageId, e.Message, ds);
OnUnhandledCall(uhc);
if (uhc.Handled)
{
e.Handled = true;
if (!ds.FormSwitched)
{
break;
}
}
}
}
//Render Event
await activeForm.RenderControls(e); await activeForm.RenderControls(e);
await activeForm.Render(e); await activeForm.Render(e);
@ -363,12 +460,12 @@ namespace TelegramBotBase
//When form has been switched due navigation within the edit method, reopen Client_Message //When form has been switched due navigation within the edit method, reopen Client_Message
if (ds.FormSwitched) if (ds.FormSwitched)
{ {
await Client_TryMessage(sender, e); await Client_Loop(sender, e);
} }
} }
private void Client_Action(object sender, MessageResult e) private async void Client_Action(object sender, MessageResult e)
{ {
try try
{ {
@ -380,7 +477,7 @@ namespace TelegramBotBase
OnMessage(new MessageIncomeEventArgs(e.DeviceId, ds, e)); OnMessage(new MessageIncomeEventArgs(e.DeviceId, ds, e));
} }
Client_TryAction(sender, e); await Client_Loop(sender, e);
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -389,80 +486,80 @@ namespace TelegramBotBase
} }
} }
private async void Client_TryAction(object sender, MessageResult e) //private async void Client_TryAction(object sender, MessageResult e)
{ //{
DeviceSession ds = e.Device; // DeviceSession ds = e.Device;
if (ds == null) // if (ds == null)
{ // {
ds = await this.Sessions.StartSession<T>(e.DeviceId); // ds = await this.Sessions.StartSession<T>(e.DeviceId);
e.Device = ds; // e.Device = ds;
} // }
ds.LastAction = DateTime.Now; // ds.LastAction = DateTime.Now;
ds.LastMessage = e.Message; // ds.LastMessage = e.Message;
FormBase activeForm = null; // FormBase activeForm = null;
int i = 0; // int i = 0;
//Should formulars get navigated (allow maximum of 10, to dont get loops) // //Should formulars get navigated (allow maximum of 10, to dont get loops)
do // do
{ // {
i++; // i++;
//Reset navigation // //Reset navigation
ds.FormSwitched = false; // ds.FormSwitched = false;
activeForm = ds.ActiveForm; // activeForm = ds.ActiveForm;
//Pre Loading Event // //Pre Loading Event
await activeForm.PreLoad(e); // await activeForm.PreLoad(e);
//Send Load event to controls // //Send Load event to controls
await activeForm.LoadControls(e); // await activeForm.LoadControls(e);
//Loading Event // //Loading Event
await activeForm.Load(e); // await activeForm.Load(e);
//Action Event // //Action Event
if (!ds.FormSwitched) // if (!ds.FormSwitched)
{ // {
//Send Action event to controls // //Send Action event to controls
await activeForm.ActionControls(e); // await activeForm.ActionControls(e);
//Send Action event to form itself // //Send Action event to form itself
await activeForm.Action(e); // await activeForm.Action(e);
if (!e.Handled) // if (!e.Handled)
{ // {
var uhc = new UnhandledCallEventArgs(e.Message.Text, e.RawData, ds.DeviceId, e.MessageId, e.Message, ds); // var uhc = new UnhandledCallEventArgs(e.Message.Text, e.RawData, ds.DeviceId, e.MessageId, e.Message, ds);
OnUnhandledCall(uhc); // OnUnhandledCall(uhc);
if (uhc.Handled) // if (uhc.Handled)
{ // {
e.Handled = true; // e.Handled = true;
if (!ds.FormSwitched) // if (!ds.FormSwitched)
{ // {
break; // break;
} // }
} // }
} // }
} // }
//Render Event // //Render Event
if (!ds.FormSwitched) // if (!ds.FormSwitched)
{ // {
await activeForm.RenderControls(e); // await activeForm.RenderControls(e);
await activeForm.Render(e); // await activeForm.Render(e);
} // }
e.IsFirstHandler = false; // e.IsFirstHandler = false;
} while (ds.FormSwitched && i < this.GetSetting(eSettings.NavigationMaximum, 10)); // } while (ds.FormSwitched && i < this.GetSetting(eSettings.NavigationMaximum, 10));
} //}
/// <summary> /// <summary>
/// This method will update all local created bot commands to the botfather. /// This method will update all local created bot commands to the botfather.