[DOCS] Write examples for middleware message loop
This commit is contained in:
parent
365845f832
commit
12c3e66fb8
47
Examples/MiddlewareBaseBot/Forms/StartForm.cs
Normal file
47
Examples/MiddlewareBaseBot/Forms/StartForm.cs
Normal file
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,4 +1,4 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<OutputType>Exe</OutputType>
|
<OutputType>Exe</OutputType>
|
||||||
|
|||||||
195
Examples/MiddlewareBaseBot/Program.cs
Normal file
195
Examples/MiddlewareBaseBot/Program.cs
Normal file
@ -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("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<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("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<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("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<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("6989687640:AAFGnjKCa9OOpVyVAYuSpt-axjCHKWpA0sI")
|
||||||
|
.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("6989687640:AAFGnjKCa9OOpVyVAYuSpt-axjCHKWpA0sI")
|
||||||
|
.MiddlewareMessageLoop(
|
||||||
|
messageLoop =>
|
||||||
|
messageLoop
|
||||||
|
.UseLoad())
|
||||||
|
.WithStartForm<StartForm>()
|
||||||
|
.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<StartForm>()
|
||||||
|
.NoProxy()
|
||||||
|
.DefaultCommands()
|
||||||
|
.NoSerialization()
|
||||||
|
.UsePersian()
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
return bot;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user