Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Middleware-based message loop #49

Open
wants to merge 12 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions Examples/MiddlewareBaseBot/Forms/StartForm.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
14 changes: 14 additions & 0 deletions Examples/MiddlewareBaseBot/MiddlewareBaseBot.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\TelegramBotBase\TelegramBotBase.csproj" />
</ItemGroup>

</Project>
195 changes: 195 additions & 0 deletions Examples/MiddlewareBaseBot/Program.cs
Original file line number Diff line number Diff line change
@@ -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();
}

/// <summary>
/// Creates a bot with middleware message loop and authentication for admin user
/// </summary>
private static BotBase GetAdminBot()
{
var bot = BotBaseBuilder
.Create()
.WithAPIKey(Environment.GetEnvironmentVariable("API_KEY") ?? throw new Exception("API_KEY is not set"))
.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<StartForm>()
.NoProxy()
.DefaultCommands()
.NoSerialization()
.UsePersian()
.Build();

return bot;
}

/// <summary>
/// Creates a bot with middleware message loop for handle inline queries
/// </summary>
private static BotBase GetInlineQueryBot()
{
var bot = BotBaseBuilder
.Create()
.WithAPIKey(Environment.GetEnvironmentVariable("API_KEY") ?? throw new Exception("API_KEY is not set"))
.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<StartForm>()
.NoProxy()
.DefaultCommands()
.NoSerialization()
.UsePersian()
.Build();

return bot;
}

/// <summary>
/// Creates a bot with middleware message loop like form base message loop
/// </summary>
private static BotBase GetFormBaseBot()
{
var bot = BotBaseBuilder
.Create()
.WithAPIKey(Environment.GetEnvironmentVariable("API_KEY") ?? throw new Exception("API_KEY is not set"))
.MiddlewareMessageLoop(
messageLoop =>
messageLoop
.UseValidUpdateTypes(
UpdateType.Message,
UpdateType.EditedMessage,
UpdateType.CallbackQuery)
.UseBotCommands()
.UseForms())
// OR instead of UseForms
// .UsePreLoad()
// .UseLoad()
// .UseAllAttachments()
// .UseActions()
// .UseRender()
.WithStartForm<StartForm>()
.NoProxy()
.DefaultCommands()
.NoSerialization()
.UsePersian()
.Build();

return bot;
}

/// <summary>
/// Creates a bot with middleware message loop like form base message loop
/// </summary>
private static BotBase GetFullBot()
{
var bot = BotBaseBuilder
.Create()
.WithAPIKey(Environment.GetEnvironmentVariable("API_KEY") ?? throw new Exception("API_KEY is not set"))
.MiddlewareMessageLoop(
messageLoop =>
messageLoop
.UseBotCommands()
.UseForms())
.WithStartForm<StartForm>()
.NoProxy()
.DefaultCommands()
.NoSerialization()
.UsePersian()
.Build();

return bot;
}

/// <summary>
/// Creates a bot with middleware message loop like minimal message loop
/// </summary>
private static BotBase GetMinimalBot()
{
var bot = BotBaseBuilder
.Create()
.WithAPIKey(Environment.GetEnvironmentVariable("API_KEY") ?? throw new Exception("API_KEY is not set"))
.MiddlewareMessageLoop(
messageLoop =>
messageLoop
.UseLoad())
.WithStartForm<StartForm>()
.NoProxy()
.DefaultCommands()
.NoSerialization()
.UsePersian()
.Build();

return bot;
}

private static BotBase GetPhotoBot()
{
var bot = BotBaseBuilder
.Create()
.WithAPIKey(Environment.GetEnvironmentVariable("API_KEY") ?? throw new Exception("API_KEY is not set"))
.MiddlewareMessageLoop(
messageLoop =>
messageLoop
.UseAttachments(MessageType.Photo))
.WithStartForm<StartForm>()
.NoProxy()
.DefaultCommands()
.NoSerialization()
.UsePersian()
.Build();

return bot;
}
}
6 changes: 6 additions & 0 deletions TelegramBotBase/Builder/BotBaseBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@ public IStartFormSelectionStage DefaultMessageLoop()
return this;
}

public IStartFormSelectionStage MiddlewareMessageLoop(Func<MiddlewareBaseMessageLoop, MiddlewareBaseMessageLoop> messageLoopConfiguration)
{
_messageLoopFactory = messageLoopConfiguration(new MiddlewareBaseMessageLoop());

return this;
}

public IStartFormSelectionStage MinimalMessageLoop()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using TelegramBotBase.Interfaces;
using System;
using TelegramBotBase.Interfaces;
using TelegramBotBase.MessageLoops;

namespace TelegramBotBase.Builder.Interfaces;

Expand All @@ -12,6 +14,12 @@ public interface IMessageLoopSelectionStage
IStartFormSelectionStage DefaultMessageLoop();


