From 12c3e66fb81caf500aeb887da7e19b0d8f59c00f Mon Sep 17 00:00:00 2001 From: AmirAbbas Date: Wed, 1 Nov 2023 14:00:31 +0330 Subject: [PATCH] [DOCS] Write examples for middleware message loop --- Examples/MiddlewareBaseBot/Forms/StartForm.cs | 47 +++++ .../MiddlewareBaseBot.csproj | 2 +- Examples/MiddlewareBaseBot/Program.cs | 195 ++++++++++++++++++ 3 files changed, 243 insertions(+), 1 deletion(-) create mode 100644 Examples/MiddlewareBaseBot/Forms/StartForm.cs create mode 100644 Examples/MiddlewareBaseBot/Program.cs diff --git a/Examples/MiddlewareBaseBot/Forms/StartForm.cs b/Examples/MiddlewareBaseBot/Forms/StartForm.cs new file mode 100644 index 0000000..1395a3a --- /dev/null +++ b/Examples/MiddlewareBaseBot/Forms/StartForm.cs @@ -0,0 +1,47 @@ +using TelegramBotBase.Base; +using TelegramBotBase.Form; + +internal sealed class StartForm : FormBase +{ + public override async Task PreLoad(MessageResult message) + { + await this.Device.Send("PreLoad"); + + await Task.Delay(200); + } + + public override async Task Load(MessageResult message) + { + await this.Device.Send("Load"); + + await Task.Delay(200); + } + + public override async Task Edited(MessageResult message) + { + await this.Device.Send("Edited"); + + await Task.Delay(200); + } + + public override async Task Action(MessageResult message) + { + await this.Device.Send("Action"); + + await Task.Delay(200); + } + + public override async Task SentData(DataResult data) + { + await this.Device.Send("SentData"); + + await Task.Delay(200); + } + + public override async Task Render(MessageResult message) + { + await this.Device.Send("Render"); + + await Task.Delay(200); + } +} \ No newline at end of file diff --git a/Examples/MiddlewareBaseBot/MiddlewareBaseBot.csproj b/Examples/MiddlewareBaseBot/MiddlewareBaseBot.csproj index 6e1e8e9..7134144 100644 --- a/Examples/MiddlewareBaseBot/MiddlewareBaseBot.csproj +++ b/Examples/MiddlewareBaseBot/MiddlewareBaseBot.csproj @@ -1,4 +1,4 @@ - + Exe diff --git a/Examples/MiddlewareBaseBot/Program.cs b/Examples/MiddlewareBaseBot/Program.cs new file mode 100644 index 0000000..2ea2f56 --- /dev/null +++ b/Examples/MiddlewareBaseBot/Program.cs @@ -0,0 +1,195 @@ +using Telegram.Bot.Types.Enums; +using TelegramBotBase; +using TelegramBotBase.Builder; +using TelegramBotBase.MessageLoops.Extensions; + +public class Program +{ + private static async Task Main(string[] args) + { + var bot = GetPhotoBot(); + + await bot.Start(); + + Console.WriteLine("Bot started :)"); + + Console.ReadLine(); + } + + /// + /// Creates a bot with middleware message loop and authentication for admin user + /// + private static BotBase GetAdminBot() + { + var bot = BotBaseBuilder + .Create() + .WithAPIKey("6989687640:AAFGnjKCa9OOpVyVAYuSpt-axjCHKWpA0sI") + .MiddlewareMessageLoop( + messageLoop => + messageLoop + .Use(async (container, next) => + { + var updateResult = container.UpdateResult; + if (updateResult.Message is not null) + { + if (updateResult.Message.From is not null) + { + var fromId = updateResult.Message.From.Id; + + if (fromId == 1) + { + await next(); + } + } + } + + return; + }) + .UseValidUpdateTypes( + UpdateType.Message, + UpdateType.EditedMessage, + UpdateType.CallbackQuery) + .UseBotCommands() + .UseForms()) + .WithStartForm() + .NoProxy() + .DefaultCommands() + .NoSerialization() + .UsePersian() + .Build(); + + return bot; + } + + /// + /// Creates a bot with middleware message loop for handle inline queries + /// + private static BotBase GetInlineQueryBot() + { + var bot = BotBaseBuilder + .Create() + .WithAPIKey("6989687640:AAFGnjKCa9OOpVyVAYuSpt-axjCHKWpA0sI") + .MiddlewareMessageLoop( + messageLoop => + messageLoop + .UseValidUpdateTypes(UpdateType.InlineQuery) + .Use(async (container, next) => + { + var query = container.UpdateResult.RawData.InlineQuery.Query; + + if (!string.IsNullOrWhiteSpace(query)) + { + // logic + + await next(); + } + + return; + })) + .WithStartForm() + .NoProxy() + .DefaultCommands() + .NoSerialization() + .UsePersian() + .Build(); + + return bot; + } + + /// + /// Creates a bot with middleware message loop like form base message loop + /// + private static BotBase GetFormBaseBot() + { + var bot = BotBaseBuilder + .Create() + .WithAPIKey("6989687640:AAFGnjKCa9OOpVyVAYuSpt-axjCHKWpA0sI") + .MiddlewareMessageLoop( + messageLoop => + messageLoop + .UseValidUpdateTypes( + UpdateType.Message, + UpdateType.EditedMessage, + UpdateType.CallbackQuery) + .UseBotCommands() + .UseForms()) + // OR instead of UseForms + // .UsePreLoad() + // .UseLoad() + // .UseAllAttachments() + // .UseActions() + // .UseRender() + .WithStartForm() + .NoProxy() + .DefaultCommands() + .NoSerialization() + .UsePersian() + .Build(); + + return bot; + } + + /// + /// Creates a bot with middleware message loop like form base message loop + /// + private static BotBase GetFullBot() + { + var bot = BotBaseBuilder + .Create() + .WithAPIKey("6989687640:AAFGnjKCa9OOpVyVAYuSpt-axjCHKWpA0sI") + .MiddlewareMessageLoop( + messageLoop => + messageLoop + .UseBotCommands() + .UseForms()) + .WithStartForm() + .NoProxy() + .DefaultCommands() + .NoSerialization() + .UsePersian() + .Build(); + + return bot; + } + + /// + /// Creates a bot with middleware message loop like minimal message loop + /// + private static BotBase GetMinimalBot() + { + var bot = BotBaseBuilder + .Create() + .WithAPIKey("6989687640:AAFGnjKCa9OOpVyVAYuSpt-axjCHKWpA0sI") + .MiddlewareMessageLoop( + messageLoop => + messageLoop + .UseLoad()) + .WithStartForm() + .NoProxy() + .DefaultCommands() + .NoSerialization() + .UsePersian() + .Build(); + + return bot; + } + + private static BotBase GetPhotoBot() + { + var bot = BotBaseBuilder + .Create() + .WithAPIKey("6989687640:AAFGnjKCa9OOpVyVAYuSpt-axjCHKWpA0sI") + .MiddlewareMessageLoop( + messageLoop => + messageLoop + .UseAttachments(MessageType.Photo)) + .WithStartForm() + .NoProxy() + .DefaultCommands() + .NoSerialization() + .UsePersian() + .Build(); + + return bot; + } +} \ No newline at end of file