/// <summary>
/// Choses a fully customizable middleware-based message loop.
/// </summary>
IStartFormSelectionStage MiddlewareMessageLoop(Func<MiddlewareBaseMessageLoop, MiddlewareBaseMessageLoop> messageLoopConfiguration);


/// <summary>
/// Chooses a minimalistic message loop, which catches all update types and only calls the Load function.
/// </summary>
Expand Down
65 changes: 32 additions & 33 deletions TelegramBotBase/Localizations/Persian.cs
Original file line number Diff line number Diff line change
@@ -1,37 +1,36 @@
namespace TelegramBotBase.Localizations
namespace TelegramBotBase.Localizations;

public sealed class Persian : Localization
{
public sealed class Persian : Localization
public Persian()
{
public Persian()
{
Values["Language"] = "فارسی";
Values["ButtonGrid_Title"] = "منو";
Values["ButtonGrid_NoItems"] = "هیچ آیتمی وجود ندارد.";
Values["ButtonGrid_PreviousPage"] = "◀️";
Values["ButtonGrid_NextPage"] = "▶️";
Values["ButtonGrid_CurrentPage"] = "صفحه ی {0} از {1}";
Values["ButtonGrid_SearchFeature"] = "💡 برای فیلتر کردن لیست پیام ارسال کنید. برای بازنشانی فیلتر روی 🔍 کلیک کنید.";
Values["ButtonGrid_Back"] = "بازگشت";
Values["ButtonGrid_CheckAll"] = "بررسی کردن همه";
Values["ButtonGrid_UncheckAll"] = "بررسی نکردن همه";
Values["CalendarPicker_Title"] = "تاریخ را انتخاب کنید";
Values["CalendarPicker_PreviousPage"] = "◀️";
Values["CalendarPicker_NextPage"] = "▶️";
Values["TreeView_Title"] = "گره را انتخاب کنید";
Values["TreeView_LevelUp"] = "🔼 سطح بالا";
Values["ToggleButton_On"] = "روشن";
Values["ToggleButton_Off"] = "خاموش";
Values["ToggleButton_OnIcon"] = "⚫";
Values["ToggleButton_OffIcon"] = "⚪";
Values["ToggleButton_Title"] = "تغییر وضعیت";
Values["ToggleButton_Changed"] = "انتخاب شده";
Values["MultiToggleButton_SelectedIcon"] = "✅";
Values["MultiToggleButton_Title"] = "چند تعویض";
Values["MultiToggleButton_Changed"] = "انتخاب شده";
Values["PromptDialog_Back"] = "بازگشت";
Values["ToggleButton_Changed"] = "تنظیمات تغییر کرد";
Values["ButtonGrid_SearchIcon"] = "🔍";
Values["ButtonGrid_TagIcon"] = "📁";
}
Values["Language"] = "فارسی";
Values["ButtonGrid_Title"] = "منو";
Values["ButtonGrid_NoItems"] = "هیچ آیتمی وجود ندارد.";
Values["ButtonGrid_PreviousPage"] = "◀️";
Values["ButtonGrid_NextPage"] = "▶️";
Values["ButtonGrid_CurrentPage"] = "صفحه ی {0} از {1}";
Values["ButtonGrid_SearchFeature"] = "💡 برای فیلتر کردن لیست پیام ارسال کنید. برای بازنشانی فیلتر روی 🔍 کلیک کنید.";
Values["ButtonGrid_Back"] = "بازگشت";
Values["ButtonGrid_CheckAll"] = "بررسی کردن همه";
Values["ButtonGrid_UncheckAll"] = "بررسی نکردن همه";
Values["CalendarPicker_Title"] = "تاریخ را انتخاب کنید";
Values["CalendarPicker_PreviousPage"] = "◀️";
Values["CalendarPicker_NextPage"] = "▶️";
Values["TreeView_Title"] = "گره را انتخاب کنید";
Values["TreeView_LevelUp"] = "🔼 سطح بالا";
Values["ToggleButton_On"] = "روشن";
Values["ToggleButton_Off"] = "خاموش";
Values["ToggleButton_OnIcon"] = "⚫";
Values["ToggleButton_OffIcon"] = "⚪";
Values["ToggleButton_Title"] = "تغییر وضعیت";
Values["ToggleButton_Changed"] = "انتخاب شده";
Values["MultiToggleButton_SelectedIcon"] = "✅";
Values["MultiToggleButton_Title"] = "چند تعویض";
Values["MultiToggleButton_Changed"] = "انتخاب شده";
Values["PromptDialog_Back"] = "بازگشت";
Values["ToggleButton_Changed"] = "تنظیمات تغییر کرد";
Values["ButtonGrid_SearchIcon"] = "🔍";
Values["ButtonGrid_TagIcon"] = "📁";
}
}
Loading