Compare commits
27
Commits
development
..
Lab
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
12622deee8 | ||
|
|
c6d5037700 | ||
|
|
53886fa6bf | ||
|
|
d572c353f6 | ||
|
|
b8fd3badd4 | ||
|
|
97e04f4162 | ||
|
|
51d9faa807 | ||
|
|
57f2139dba | ||
|
|
9da952aa28 | ||
|
|
45b05d301b | ||
|
|
d910b42473 | ||
|
|
952e0cb979 | ||
|
|
3c93daed35 | ||
|
|
de93598e6f | ||
|
|
d6961deb8e | ||
|
|
c821a69d57 | ||
|
|
07fe8e9074 | ||
|
|
a26c4ed858 | ||
|
|
9aab6bca76 | ||
|
|
fc1e8e84c6 | ||
|
|
aee7562424 | ||
|
|
161f191c43 | ||
|
|
d0a6b74244 | ||
|
|
976f1da602 | ||
|
|
0d3979bbab | ||
|
|
5713dac312 | ||
|
|
9f41ab352f |
@@ -8,7 +8,7 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="TelegramBotBase" Version="6.4.1" />
|
<ProjectReference Include="..\..\TelegramBotBase\TelegramBotBase.csproj"/>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -7,7 +7,7 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="TelegramBotBase" Version="6.4.1" />
|
<PackageReference Include="TelegramBotBase" Version="5.3.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -16,7 +16,6 @@ namespace BotAndWebApplication.BotStuff.Tasks
|
|||||||
.DefaultCommands()
|
.DefaultCommands()
|
||||||
.NoSerialization()
|
.NoSerialization()
|
||||||
.UseEnglish()
|
.UseEnglish()
|
||||||
.UseThreadPool()
|
|
||||||
.Build();
|
.Build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -8,12 +8,12 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.11" />
|
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.9"/>
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.11" />
|
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="6.0.9"/>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\..\TelegramBotBase\TelegramBotBase.csproj" />
|
<ProjectReference Include="..\..\TelegramBotBase\TelegramBotBase.csproj"/>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -8,7 +8,7 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="TelegramBotBase" Version="6.4.1" />
|
<ProjectReference Include="..\..\TelegramBotBase\TelegramBotBase.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -8,7 +8,7 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="TelegramBotBase" Version="6.4.1" />
|
<ProjectReference Include="..\..\TelegramBotBase\TelegramBotBase.csproj"/>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
<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" />
|
||||||
|
<ProjectReference Include="..\TelegramBotBase.Experiments\TelegramBotBase.Experiments.ActionManager.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
|
||||||
|
</Project>
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Net.WebSockets;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using TelegramBotBase.Base;
|
||||||
|
using TelegramBotBase.Form;
|
||||||
|
|
||||||
|
namespace DemoBot.Forms
|
||||||
|
{
|
||||||
|
public class HiddenForm : AutoCleanForm
|
||||||
|
{
|
||||||
|
public String value { get; set; }
|
||||||
|
|
||||||
|
public HiddenForm() {
|
||||||
|
|
||||||
|
DeleteMode = TelegramBotBase.Enums.EDeleteMode.OnLeavingForm;
|
||||||
|
DeleteSide = TelegramBotBase.Enums.EDeleteSide.Both;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override async Task Action(MessageResult message)
|
||||||
|
{
|
||||||
|
if (message.RawData != "start")
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await message.ConfirmAction("Lets go");
|
||||||
|
|
||||||
|
message.Handled = true;
|
||||||
|
|
||||||
|
var st = new StartForm();
|
||||||
|
|
||||||
|
await NavigateTo(st);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override async Task Render(MessageResult message)
|
||||||
|
{
|
||||||
|
|
||||||
|
var bf = new ButtonForm();
|
||||||
|
|
||||||
|
bf.AddButtonRow("Goto Start", "start");
|
||||||
|
|
||||||
|
value = value.Replace("_", "\\_");
|
||||||
|
|
||||||
|
await Device.Send($"Welcome to Hidden form\n\nThe given value is {value}", bf);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Net.WebSockets;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using TelegramBotBase.Base;
|
||||||
|
using TelegramBotBase.Form;
|
||||||
|
|
||||||
|
namespace DemoBot.Forms
|
||||||
|
{
|
||||||
|
public class HiddenForm_EndsWith : AutoCleanForm
|
||||||
|
{
|
||||||
|
public String value { get; set; }
|
||||||
|
|
||||||
|
public HiddenForm_EndsWith() {
|
||||||
|
|
||||||
|
DeleteMode = TelegramBotBase.Enums.EDeleteMode.OnLeavingForm;
|
||||||
|
DeleteSide = TelegramBotBase.Enums.EDeleteSide.Both;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override async Task Action(MessageResult message)
|
||||||
|
{
|
||||||
|
if (message.RawData != "start")
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await message.ConfirmAction("Lets go");
|
||||||
|
|
||||||
|
message.Handled = true;
|
||||||
|
|
||||||
|
var st = new StartForm();
|
||||||
|
|
||||||
|
await NavigateTo(st);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override async Task Render(MessageResult message)
|
||||||
|
{
|
||||||
|
|
||||||
|
var bf = new ButtonForm();
|
||||||
|
|
||||||
|
bf.AddButtonRow("Goto Start", "start");
|
||||||
|
|
||||||
|
value = value.Replace("_", "\\_");
|
||||||
|
|
||||||
|
await Device.Send($"Welcome to Hidden ends with form\n\nThe given value is {value}", bf);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using TelegramBotBase.Base;
|
||||||
|
using TelegramBotBase.Form;
|
||||||
|
|
||||||
|
namespace DemoBot.Forms
|
||||||
|
{
|
||||||
|
public class HiddenLetterForm : AutoCleanForm
|
||||||
|
{
|
||||||
|
|
||||||
|
public Guid letterId { get; set; }
|
||||||
|
|
||||||
|
public HiddenLetterForm()
|
||||||
|
{
|
||||||
|
|
||||||
|
DeleteMode = TelegramBotBase.Enums.EDeleteMode.OnLeavingForm;
|
||||||
|
DeleteSide = TelegramBotBase.Enums.EDeleteSide.Both;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override async Task Action(MessageResult message)
|
||||||
|
{
|
||||||
|
if (message.RawData != "start")
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
await message.ConfirmAction("Lets go");
|
||||||
|
|
||||||
|
message.Handled = true;
|
||||||
|
|
||||||
|
var st = new StartForm();
|
||||||
|
|
||||||
|
await NavigateTo(st);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override async Task Render(MessageResult message)
|
||||||
|
{
|
||||||
|
|
||||||
|
var bf = new ButtonForm();
|
||||||
|
|
||||||
|
bf.AddButtonRow("Goto Start", "start");
|
||||||
|
|
||||||
|
await Device.Send($"Welcome to Hidden letter form\n\nYour letter Id is: {letterId}", bf);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using TelegramBotBase.Base;
|
||||||
|
using TelegramBotBase.Form;
|
||||||
|
|
||||||
|
namespace DemoBot.Forms
|
||||||
|
{
|
||||||
|
public class HiddenOpenForm : AutoCleanForm
|
||||||
|
{
|
||||||
|
|
||||||
|
public Guid guid { get; set; }
|
||||||
|
|
||||||
|
public HiddenOpenForm()
|
||||||
|
{
|
||||||
|
|
||||||
|
DeleteMode = TelegramBotBase.Enums.EDeleteMode.OnLeavingForm;
|
||||||
|
DeleteSide = TelegramBotBase.Enums.EDeleteSide.Both;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override async Task Action(MessageResult message)
|
||||||
|
{
|
||||||
|
if (message.RawData != "start")
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await message.ConfirmAction("Lets go");
|
||||||
|
|
||||||
|
message.Handled = true;
|
||||||
|
|
||||||
|
var st = new StartForm();
|
||||||
|
|
||||||
|
await NavigateTo(st);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override async Task Render(MessageResult message)
|
||||||
|
{
|
||||||
|
|
||||||
|
var bf = new ButtonForm();
|
||||||
|
|
||||||
|
bf.AddButtonRow("Goto Start", "start");
|
||||||
|
|
||||||
|
await Device.Send($"Welcome to Hidden open form\n\nYour guid is: {guid}", bf);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using TelegramBotBase.Base;
|
||||||
|
using TelegramBotBase.Form;
|
||||||
|
|
||||||
|
namespace DemoBot.Forms
|
||||||
|
{
|
||||||
|
public class HiddenTicketForm : AutoCleanForm
|
||||||
|
{
|
||||||
|
|
||||||
|
public Guid ticketId { get; set; }
|
||||||
|
|
||||||
|
public HiddenTicketForm()
|
||||||
|
{
|
||||||
|
|
||||||
|
DeleteMode = TelegramBotBase.Enums.EDeleteMode.OnLeavingForm;
|
||||||
|
DeleteSide = TelegramBotBase.Enums.EDeleteSide.Both;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override async Task Action(MessageResult message)
|
||||||
|
{
|
||||||
|
if (message.RawData != "start")
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
await message.ConfirmAction("Lets go");
|
||||||
|
|
||||||
|
message.Handled = true;
|
||||||
|
|
||||||
|
var st = new StartForm();
|
||||||
|
|
||||||
|
await NavigateTo(st);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override async Task Render(MessageResult message)
|
||||||
|
{
|
||||||
|
|
||||||
|
var bf = new ButtonForm();
|
||||||
|
|
||||||
|
bf.AddButtonRow("Goto Start", "start");
|
||||||
|
|
||||||
|
await Device.Send($"Welcome to Hidden ticket form\n\nYour ticket Id is: {ticketId}", bf);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using TelegramBotBase.Base;
|
||||||
|
using TelegramBotBase.Form;
|
||||||
|
|
||||||
|
namespace DemoBot.Forms
|
||||||
|
{
|
||||||
|
internal class StartForm : AutoCleanForm
|
||||||
|
{
|
||||||
|
public StartForm()
|
||||||
|
{
|
||||||
|
|
||||||
|
DeleteMode = TelegramBotBase.Enums.EDeleteMode.OnLeavingForm;
|
||||||
|
DeleteSide = TelegramBotBase.Enums.EDeleteSide.Both;
|
||||||
|
|
||||||
|
Opened += StartForm_Opened;
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task StartForm_Opened(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
await Device.Send("Hey!\r\n\r\nChoose the /test command to get a message from outside.", disableNotification: true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override async Task Load(MessageResult message)
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,310 @@
|
|||||||
|
using DemoBot.Forms;
|
||||||
|
using Telegram.Bot;
|
||||||
|
using Telegram.Bot.Types.ReplyMarkups;
|
||||||
|
using TelegramBotBase.Builder;
|
||||||
|
using TelegramBotBase.Commands;
|
||||||
|
using TelegramBotBase.Form;
|
||||||
|
using TelegramBotBase.Base;
|
||||||
|
using TelegramBotBase.Experiments.ActionManager;
|
||||||
|
using TelegramBotBase.Experiments.ActionManager.Actions;
|
||||||
|
using TelegramBotBase.Experiments.ActionManager.Navigation;
|
||||||
|
using Telegram.Bot.Types;
|
||||||
|
|
||||||
|
namespace DemoBot
|
||||||
|
{
|
||||||
|
internal class Program
|
||||||
|
{
|
||||||
|
public static String Token = Environment.GetEnvironmentVariable("API_KEY") ?? throw new Exception("API_KEY is not set");
|
||||||
|
|
||||||
|
static async Task Main(string[] args)
|
||||||
|
{
|
||||||
|
|
||||||
|
//Using a custom FormBase message loop which is based on the original FormBaseMessageLoop within the framework.
|
||||||
|
//Would integrate this later into the BotBaseBuilder -> MessageLoop step.
|
||||||
|
//var cfb = new CustomFormBaseMessageLoop();
|
||||||
|
|
||||||
|
//Example 1: Long version
|
||||||
|
var eam = ExternalActionManager.Configure(config =>
|
||||||
|
{
|
||||||
|
//Waiting for input starting with 'n_'
|
||||||
|
config.AddStartsWithNavigation<HiddenForm>("n_", (a, b) =>
|
||||||
|
{
|
||||||
|
a.value = b;
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
//Minimal version, using reflection right now
|
||||||
|
config.AddStartsWithNavigation<HiddenForm>("a_", (a, b) =>
|
||||||
|
{
|
||||||
|
a.value = b;
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
//Waiting for input starting with 't_'
|
||||||
|
config.AddStartsWithNavigation(typeof(HiddenForm), "t_", (a, b) =>
|
||||||
|
{
|
||||||
|
var hf = a as HiddenForm;
|
||||||
|
if (hf == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
hf.value = b;
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
//Minimal version, using reflection right now
|
||||||
|
config.AddEndsWithNavigation<HiddenForm_EndsWith>("_u", (a, b) =>
|
||||||
|
{
|
||||||
|
a.value = b;
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
//Deserialize input and waiting for the method property to has value 'tickets'
|
||||||
|
config.AddGuidNavigation<HiddenTicketForm>("tickets", (a, b) =>
|
||||||
|
{
|
||||||
|
a.ticketId = b;
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
//Minimal version, using reflection right now
|
||||||
|
config.AddGuidNavigation<HiddenLetterForm>("letters", (a, b) =>
|
||||||
|
{
|
||||||
|
a.letterId = b;
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
//Deserialize input and waiting for the method property to has value 'open'
|
||||||
|
config.AddGuidNavigation<HiddenOpenForm>("open", (a, b) =>
|
||||||
|
{
|
||||||
|
a.guid = b;
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
//Do custom action
|
||||||
|
config.AddStartsWithAction("p_", StartsWithActionCallback_Demo);
|
||||||
|
|
||||||
|
//Do custom action
|
||||||
|
config.AddStartsWithAction("p2_", async (value, update, message) =>
|
||||||
|
{
|
||||||
|
if (message.UpdateData.CallbackQuery == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
await update.Device.ConfirmAction(message.UpdateData.CallbackQuery.Id, "Confirmed!");
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
//Example 2: Short version
|
||||||
|
var eam2 = ExternalActionManager.Configure(config =>
|
||||||
|
{
|
||||||
|
//Waiting for input starting with 'n_'
|
||||||
|
config.AddStartsWithNavigation<HiddenForm>("n_", a => a.value);
|
||||||
|
|
||||||
|
|
||||||
|
//Waiting for input starting with 'a_'
|
||||||
|
config.AddStartsWithNavigation<HiddenForm>("a_", a => a.value);
|
||||||
|
|
||||||
|
|
||||||
|
//Waiting for input starting with 't_' (Non generic version)
|
||||||
|
config.AddStartsWithNavigation(typeof(HiddenForm), "t_", a => ((HiddenForm)a).value);
|
||||||
|
|
||||||
|
|
||||||
|
//Waiting for input ending with 'n_'
|
||||||
|
config.AddEndsWithNavigation<HiddenForm_EndsWith>("_u", a => a.value);
|
||||||
|
|
||||||
|
|
||||||
|
//Deserialize input and waiting for the method property to has value 'tickets'
|
||||||
|
config.AddGuidNavigation<HiddenTicketForm>("tickets", a => a.ticketId);
|
||||||
|
|
||||||
|
|
||||||
|
//Minimal version, using reflection right now
|
||||||
|
config.AddGuidNavigation<HiddenLetterForm>("letters", a => a.letterId);
|
||||||
|
|
||||||
|
|
||||||
|
//Deserialize input and waiting for the method property to has value 'open'
|
||||||
|
config.AddGuidNavigation<HiddenOpenForm>("open", a => a.guid);
|
||||||
|
|
||||||
|
|
||||||
|
//Do custom action
|
||||||
|
config.AddStartsWithAction("p_", StartsWithActionCallback_Demo);
|
||||||
|
|
||||||
|
|
||||||
|
//Do custom action
|
||||||
|
config.AddStartsWithAction("p2_", async (value, update, message) =>
|
||||||
|
{
|
||||||
|
if (message.UpdateData.CallbackQuery == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
await update.Device.ConfirmAction(message.UpdateData.CallbackQuery.Id, "Confirmed!");
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
config.AddGuidAction("guid.test.too.long", async (g, c, u, m) =>
|
||||||
|
{
|
||||||
|
if (m.UpdateData.CallbackQuery == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
await u.Device.ConfirmAction(m.UpdateData.CallbackQuery.Id, "Confirmed!");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
var bb = BotBaseBuilder.Create()
|
||||||
|
.WithAPIKey(Token)
|
||||||
|
.ActionMessageLoop(eam2)
|
||||||
|
.WithStartForm<Forms.StartForm>()
|
||||||
|
.NoProxy()
|
||||||
|
.CustomCommands(a =>
|
||||||
|
{
|
||||||
|
a.Start("Starts the bot");
|
||||||
|
a.Add("test", "Sends a test notification");
|
||||||
|
a.Add("invalid", "Try to send an invalid data message");
|
||||||
|
|
||||||
|
})
|
||||||
|
.NoSerialization()
|
||||||
|
.UseGerman()
|
||||||
|
.UseSingleThread()
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
bb.BotCommand += Bb_BotCommand;
|
||||||
|
|
||||||
|
bb.UnhandledCall += Bb_UnhandledCall;
|
||||||
|
|
||||||
|
await bb.Start();
|
||||||
|
|
||||||
|
await bb.UploadBotCommands();
|
||||||
|
|
||||||
|
Console.WriteLine("Bot started.");
|
||||||
|
|
||||||
|
|
||||||
|
Console.ReadLine();
|
||||||
|
|
||||||
|
|
||||||
|
await bb.Stop();
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private static async Task StartsWithActionCallback_Demo(String value, UpdateResult ur, MessageResult mr)
|
||||||
|
{
|
||||||
|
if (mr.UpdateData.CallbackQuery == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
await ur.Device.ConfirmAction(mr.UpdateData.CallbackQuery.Id, "Confirmed!");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void Bb_UnhandledCall(object? sender, TelegramBotBase.Args.UnhandledCallEventArgs e)
|
||||||
|
{
|
||||||
|
|
||||||
|
Console.WriteLine($"Unhandled call: {e.RawData}");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private static async Task Bb_BotCommand(object sender, TelegramBotBase.Args.BotCommandEventArgs e)
|
||||||
|
{
|
||||||
|
|
||||||
|
var current_form = e.Device.ActiveForm;
|
||||||
|
|
||||||
|
switch (e.Command)
|
||||||
|
{
|
||||||
|
case "/start":
|
||||||
|
|
||||||
|
//Already on start form
|
||||||
|
if (current_form.GetType() == typeof(Forms.StartForm))
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var st = new Forms.StartForm();
|
||||||
|
|
||||||
|
await current_form.NavigateTo(st);
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "/test":
|
||||||
|
|
||||||
|
//Send test notification
|
||||||
|
|
||||||
|
//Test values
|
||||||
|
|
||||||
|
String max_value = "n_".PadRight(32, '1'); //Starts with
|
||||||
|
|
||||||
|
String max_value2 = "t_".PadRight(32, '2'); //Starts with
|
||||||
|
|
||||||
|
String max_value3 = "a_".PadRight(32, '3'); //Starts with
|
||||||
|
|
||||||
|
String max_value4 = "_u".PadLeft(32, '4'); //Ends with
|
||||||
|
|
||||||
|
String max_value5 = "p_".PadRight(32, '5'); //Ends with
|
||||||
|
|
||||||
|
String max_value6 = "p2_".PadRight(32, '6'); //Ends with
|
||||||
|
|
||||||
|
Guid test_value = Guid.NewGuid(); //Unhandled caller
|
||||||
|
|
||||||
|
var callback_guid = GuidNavigation.GetCallback("open", Guid.NewGuid()); //HiddenOpenForm
|
||||||
|
|
||||||
|
var callback_tickets = GuidNavigation.GetCallback("tickets", Guid.NewGuid()); //HiddenTicketForm
|
||||||
|
|
||||||
|
var callback_letters = GuidNavigation.GetCallback("letters", Guid.NewGuid()); //HiddenLetterForm
|
||||||
|
|
||||||
|
|
||||||
|
String message = $"Test notification from 'outside'\n\nTest values are:\n\nTest: {max_value}\nTest2: {max_value2}\nTest3: {max_value3}\nTest (Guid): {test_value.ToString()}\nTest (Callback Guid): {callback_guid.Value}\nTickets (Guid): {callback_tickets.Value}\nLetters (Guid): {callback_letters.Value}\n";
|
||||||
|
|
||||||
|
|
||||||
|
var tb = new TelegramBotClient(Token);
|
||||||
|
|
||||||
|
var bf = new ButtonForm();
|
||||||
|
|
||||||
|
bf.AddButtonRow(new ButtonBase("Ok", "n_ok"), new ButtonBase("Later", "n_later"));
|
||||||
|
|
||||||
|
bf.AddButtonRow("Test (n_)", max_value);
|
||||||
|
|
||||||
|
bf.AddButtonRow("Test2 (t_)", max_value2);
|
||||||
|
|
||||||
|
bf.AddButtonRow("Test3 (a_)", max_value3);
|
||||||
|
|
||||||
|
bf.AddButtonRow("Test4 (_u)", max_value4);
|
||||||
|
|
||||||
|
bf.AddButtonRow("Test5 (p_)", max_value5);
|
||||||
|
|
||||||
|
bf.AddButtonRow("Test6 (p2_)", max_value6);
|
||||||
|
|
||||||
|
bf.AddButtonRow("Test (Guid)", test_value.ToString());
|
||||||
|
|
||||||
|
bf.AddButtonRow("Test (Callback Gui)", callback_guid);
|
||||||
|
|
||||||
|
bf.AddButtonRow("Tickets", callback_tickets);
|
||||||
|
|
||||||
|
bf.AddButtonRow("Letters", callback_letters);
|
||||||
|
|
||||||
|
bf.AddButtonRow("Close", "close");
|
||||||
|
|
||||||
|
await tb.SendTextMessageAsync(e.DeviceId, message, disableNotification: true, replyMarkup: (InlineKeyboardMarkup)bf);
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "/invalid":
|
||||||
|
|
||||||
|
Guid g = Guid.NewGuid();
|
||||||
|
|
||||||
|
var tb2 = new TelegramBotClient(Token);
|
||||||
|
|
||||||
|
var bf2 = new ButtonForm();
|
||||||
|
|
||||||
|
bf2.AddButtonRow("Test test", GuidAction.GetCallback("guid.test.too.long", g));
|
||||||
|
|
||||||
|
String message2 = "This is an invalid test message.";
|
||||||
|
|
||||||
|
await tb2.SendTextMessageAsync(e.DeviceId, message2, disableNotification: true, replyMarkup: (InlineKeyboardMarkup)bf2);
|
||||||
|
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
# ExternalActionManager
|
||||||
|
|
||||||
|
Idea of this experiment is to find a good intuitive way for handling "Unhandled calls".
|
||||||
|
Right now they are "thrown" into an event handler and you have to take care over them on yourself.
|
||||||
|
|
||||||
|
|
||||||
|
Source of them would be in most cases Telegram Bot messages which has an Inlinekeyboard attached and comes from outside of the framework. (via webservice or other remote channels)
|
||||||
|
|
||||||
|
And the button press should navigate to a different form, or invoke a different action.
|
||||||
|
|
||||||
|
|
||||||
|
## How does it work ?
|
||||||
|
|
||||||
|
|
||||||
|
- Open the Bot
|
||||||
|
- Run the "/start" command
|
||||||
|
- Now you are on the only FormBase where you can get without interaction
|
||||||
|
- Use the "/test" command to invoke an "outside" message with some different buttons.
|
||||||
|
- Use them to navigate to different "hidden" forms
|
||||||
|
|
||||||
|
|
||||||
|
## Future ideas
|
||||||
|
|
||||||
|
- adding an Action for int and/or long datatypes
|
||||||
|
|
||||||
|
|
||||||
|
-
|
||||||
|
|
||||||
|
Begin: 18.01.2024
|
||||||
+13
@@ -0,0 +1,13 @@
|
|||||||
|
using TelegramBotBase.Base;
|
||||||
|
|
||||||
|
namespace TelegramBotBase.Experiments.ActionManager.Actions
|
||||||
|
{
|
||||||
|
public static class EndsWithAction_Extensions
|
||||||
|
{
|
||||||
|
public static void AddEndsWithAction(this ExternalActionManager manager, string value, Func<String, UpdateResult, MessageResult, Task> action)
|
||||||
|
{
|
||||||
|
manager.Add(new EndsWithAction(value, action));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+29
@@ -0,0 +1,29 @@
|
|||||||
|
using TelegramBotBase.Base;
|
||||||
|
using TelegramBotBase.Form;
|
||||||
|
|
||||||
|
namespace TelegramBotBase.Experiments.ActionManager.Actions
|
||||||
|
{
|
||||||
|
|
||||||
|
public class EndsWithAction : IExternalAction
|
||||||
|
{
|
||||||
|
public string SearchForString { get; set; }
|
||||||
|
|
||||||
|
public Action<FormBase, string> SetProperty { get; set; }
|
||||||
|
|
||||||
|
Func<String, UpdateResult, MessageResult, Task> Action;
|
||||||
|
|
||||||
|
|
||||||
|
public EndsWithAction(string searchFor, Func<String, UpdateResult, MessageResult, Task> action)
|
||||||
|
{
|
||||||
|
SearchForString = searchFor;
|
||||||
|
Action = action;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool DoesFit(string raw_data) => raw_data.EndsWith(SearchForString);
|
||||||
|
|
||||||
|
|
||||||
|
public async Task DoAction(UpdateResult ur, MessageResult mr) => await Action(mr.RawData, ur, mr);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+14
@@ -0,0 +1,14 @@
|
|||||||
|
using TelegramBotBase.Base;
|
||||||
|
using TelegramBotBase.Form;
|
||||||
|
|
||||||
|
namespace TelegramBotBase.Experiments.ActionManager.Actions
|
||||||
|
{
|
||||||
|
public static class GuidAction_Extensions
|
||||||
|
{
|
||||||
|
|
||||||
|
public static void AddGuidAction(this ExternalActionManager manager, string method, Func<Guid, CallbackData, UpdateResult, MessageResult, Task> action)
|
||||||
|
{
|
||||||
|
manager.Add(new GuidAction(method, action));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+85
@@ -0,0 +1,85 @@
|
|||||||
|
using TelegramBotBase.Base;
|
||||||
|
using TelegramBotBase.Form;
|
||||||
|
|
||||||
|
namespace TelegramBotBase.Experiments.ActionManager.Actions
|
||||||
|
{
|
||||||
|
public class GuidAction : IExternalAction
|
||||||
|
{
|
||||||
|
public string Method { get; set; }
|
||||||
|
|
||||||
|
CallbackData? _lastData { get; set; }
|
||||||
|
|
||||||
|
Guid? _lastValue { get; set; }
|
||||||
|
|
||||||
|
Func<Guid, CallbackData, UpdateResult, MessageResult, Task> Action;
|
||||||
|
|
||||||
|
public GuidAction(string method, Func<Guid, CallbackData, UpdateResult, MessageResult, Task> action)
|
||||||
|
{
|
||||||
|
Method = method;
|
||||||
|
Action = action;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool DoesFit(string raw_data)
|
||||||
|
{
|
||||||
|
var cd = CallbackData.Deserialize(raw_data);
|
||||||
|
|
||||||
|
if (cd == null)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (cd.Method != Method)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
Guid g;
|
||||||
|
|
||||||
|
if (Guid.TryParse(cd.Value, out g))
|
||||||
|
_lastValue = g;
|
||||||
|
|
||||||
|
_lastData = cd;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public async Task DoAction(UpdateResult ur, MessageResult mr) => await Action(_lastValue.Value, _lastData, ur, mr);
|
||||||
|
|
||||||
|
public static CallbackData GetCallback(string method, Guid guid) => new CallbackData(method, guid.ToString());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public class GuidAction<TForm> : IExternalAction
|
||||||
|
where TForm : FormBase
|
||||||
|
{
|
||||||
|
public string Method { get; set; }
|
||||||
|
|
||||||
|
Guid? _lastValue { get; set; }
|
||||||
|
|
||||||
|
Func<Guid, UpdateResult, MessageResult, Task> Action;
|
||||||
|
|
||||||
|
public GuidAction(string method, Func<Guid, UpdateResult, MessageResult, Task> action)
|
||||||
|
{
|
||||||
|
Method = method;
|
||||||
|
Action = action;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool DoesFit(string raw_data)
|
||||||
|
{
|
||||||
|
var cd = CallbackData.Deserialize(raw_data);
|
||||||
|
|
||||||
|
if (cd == null)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (cd.Method != Method)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
Guid g;
|
||||||
|
|
||||||
|
if (Guid.TryParse(cd.Value, out g))
|
||||||
|
_lastValue = g;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public async Task DoAction(UpdateResult ur, MessageResult mr) => await Action(_lastValue.Value, ur, mr);
|
||||||
|
}
|
||||||
|
}
|
||||||
+14
@@ -0,0 +1,14 @@
|
|||||||
|
using TelegramBotBase.Base;
|
||||||
|
using TelegramBotBase.Form;
|
||||||
|
|
||||||
|
namespace TelegramBotBase.Experiments.ActionManager.Actions
|
||||||
|
{
|
||||||
|
public static class Int32Action_Extensions
|
||||||
|
{
|
||||||
|
|
||||||
|
public static void AddInt32Action(this ExternalActionManager manager, string method, Func<int, CallbackData, UpdateResult, MessageResult, Task> action)
|
||||||
|
{
|
||||||
|
manager.Add(new Int32Action(method, action));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+90
@@ -0,0 +1,90 @@
|
|||||||
|
using TelegramBotBase.Base;
|
||||||
|
using TelegramBotBase.Form;
|
||||||
|
|
||||||
|
namespace TelegramBotBase.Experiments.ActionManager.Actions
|
||||||
|
{
|
||||||
|
public class Int32Action : IExternalAction
|
||||||
|
{
|
||||||
|
public string Method { get; set; }
|
||||||
|
|
||||||
|
CallbackData? _lastData { get; set; }
|
||||||
|
|
||||||
|
int? _lastValue { get; set; }
|
||||||
|
|
||||||
|
Func<int, CallbackData, UpdateResult, MessageResult, Task> Action;
|
||||||
|
|
||||||
|
public Int32Action(string method, Func<int, CallbackData, UpdateResult, MessageResult, Task> action)
|
||||||
|
{
|
||||||
|
Method = method;
|
||||||
|
Action = action;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public bool DoesFit(string raw_data)
|
||||||
|
{
|
||||||
|
var cd = CallbackData.Deserialize(raw_data);
|
||||||
|
|
||||||
|
if (cd == null)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (cd.Method != Method)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
int i;
|
||||||
|
|
||||||
|
if (int.TryParse(cd.Value, out i))
|
||||||
|
_lastValue = i;
|
||||||
|
|
||||||
|
_lastData = cd;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public async Task DoAction(UpdateResult ur, MessageResult mr) => await Action(_lastValue.Value, _lastData, ur, mr);
|
||||||
|
|
||||||
|
public static CallbackData GetCallback(string method, long l) => new CallbackData(method, l.ToString());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Int32Action<TForm> : IExternalAction
|
||||||
|
where TForm : FormBase
|
||||||
|
{
|
||||||
|
public string Method { get; set; }
|
||||||
|
|
||||||
|
CallbackData? _lastData { get; set; }
|
||||||
|
|
||||||
|
int? _lastValue { get; set; }
|
||||||
|
|
||||||
|
Func<int, CallbackData, UpdateResult, MessageResult, Task> Action;
|
||||||
|
|
||||||
|
public Int32Action(string method, Func<int, CallbackData, UpdateResult, MessageResult, Task> action)
|
||||||
|
{
|
||||||
|
Method = method;
|
||||||
|
Action = action;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool DoesFit(string raw_data)
|
||||||
|
{
|
||||||
|
var cd = CallbackData.Deserialize(raw_data);
|
||||||
|
|
||||||
|
if (cd == null)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (cd.Method != Method)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
int i;
|
||||||
|
|
||||||
|
if (int.TryParse(cd.Value, out i))
|
||||||
|
_lastValue = i;
|
||||||
|
|
||||||
|
_lastData = cd;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public async Task DoAction(UpdateResult ur, MessageResult mr) => await Action(_lastValue.Value, _lastData, ur, mr);
|
||||||
|
}
|
||||||
|
}
|
||||||
+14
@@ -0,0 +1,14 @@
|
|||||||
|
using TelegramBotBase.Base;
|
||||||
|
using TelegramBotBase.Form;
|
||||||
|
|
||||||
|
namespace TelegramBotBase.Experiments.ActionManager.Actions
|
||||||
|
{
|
||||||
|
public static class Int64Action_Extensions
|
||||||
|
{
|
||||||
|
|
||||||
|
public static void AddInt64Action(this ExternalActionManager manager, string method, Func<long, CallbackData, UpdateResult, MessageResult, Task> action)
|
||||||
|
{
|
||||||
|
manager.Add(new Int64Action(method, action));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+89
@@ -0,0 +1,89 @@
|
|||||||
|
using TelegramBotBase.Base;
|
||||||
|
using TelegramBotBase.Form;
|
||||||
|
|
||||||
|
namespace TelegramBotBase.Experiments.ActionManager.Actions
|
||||||
|
{
|
||||||
|
public class Int64Action : IExternalAction
|
||||||
|
{
|
||||||
|
public string Method { get; set; }
|
||||||
|
|
||||||
|
CallbackData? _lastData { get; set; }
|
||||||
|
|
||||||
|
long? _lastValue { get; set; }
|
||||||
|
|
||||||
|
Func<long, CallbackData, UpdateResult, MessageResult, Task> Action;
|
||||||
|
|
||||||
|
public Int64Action(string method, Func<long, CallbackData, UpdateResult, MessageResult, Task> action)
|
||||||
|
{
|
||||||
|
Method = method;
|
||||||
|
Action = action;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool DoesFit(string raw_data)
|
||||||
|
{
|
||||||
|
var cd = CallbackData.Deserialize(raw_data);
|
||||||
|
|
||||||
|
if (cd == null)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (cd.Method != Method)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
long l;
|
||||||
|
|
||||||
|
if (long.TryParse(cd.Value, out l))
|
||||||
|
_lastValue = l;
|
||||||
|
|
||||||
|
_lastData = cd;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public async Task DoAction(UpdateResult ur, MessageResult mr) => await Action(_lastValue.Value, _lastData, ur, mr);
|
||||||
|
|
||||||
|
public static CallbackData GetCallback(string method, long l) => new CallbackData(method, l.ToString());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Int64Action<TForm> : IExternalAction
|
||||||
|
where TForm : FormBase
|
||||||
|
{
|
||||||
|
public string Method { get; set; }
|
||||||
|
|
||||||
|
CallbackData? _lastData { get; set; }
|
||||||
|
|
||||||
|
long? _lastValue { get; set; }
|
||||||
|
|
||||||
|
Func<long, CallbackData, UpdateResult, MessageResult, Task> Action;
|
||||||
|
|
||||||
|
public Int64Action(string method, Func<long, CallbackData, UpdateResult, MessageResult, Task> action)
|
||||||
|
{
|
||||||
|
Method = method;
|
||||||
|
Action = action;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool DoesFit(string raw_data)
|
||||||
|
{
|
||||||
|
var cd = CallbackData.Deserialize(raw_data);
|
||||||
|
|
||||||
|
if (cd == null)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (cd.Method != Method)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
long g;
|
||||||
|
|
||||||
|
if (long.TryParse(cd.Value, out g))
|
||||||
|
_lastValue = g;
|
||||||
|
|
||||||
|
_lastData = cd;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public async Task DoAction(UpdateResult ur, MessageResult mr) => await Action(_lastValue.Value, _lastData, ur, mr);
|
||||||
|
}
|
||||||
|
}
|
||||||
+13
@@ -0,0 +1,13 @@
|
|||||||
|
using TelegramBotBase.Base;
|
||||||
|
|
||||||
|
namespace TelegramBotBase.Experiments.ActionManager.Actions
|
||||||
|
{
|
||||||
|
public static class StartWithAction_Extensions
|
||||||
|
{
|
||||||
|
public static void AddStartsWithAction(this ExternalActionManager manager, string value, Func<String, UpdateResult, MessageResult, Task> action)
|
||||||
|
{
|
||||||
|
manager.Add(new StartWithAction(value, action));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+29
@@ -0,0 +1,29 @@
|
|||||||
|
using TelegramBotBase.Base;
|
||||||
|
using TelegramBotBase.Form;
|
||||||
|
|
||||||
|
namespace TelegramBotBase.Experiments.ActionManager.Actions
|
||||||
|
{
|
||||||
|
|
||||||
|
public class StartWithAction : IExternalAction
|
||||||
|
{
|
||||||
|
public string SearchForString { get; set; }
|
||||||
|
|
||||||
|
public Action<FormBase, string> SetProperty { get; set; }
|
||||||
|
|
||||||
|
Func<String, UpdateResult, MessageResult, Task> Action;
|
||||||
|
|
||||||
|
|
||||||
|
public StartWithAction(string searchFor, Func<String, UpdateResult, MessageResult, Task> action)
|
||||||
|
{
|
||||||
|
SearchForString = searchFor;
|
||||||
|
Action = action;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool DoesFit(string raw_data) => raw_data.StartsWith(SearchForString);
|
||||||
|
|
||||||
|
|
||||||
|
public async Task DoAction(UpdateResult ur, MessageResult mr) => await Action(mr.RawData, ur, mr);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+14
@@ -0,0 +1,14 @@
|
|||||||
|
using TelegramBotBase.Base;
|
||||||
|
using TelegramBotBase.Form;
|
||||||
|
|
||||||
|
namespace TelegramBotBase.Experiments.ActionManager.Actions
|
||||||
|
{
|
||||||
|
public static class StringAction_Extensions
|
||||||
|
{
|
||||||
|
|
||||||
|
public static void AddStringAction(this ExternalActionManager manager, string method, Func<String, CallbackData, UpdateResult, MessageResult, Task> action)
|
||||||
|
{
|
||||||
|
manager.Add(new StringAction(method, action));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+85
@@ -0,0 +1,85 @@
|
|||||||
|
using TelegramBotBase.Base;
|
||||||
|
using TelegramBotBase.Form;
|
||||||
|
|
||||||
|
namespace TelegramBotBase.Experiments.ActionManager.Actions
|
||||||
|
{
|
||||||
|
public class StringAction : IExternalAction
|
||||||
|
{
|
||||||
|
public string Method { get; set; }
|
||||||
|
|
||||||
|
CallbackData? _lastData { get; set; }
|
||||||
|
|
||||||
|
String? _lastValue { get; set; }
|
||||||
|
|
||||||
|
Func<string, CallbackData, UpdateResult, MessageResult, Task> Action;
|
||||||
|
|
||||||
|
public StringAction(string method, Func<string, CallbackData, UpdateResult, MessageResult, Task> action)
|
||||||
|
{
|
||||||
|
Method = method;
|
||||||
|
Action = action;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public bool DoesFit(string raw_data)
|
||||||
|
{
|
||||||
|
var cd = CallbackData.Deserialize(raw_data);
|
||||||
|
|
||||||
|
if (cd == null)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (cd.Method != Method)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
_lastValue = cd.Value;
|
||||||
|
|
||||||
|
_lastData = cd;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public async Task DoAction(UpdateResult ur, MessageResult mr) => await Action(_lastValue, _lastData, ur, mr);
|
||||||
|
|
||||||
|
public static CallbackData GetCallback(string method, string str) => new CallbackData(method, str);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public class StringAction<TForm> : IExternalAction
|
||||||
|
where TForm : FormBase
|
||||||
|
{
|
||||||
|
public string Method { get; set; }
|
||||||
|
|
||||||
|
CallbackData? _lastData { get; set; }
|
||||||
|
|
||||||
|
String? _lastValue { get; set; }
|
||||||
|
|
||||||
|
Func<String, CallbackData, UpdateResult, MessageResult, Task> Action;
|
||||||
|
|
||||||
|
public StringAction(string method, Func<String, CallbackData, UpdateResult, MessageResult, Task> action)
|
||||||
|
{
|
||||||
|
Method = method;
|
||||||
|
Action = action;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool DoesFit(string raw_data)
|
||||||
|
{
|
||||||
|
var cd = CallbackData.Deserialize(raw_data);
|
||||||
|
|
||||||
|
if (cd == null)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (cd.Method != Method)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
_lastValue = cd.Value;
|
||||||
|
|
||||||
|
_lastData = cd;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public async Task DoAction(UpdateResult ur, MessageResult mr) => await Action(_lastValue, _lastData, ur, mr);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
+46
@@ -0,0 +1,46 @@
|
|||||||
|
using TelegramBotBase.Base;
|
||||||
|
|
||||||
|
namespace TelegramBotBase.Experiments.ActionManager
|
||||||
|
{
|
||||||
|
public partial class ExternalActionManager
|
||||||
|
{
|
||||||
|
|
||||||
|
List<IExternalAction> actions = new List<IExternalAction>();
|
||||||
|
|
||||||
|
public void Add(IExternalAction action)
|
||||||
|
{
|
||||||
|
actions.Add(action);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<bool> ManageCall(UpdateResult ur, MessageResult mr)
|
||||||
|
{
|
||||||
|
|
||||||
|
foreach (var action in actions)
|
||||||
|
{
|
||||||
|
if (!action.DoesFit(mr.RawData))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
await action.DoAction(ur, mr);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates an instance of the ExternalActionManager for configuration.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="action"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static ExternalActionManager Configure(Action<ExternalActionManager> action)
|
||||||
|
{
|
||||||
|
var eam = new ExternalActionManager();
|
||||||
|
|
||||||
|
action(eam);
|
||||||
|
|
||||||
|
return eam;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+13
@@ -0,0 +1,13 @@
|
|||||||
|
using TelegramBotBase.Base;
|
||||||
|
|
||||||
|
namespace TelegramBotBase.Experiments.ActionManager
|
||||||
|
{
|
||||||
|
|
||||||
|
public interface IExternalAction
|
||||||
|
{
|
||||||
|
bool DoesFit(string raw_data);
|
||||||
|
|
||||||
|
Task DoAction(UpdateResult ur, MessageResult mr);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+63
@@ -0,0 +1,63 @@
|
|||||||
|
using System.Linq.Expressions;
|
||||||
|
using TelegramBotBase.Form;
|
||||||
|
|
||||||
|
namespace TelegramBotBase.Experiments.ActionManager.Navigation
|
||||||
|
{
|
||||||
|
public static class EndsWithNavigation_Extensions
|
||||||
|
{
|
||||||
|
public static void AddEndsWithNavigation<TForm>(this ExternalActionManager manager, string method, Expression<Func<TForm, string>> propertySelector)
|
||||||
|
where TForm : FormBase
|
||||||
|
{
|
||||||
|
if (!typeof(FormBase).IsAssignableFrom(typeof(TForm)))
|
||||||
|
{
|
||||||
|
throw new ArgumentException($"{nameof(TForm)} argument must be a {nameof(FormBase)} type");
|
||||||
|
}
|
||||||
|
|
||||||
|
var newValue = Expression.Parameter(propertySelector.Body.Type);
|
||||||
|
|
||||||
|
var assign = Expression.Lambda<Action<TForm, string>>(Expression.Assign(propertySelector.Body, newValue), propertySelector.Parameters[0], newValue);
|
||||||
|
|
||||||
|
var setter = assign.Compile(true);
|
||||||
|
|
||||||
|
manager.Add(new EndsWithNavigation<TForm>(method, setter));
|
||||||
|
}
|
||||||
|
public static void AddEndsWithNavigation<TForm>(this ExternalActionManager manager, string value, Action<TForm, string> setProperty)
|
||||||
|
where TForm : FormBase
|
||||||
|
{
|
||||||
|
if (!typeof(FormBase).IsAssignableFrom(typeof(TForm)))
|
||||||
|
{
|
||||||
|
throw new ArgumentException($"{nameof(TForm)} argument must be a {nameof(FormBase)} type");
|
||||||
|
}
|
||||||
|
|
||||||
|
manager.Add(new EndsWithNavigation<TForm>(value, setProperty));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void AddEndsWithNavigation(this ExternalActionManager manager, Type formType, string value, Expression<Func<FormBase, string>> propertySelector)
|
||||||
|
{
|
||||||
|
if (!typeof(FormBase).IsAssignableFrom(formType))
|
||||||
|
{
|
||||||
|
throw new ArgumentException($"{nameof(formType)} argument must be a {nameof(FormBase)} type");
|
||||||
|
}
|
||||||
|
|
||||||
|
var newValue = Expression.Parameter(propertySelector.Body.Type);
|
||||||
|
|
||||||
|
var assign = Expression.Lambda<Action<FormBase, string>>(Expression.Assign(propertySelector.Body, newValue), propertySelector.Parameters[0], newValue);
|
||||||
|
|
||||||
|
var setter = assign.Compile(true);
|
||||||
|
|
||||||
|
manager.Add(new EndsWithNavigation(formType, value, setter));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void AddEndsWithNavigation(this ExternalActionManager manager, Type formType, string value, Action<FormBase, string> setProperty)
|
||||||
|
{
|
||||||
|
if (!typeof(FormBase).IsAssignableFrom(formType))
|
||||||
|
{
|
||||||
|
throw new ArgumentException($"{nameof(formType)} argument must be a {nameof(FormBase)} type");
|
||||||
|
}
|
||||||
|
|
||||||
|
manager.Add(new EndsWithNavigation(formType, value, setProperty));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+77
@@ -0,0 +1,77 @@
|
|||||||
|
using TelegramBotBase.Base;
|
||||||
|
using TelegramBotBase.Form;
|
||||||
|
|
||||||
|
namespace TelegramBotBase.Experiments.ActionManager.Navigation
|
||||||
|
{
|
||||||
|
public class EndsWithNavigation : IExternalAction
|
||||||
|
{
|
||||||
|
public Type FormType { get; }
|
||||||
|
|
||||||
|
public string Value { get; set; }
|
||||||
|
|
||||||
|
public Action<FormBase, string> SetProperty { get; set; }
|
||||||
|
|
||||||
|
public EndsWithNavigation(Type formType, string value, Action<FormBase, string> setProperty)
|
||||||
|
{
|
||||||
|
FormType = formType;
|
||||||
|
Value = value;
|
||||||
|
SetProperty = setProperty;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public bool DoesFit(string raw_data) => raw_data.EndsWith(Value);
|
||||||
|
|
||||||
|
|
||||||
|
public async Task DoAction(UpdateResult ur, MessageResult mr)
|
||||||
|
{
|
||||||
|
await mr.ConfirmAction();
|
||||||
|
|
||||||
|
var new_form = FormType.GetConstructor(new Type[] { })?.Invoke(new object[] { }) as FormBase;
|
||||||
|
|
||||||
|
if (mr.RawData != null)
|
||||||
|
{
|
||||||
|
SetProperty(new_form, mr.RawData);
|
||||||
|
}
|
||||||
|
|
||||||
|
await ur.Device.ActiveForm.NavigateTo(new_form);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class EndsWithNavigation<TForm> : IExternalAction
|
||||||
|
where TForm : FormBase
|
||||||
|
{
|
||||||
|
public string Value { get; set; }
|
||||||
|
|
||||||
|
public Action<TForm, string> SetProperty { get; set; }
|
||||||
|
|
||||||
|
public EndsWithNavigation(string value, Action<TForm, string> setProperty)
|
||||||
|
{
|
||||||
|
Value = value;
|
||||||
|
SetProperty = setProperty;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public bool DoesFit(string raw_data) => raw_data.EndsWith(Value);
|
||||||
|
|
||||||
|
|
||||||
|
public async Task DoAction(UpdateResult ur, MessageResult mr)
|
||||||
|
{
|
||||||
|
await mr.ConfirmAction();
|
||||||
|
|
||||||
|
var type = typeof(TForm);
|
||||||
|
|
||||||
|
TForm new_form = type.GetConstructor(new Type[] { })?.Invoke(new object[] { }) as TForm;
|
||||||
|
|
||||||
|
if (mr.RawData != null)
|
||||||
|
{
|
||||||
|
SetProperty(new_form, mr.RawData);
|
||||||
|
}
|
||||||
|
|
||||||
|
await ur.Device.ActiveForm.NavigateTo(new_form);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+63
@@ -0,0 +1,63 @@
|
|||||||
|
using System.Linq.Expressions;
|
||||||
|
using TelegramBotBase.Form;
|
||||||
|
|
||||||
|
namespace TelegramBotBase.Experiments.ActionManager.Navigation
|
||||||
|
{
|
||||||
|
public static class GuidNavigation_Extensions
|
||||||
|
{
|
||||||
|
|
||||||
|
public static void AddGuidNavigation<TForm>(this ExternalActionManager manager, string method, Expression<Func<TForm, Guid>> propertySelector)
|
||||||
|
where TForm : FormBase
|
||||||
|
{
|
||||||
|
if (!typeof(FormBase).IsAssignableFrom(typeof(TForm)))
|
||||||
|
{
|
||||||
|
throw new ArgumentException($"{nameof(TForm)} argument must be a {nameof(FormBase)} type");
|
||||||
|
}
|
||||||
|
|
||||||
|
var newValue = Expression.Parameter(propertySelector.Body.Type);
|
||||||
|
|
||||||
|
var assign = Expression.Lambda<Action<TForm, Guid>>(Expression.Assign(propertySelector.Body, newValue), propertySelector.Parameters[0], newValue);
|
||||||
|
|
||||||
|
var setter = assign.Compile(true);
|
||||||
|
|
||||||
|
manager.Add(new GuidNavigation<TForm>(method, setter));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void AddGuidNavigation<TForm>(this ExternalActionManager manager, string method, Action<TForm, Guid> action)
|
||||||
|
where TForm : FormBase
|
||||||
|
{
|
||||||
|
if (!typeof(FormBase).IsAssignableFrom(typeof(TForm)))
|
||||||
|
{
|
||||||
|
throw new ArgumentException($"{nameof(TForm)} argument must be a {nameof(FormBase)} type");
|
||||||
|
}
|
||||||
|
|
||||||
|
manager.Add(new GuidNavigation<TForm>(method, action));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void AddGuidNavigation(this ExternalActionManager manager, Type formType, string value, Expression<Func<FormBase, Guid>> propertySelector)
|
||||||
|
{
|
||||||
|
if (!typeof(FormBase).IsAssignableFrom(formType))
|
||||||
|
{
|
||||||
|
throw new ArgumentException($"{nameof(formType)} argument must be a {nameof(FormBase)} type");
|
||||||
|
}
|
||||||
|
|
||||||
|
var newValue = Expression.Parameter(propertySelector.Body.Type);
|
||||||
|
|
||||||
|
var assign = Expression.Lambda<Action<FormBase, Guid>>(Expression.Assign(propertySelector.Body, newValue), propertySelector.Parameters[0], newValue);
|
||||||
|
|
||||||
|
var setter = assign.Compile(true);
|
||||||
|
|
||||||
|
manager.Add(new GuidNavigation(formType, value, setter));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void AddGuidNavigation(this ExternalActionManager manager, Type formType, string method, Action<FormBase, Guid> action)
|
||||||
|
{
|
||||||
|
if (!typeof(FormBase).IsAssignableFrom(formType))
|
||||||
|
{
|
||||||
|
throw new ArgumentException($"{nameof(formType)} argument must be a {nameof(FormBase)} type");
|
||||||
|
}
|
||||||
|
|
||||||
|
manager.Add(new GuidNavigation(formType, method, action));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+106
@@ -0,0 +1,106 @@
|
|||||||
|
using TelegramBotBase.Base;
|
||||||
|
using TelegramBotBase.Form;
|
||||||
|
|
||||||
|
namespace TelegramBotBase.Experiments.ActionManager.Navigation
|
||||||
|
{
|
||||||
|
public class GuidNavigation : IExternalAction
|
||||||
|
{
|
||||||
|
public Type FormType { get; }
|
||||||
|
|
||||||
|
public string Method { get; set; }
|
||||||
|
|
||||||
|
public Action<FormBase, Guid> SetProperty { get; set; }
|
||||||
|
|
||||||
|
Guid? _lastValue { get; set; }
|
||||||
|
|
||||||
|
public GuidNavigation(Type formType, string method, Action<FormBase, Guid> setProperty)
|
||||||
|
{
|
||||||
|
FormType = formType;
|
||||||
|
Method = method;
|
||||||
|
SetProperty = setProperty;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool DoesFit(string raw_action)
|
||||||
|
{
|
||||||
|
var cd = CallbackData.Deserialize(raw_action);
|
||||||
|
|
||||||
|
if (cd == null)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (cd.Method != Method)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
Guid g;
|
||||||
|
|
||||||
|
if (Guid.TryParse(cd.Value, out g))
|
||||||
|
_lastValue = g;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public async Task DoAction(UpdateResult ur, MessageResult mr)
|
||||||
|
{
|
||||||
|
await mr.ConfirmAction();
|
||||||
|
|
||||||
|
var new_form = FormType.GetConstructor(new Type[] { })?.Invoke(new object[] { }) as FormBase;
|
||||||
|
|
||||||
|
if (_lastValue != null)
|
||||||
|
SetProperty(new_form, _lastValue.Value);
|
||||||
|
|
||||||
|
await ur.Device.ActiveForm.NavigateTo(new_form);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static CallbackData GetCallback(string method, Guid guid) => new CallbackData(method, guid.ToString());
|
||||||
|
}
|
||||||
|
|
||||||
|
public class GuidNavigation<TForm> : IExternalAction
|
||||||
|
where TForm : FormBase
|
||||||
|
{
|
||||||
|
public string Method { get; set; }
|
||||||
|
|
||||||
|
public Action<TForm, Guid> SetProperty { get; set; }
|
||||||
|
|
||||||
|
Guid? _lastValue { get; set; }
|
||||||
|
|
||||||
|
public GuidNavigation(string method, Action<TForm, Guid> setProperty)
|
||||||
|
{
|
||||||
|
Method = method;
|
||||||
|
SetProperty = setProperty;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool DoesFit(string raw_data)
|
||||||
|
{
|
||||||
|
var cd = CallbackData.Deserialize(raw_data);
|
||||||
|
|
||||||
|
if (cd == null)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (cd.Method != Method)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
Guid g;
|
||||||
|
|
||||||
|
if (Guid.TryParse(cd.Value, out g))
|
||||||
|
_lastValue = g;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public async Task DoAction(UpdateResult ur, MessageResult mr)
|
||||||
|
{
|
||||||
|
await mr.ConfirmAction();
|
||||||
|
|
||||||
|
var type = typeof(TForm);
|
||||||
|
|
||||||
|
TForm new_form = type.GetConstructor(new Type[] { })?.Invoke(new object[] { }) as TForm;
|
||||||
|
|
||||||
|
if (_lastValue != null)
|
||||||
|
SetProperty(new_form, _lastValue.Value);
|
||||||
|
|
||||||
|
await ur.Device.ActiveForm.NavigateTo(new_form);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+63
@@ -0,0 +1,63 @@
|
|||||||
|
using System.Linq.Expressions;
|
||||||
|
using TelegramBotBase.Form;
|
||||||
|
|
||||||
|
namespace TelegramBotBase.Experiments.ActionManager.Navigation
|
||||||
|
{
|
||||||
|
public static class Int32Navigation_Extensions
|
||||||
|
{
|
||||||
|
|
||||||
|
public static void AddInt32Navigation<TForm>(this ExternalActionManager manager, string method, Expression<Func<TForm, int>> propertySelector)
|
||||||
|
where TForm : FormBase
|
||||||
|
{
|
||||||
|
if (!typeof(FormBase).IsAssignableFrom(typeof(TForm)))
|
||||||
|
{
|
||||||
|
throw new ArgumentException($"{nameof(TForm)} argument must be a {nameof(FormBase)} type");
|
||||||
|
}
|
||||||
|
|
||||||
|
var newValue = Expression.Parameter(propertySelector.Body.Type);
|
||||||
|
|
||||||
|
var assign = Expression.Lambda<Action<TForm, int>>(Expression.Assign(propertySelector.Body, newValue), propertySelector.Parameters[0], newValue);
|
||||||
|
|
||||||
|
var setter = assign.Compile(true);
|
||||||
|
|
||||||
|
manager.Add(new Int32Navigation<TForm>(method, setter));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void AddInt32Navigation<TForm>(this ExternalActionManager manager, string method, Action<TForm, int> action)
|
||||||
|
where TForm : FormBase
|
||||||
|
{
|
||||||
|
if (!typeof(FormBase).IsAssignableFrom(typeof(TForm)))
|
||||||
|
{
|
||||||
|
throw new ArgumentException($"{nameof(TForm)} argument must be a {nameof(FormBase)} type");
|
||||||
|
}
|
||||||
|
|
||||||
|
manager.Add(new Int32Navigation<TForm>(method, action));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void AddInt32Navigation(this ExternalActionManager manager, Type formType, string value, Expression<Func<FormBase, int>> propertySelector)
|
||||||
|
{
|
||||||
|
if (!typeof(FormBase).IsAssignableFrom(formType))
|
||||||
|
{
|
||||||
|
throw new ArgumentException($"{nameof(formType)} argument must be a {nameof(FormBase)} type");
|
||||||
|
}
|
||||||
|
|
||||||
|
var newValue = Expression.Parameter(propertySelector.Body.Type);
|
||||||
|
|
||||||
|
var assign = Expression.Lambda<Action<FormBase, int>>(Expression.Assign(propertySelector.Body, newValue), propertySelector.Parameters[0], newValue);
|
||||||
|
|
||||||
|
var setter = assign.Compile(true);
|
||||||
|
|
||||||
|
manager.Add(new Int32Navigation(formType, value, setter));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void AddInt32Navigation(this ExternalActionManager manager, Type formType, string method, Action<FormBase, int> action)
|
||||||
|
{
|
||||||
|
if (!typeof(FormBase).IsAssignableFrom(formType))
|
||||||
|
{
|
||||||
|
throw new ArgumentException($"{nameof(formType)} argument must be a {nameof(FormBase)} type");
|
||||||
|
}
|
||||||
|
|
||||||
|
manager.Add(new Int32Navigation(formType, method, action));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+106
@@ -0,0 +1,106 @@
|
|||||||
|
using TelegramBotBase.Base;
|
||||||
|
using TelegramBotBase.Form;
|
||||||
|
|
||||||
|
namespace TelegramBotBase.Experiments.ActionManager.Navigation
|
||||||
|
{
|
||||||
|
public class Int32Navigation : IExternalAction
|
||||||
|
{
|
||||||
|
public Type FormType { get; }
|
||||||
|
|
||||||
|
public string Method { get; set; }
|
||||||
|
|
||||||
|
public Action<FormBase, int> SetProperty { get; set; }
|
||||||
|
|
||||||
|
int? _lastValue { get; set; }
|
||||||
|
|
||||||
|
public Int32Navigation(Type formType, string method, Action<FormBase, int> setProperty)
|
||||||
|
{
|
||||||
|
FormType = formType;
|
||||||
|
Method = method;
|
||||||
|
SetProperty = setProperty;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool DoesFit(string raw_action)
|
||||||
|
{
|
||||||
|
var cd = CallbackData.Deserialize(raw_action);
|
||||||
|
|
||||||
|
if (cd == null)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (cd.Method != Method)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
int i;
|
||||||
|
|
||||||
|
if (int.TryParse(cd.Value, out i))
|
||||||
|
_lastValue = i;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public async Task DoAction(UpdateResult ur, MessageResult mr)
|
||||||
|
{
|
||||||
|
await mr.ConfirmAction();
|
||||||
|
|
||||||
|
var new_form = FormType.GetConstructor(new Type[] { })?.Invoke(new object[] { }) as FormBase;
|
||||||
|
|
||||||
|
if (_lastValue != null)
|
||||||
|
SetProperty(new_form, _lastValue.Value);
|
||||||
|
|
||||||
|
await ur.Device.ActiveForm.NavigateTo(new_form);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static CallbackData GetCallback(string method, int i) => new CallbackData(method, i.ToString());
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Int32Navigation<TForm> : IExternalAction
|
||||||
|
where TForm : FormBase
|
||||||
|
{
|
||||||
|
public string Method { get; set; }
|
||||||
|
|
||||||
|
public Action<TForm, int> SetProperty { get; set; }
|
||||||
|
|
||||||
|
int? _lastValue { get; set; }
|
||||||
|
|
||||||
|
public Int32Navigation(string method, Action<TForm, int> setProperty)
|
||||||
|
{
|
||||||
|
Method = method;
|
||||||
|
SetProperty = setProperty;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool DoesFit(string raw_data)
|
||||||
|
{
|
||||||
|
var cd = CallbackData.Deserialize(raw_data);
|
||||||
|
|
||||||
|
if (cd == null)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (cd.Method != Method)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
int g;
|
||||||
|
|
||||||
|
if (int.TryParse(cd.Value, out g))
|
||||||
|
_lastValue = g;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public async Task DoAction(UpdateResult ur, MessageResult mr)
|
||||||
|
{
|
||||||
|
await mr.ConfirmAction();
|
||||||
|
|
||||||
|
var type = typeof(TForm);
|
||||||
|
|
||||||
|
TForm new_form = type.GetConstructor(new Type[] { })?.Invoke(new object[] { }) as TForm;
|
||||||
|
|
||||||
|
if (_lastValue != null)
|
||||||
|
SetProperty(new_form, _lastValue.Value);
|
||||||
|
|
||||||
|
await ur.Device.ActiveForm.NavigateTo(new_form);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+63
@@ -0,0 +1,63 @@
|
|||||||
|
using System.Linq.Expressions;
|
||||||
|
using TelegramBotBase.Form;
|
||||||
|
|
||||||
|
namespace TelegramBotBase.Experiments.ActionManager.Navigation
|
||||||
|
{
|
||||||
|
public static class Int64Navigation_Extensions
|
||||||
|
{
|
||||||
|
|
||||||
|
public static void AddInt64Navigation<TForm>(this ExternalActionManager manager, string method, Expression<Func<TForm, long>> propertySelector)
|
||||||
|
where TForm : FormBase
|
||||||
|
{
|
||||||
|
if (!typeof(FormBase).IsAssignableFrom(typeof(TForm)))
|
||||||
|
{
|
||||||
|
throw new ArgumentException($"{nameof(TForm)} argument must be a {nameof(FormBase)} type");
|
||||||
|
}
|
||||||
|
|
||||||
|
var newValue = Expression.Parameter(propertySelector.Body.Type);
|
||||||
|
|
||||||
|
var assign = Expression.Lambda<Action<TForm, long>>(Expression.Assign(propertySelector.Body, newValue), propertySelector.Parameters[0], newValue);
|
||||||
|
|
||||||
|
var setter = assign.Compile(true);
|
||||||
|
|
||||||
|
manager.Add(new Int64Navigation<TForm>(method, setter));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void AddInt64Navigation<TForm>(this ExternalActionManager manager, string method, Action<TForm, long> action)
|
||||||
|
where TForm : FormBase
|
||||||
|
{
|
||||||
|
if (!typeof(FormBase).IsAssignableFrom(typeof(TForm)))
|
||||||
|
{
|
||||||
|
throw new ArgumentException($"{nameof(TForm)} argument must be a {nameof(FormBase)} type");
|
||||||
|
}
|
||||||
|
|
||||||
|
manager.Add(new Int64Navigation<TForm>(method, action));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void AddInt64Navigation(this ExternalActionManager manager, Type formType, string value, Expression<Func<FormBase, long>> propertySelector)
|
||||||
|
{
|
||||||
|
if (!typeof(FormBase).IsAssignableFrom(formType))
|
||||||
|
{
|
||||||
|
throw new ArgumentException($"{nameof(formType)} argument must be a {nameof(FormBase)} type");
|
||||||
|
}
|
||||||
|
|
||||||
|
var newValue = Expression.Parameter(propertySelector.Body.Type);
|
||||||
|
|
||||||
|
var assign = Expression.Lambda<Action<FormBase, long>>(Expression.Assign(propertySelector.Body, newValue), propertySelector.Parameters[0], newValue);
|
||||||
|
|
||||||
|
var setter = assign.Compile(true);
|
||||||
|
|
||||||
|
manager.Add(new Int64Navigation(formType, value, setter));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void AddInt64Navigation(this ExternalActionManager manager, Type formType, string method, Action<FormBase, long> action)
|
||||||
|
{
|
||||||
|
if (!typeof(FormBase).IsAssignableFrom(formType))
|
||||||
|
{
|
||||||
|
throw new ArgumentException($"{nameof(formType)} argument must be a {nameof(FormBase)} type");
|
||||||
|
}
|
||||||
|
|
||||||
|
manager.Add(new Int64Navigation(formType, method, action));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+106
@@ -0,0 +1,106 @@
|
|||||||
|
using TelegramBotBase.Base;
|
||||||
|
using TelegramBotBase.Form;
|
||||||
|
|
||||||
|
namespace TelegramBotBase.Experiments.ActionManager.Navigation
|
||||||
|
{
|
||||||
|
public class Int64Navigation : IExternalAction
|
||||||
|
{
|
||||||
|
public Type FormType { get; }
|
||||||
|
|
||||||
|
public string Method { get; set; }
|
||||||
|
|
||||||
|
public Action<FormBase, long> SetProperty { get; set; }
|
||||||
|
|
||||||
|
long? _lastValue { get; set; }
|
||||||
|
|
||||||
|
public Int64Navigation(Type formType, string method, Action<FormBase, long> setProperty)
|
||||||
|
{
|
||||||
|
FormType = formType;
|
||||||
|
Method = method;
|
||||||
|
SetProperty = setProperty;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool DoesFit(string raw_action)
|
||||||
|
{
|
||||||
|
var cd = CallbackData.Deserialize(raw_action);
|
||||||
|
|
||||||
|
if (cd == null)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (cd.Method != Method)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
long l;
|
||||||
|
|
||||||
|
if (long.TryParse(cd.Value, out l))
|
||||||
|
_lastValue = l;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public async Task DoAction(UpdateResult ur, MessageResult mr)
|
||||||
|
{
|
||||||
|
await mr.ConfirmAction();
|
||||||
|
|
||||||
|
var new_form = FormType.GetConstructor(new Type[] { })?.Invoke(new object[] { }) as FormBase;
|
||||||
|
|
||||||
|
if (_lastValue != null)
|
||||||
|
SetProperty(new_form, _lastValue.Value);
|
||||||
|
|
||||||
|
await ur.Device.ActiveForm.NavigateTo(new_form);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static CallbackData GetCallback(string method, long l) => new CallbackData(method, l.ToString());
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Int64Navigation<TForm> : IExternalAction
|
||||||
|
where TForm : FormBase
|
||||||
|
{
|
||||||
|
public string Method { get; set; }
|
||||||
|
|
||||||
|
public Action<TForm, long> SetProperty { get; set; }
|
||||||
|
|
||||||
|
long? _lastValue { get; set; }
|
||||||
|
|
||||||
|
public Int64Navigation(string method, Action<TForm, long> setProperty)
|
||||||
|
{
|
||||||
|
Method = method;
|
||||||
|
SetProperty = setProperty;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool DoesFit(string raw_data)
|
||||||
|
{
|
||||||
|
var cd = CallbackData.Deserialize(raw_data);
|
||||||
|
|
||||||
|
if (cd == null)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (cd.Method != Method)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
long l;
|
||||||
|
|
||||||
|
if (long.TryParse(cd.Value, out l))
|
||||||
|
_lastValue = l;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public async Task DoAction(UpdateResult ur, MessageResult mr)
|
||||||
|
{
|
||||||
|
await mr.ConfirmAction();
|
||||||
|
|
||||||
|
var type = typeof(TForm);
|
||||||
|
|
||||||
|
TForm new_form = type.GetConstructor(new Type[] { })?.Invoke(new object[] { }) as TForm;
|
||||||
|
|
||||||
|
if (_lastValue != null)
|
||||||
|
SetProperty(new_form, _lastValue.Value);
|
||||||
|
|
||||||
|
await ur.Device.ActiveForm.NavigateTo(new_form);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+63
@@ -0,0 +1,63 @@
|
|||||||
|
using System.Linq.Expressions;
|
||||||
|
using TelegramBotBase.Form;
|
||||||
|
|
||||||
|
namespace TelegramBotBase.Experiments.ActionManager.Navigation
|
||||||
|
{
|
||||||
|
public static class StartWithNavigation_Extensions
|
||||||
|
{
|
||||||
|
public static void AddStartsWithNavigation<TForm>(this ExternalActionManager manager, string method, Expression<Func<TForm, string>> propertySelector)
|
||||||
|
where TForm : FormBase
|
||||||
|
{
|
||||||
|
if (!typeof(FormBase).IsAssignableFrom(typeof(TForm)))
|
||||||
|
{
|
||||||
|
throw new ArgumentException($"{nameof(TForm)} argument must be a {nameof(FormBase)} type");
|
||||||
|
}
|
||||||
|
|
||||||
|
var newValue = Expression.Parameter(propertySelector.Body.Type);
|
||||||
|
|
||||||
|
var assign = Expression.Lambda<Action<TForm, string>>(Expression.Assign(propertySelector.Body, newValue), propertySelector.Parameters[0], newValue);
|
||||||
|
|
||||||
|
var setter = assign.Compile(true);
|
||||||
|
|
||||||
|
manager.Add(new StartWithNavigation<TForm>(method, setter));
|
||||||
|
}
|
||||||
|
public static void AddStartsWithNavigation<TForm>(this ExternalActionManager manager, string value, Action<TForm, string> setProperty)
|
||||||
|
where TForm : FormBase
|
||||||
|
{
|
||||||
|
if (!typeof(FormBase).IsAssignableFrom(typeof(TForm)))
|
||||||
|
{
|
||||||
|
throw new ArgumentException($"{nameof(TForm)} argument must be a {nameof(FormBase)} type");
|
||||||
|
}
|
||||||
|
|
||||||
|
manager.Add(new StartWithNavigation<TForm>(value, setProperty));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void AddStartsWithNavigation(this ExternalActionManager manager, Type formType, string value, Expression<Func<FormBase, string>> propertySelector)
|
||||||
|
{
|
||||||
|
if (!typeof(FormBase).IsAssignableFrom(formType))
|
||||||
|
{
|
||||||
|
throw new ArgumentException($"{nameof(formType)} argument must be a {nameof(FormBase)} type");
|
||||||
|
}
|
||||||
|
|
||||||
|
var newValue = Expression.Parameter(propertySelector.Body.Type);
|
||||||
|
|
||||||
|
var assign = Expression.Lambda<Action<FormBase, string>>(Expression.Assign(propertySelector.Body, newValue), propertySelector.Parameters[0], newValue);
|
||||||
|
|
||||||
|
var setter = assign.Compile(true);
|
||||||
|
|
||||||
|
manager.Add(new StartWithNavigation(formType, value, setter));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void AddStartsWithNavigation(this ExternalActionManager manager, Type formType, string value, Action<FormBase, string> setProperty)
|
||||||
|
{
|
||||||
|
if (!typeof(FormBase).IsAssignableFrom(formType))
|
||||||
|
{
|
||||||
|
throw new ArgumentException($"{nameof(formType)} argument must be a {nameof(FormBase)} type");
|
||||||
|
}
|
||||||
|
|
||||||
|
manager.Add(new StartWithNavigation(formType, value, setProperty));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+74
@@ -0,0 +1,74 @@
|
|||||||
|
using TelegramBotBase.Base;
|
||||||
|
using TelegramBotBase.Form;
|
||||||
|
|
||||||
|
namespace TelegramBotBase.Experiments.ActionManager.Navigation
|
||||||
|
{
|
||||||
|
public class StartWithNavigation : IExternalAction
|
||||||
|
{
|
||||||
|
public Type FormType { get; }
|
||||||
|
|
||||||
|
public string Value { get; set; }
|
||||||
|
|
||||||
|
public Action<FormBase, string> SetProperty { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
public StartWithNavigation(Type formType, string value, Action<FormBase, string> setProperty)
|
||||||
|
{
|
||||||
|
FormType = formType;
|
||||||
|
Value = value;
|
||||||
|
SetProperty = setProperty;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool DoesFit(string raw_data) => raw_data.StartsWith(Value);
|
||||||
|
|
||||||
|
public async Task DoAction(UpdateResult ur, MessageResult mr)
|
||||||
|
{
|
||||||
|
await mr.ConfirmAction();
|
||||||
|
|
||||||
|
var new_form = FormType.GetConstructor(new Type[] { })?.Invoke(new object[] { }) as FormBase;
|
||||||
|
|
||||||
|
if (mr.RawData != null)
|
||||||
|
{
|
||||||
|
SetProperty(new_form, mr.RawData);
|
||||||
|
}
|
||||||
|
|
||||||
|
await ur.Device.ActiveForm.NavigateTo(new_form);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class StartWithNavigation<TForm> : IExternalAction
|
||||||
|
where TForm : FormBase
|
||||||
|
{
|
||||||
|
public string Value { get; set; }
|
||||||
|
|
||||||
|
public Action<TForm, string> SetProperty { get; set; }
|
||||||
|
|
||||||
|
public StartWithNavigation(string value, Action<TForm, string> setProperty)
|
||||||
|
{
|
||||||
|
Value = value;
|
||||||
|
SetProperty = setProperty;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public bool DoesFit(string raw_data) => raw_data.StartsWith(Value);
|
||||||
|
|
||||||
|
|
||||||
|
public async Task DoAction(UpdateResult ur, MessageResult mr)
|
||||||
|
{
|
||||||
|
await mr.ConfirmAction();
|
||||||
|
|
||||||
|
var type = typeof(TForm);
|
||||||
|
|
||||||
|
TForm new_form = type.GetConstructor(new Type[] { })?.Invoke(new object[] { }) as TForm;
|
||||||
|
|
||||||
|
if (mr.RawData != null)
|
||||||
|
{
|
||||||
|
SetProperty(new_form, mr.RawData);
|
||||||
|
}
|
||||||
|
|
||||||
|
await ur.Device.ActiveForm.NavigateTo(new_form);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+63
@@ -0,0 +1,63 @@
|
|||||||
|
using System.Linq.Expressions;
|
||||||
|
using TelegramBotBase.Form;
|
||||||
|
|
||||||
|
namespace TelegramBotBase.Experiments.ActionManager.Navigation
|
||||||
|
{
|
||||||
|
public static class StringNavigation_Extensions
|
||||||
|
{
|
||||||
|
|
||||||
|
public static void AddStringNavigation<TForm>(this ExternalActionManager manager, string method, Expression<Func<TForm, String>> propertySelector)
|
||||||
|
where TForm : FormBase
|
||||||
|
{
|
||||||
|
if (!typeof(FormBase).IsAssignableFrom(typeof(TForm)))
|
||||||
|
{
|
||||||
|
throw new ArgumentException($"{nameof(TForm)} argument must be a {nameof(FormBase)} type");
|
||||||
|
}
|
||||||
|
|
||||||
|
var newValue = Expression.Parameter(propertySelector.Body.Type);
|
||||||
|
|
||||||
|
var assign = Expression.Lambda<Action<TForm, String>>(Expression.Assign(propertySelector.Body, newValue), propertySelector.Parameters[0], newValue);
|
||||||
|
|
||||||
|
var setter = assign.Compile(true);
|
||||||
|
|
||||||
|
manager.Add(new StringNavigation<TForm>(method, setter));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void AddStringNavigation<TForm>(this ExternalActionManager manager, string method, Action<TForm, String> action)
|
||||||
|
where TForm : FormBase
|
||||||
|
{
|
||||||
|
if (!typeof(FormBase).IsAssignableFrom(typeof(TForm)))
|
||||||
|
{
|
||||||
|
throw new ArgumentException($"{nameof(TForm)} argument must be a {nameof(FormBase)} type");
|
||||||
|
}
|
||||||
|
|
||||||
|
manager.Add(new StringNavigation<TForm>(method, action));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void AddStringNavigation(this ExternalActionManager manager, Type formType, string value, Expression<Func<FormBase, String>> propertySelector)
|
||||||
|
{
|
||||||
|
if (!typeof(FormBase).IsAssignableFrom(formType))
|
||||||
|
{
|
||||||
|
throw new ArgumentException($"{nameof(formType)} argument must be a {nameof(FormBase)} type");
|
||||||
|
}
|
||||||
|
|
||||||
|
var newValue = Expression.Parameter(propertySelector.Body.Type);
|
||||||
|
|
||||||
|
var assign = Expression.Lambda<Action<FormBase, String>>(Expression.Assign(propertySelector.Body, newValue), propertySelector.Parameters[0], newValue);
|
||||||
|
|
||||||
|
var setter = assign.Compile(true);
|
||||||
|
|
||||||
|
manager.Add(new StringNavigation(formType, value, setter));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void AddStringNavigation(this ExternalActionManager manager, Type formType, string method, Action<FormBase, String> action)
|
||||||
|
{
|
||||||
|
if (!typeof(FormBase).IsAssignableFrom(formType))
|
||||||
|
{
|
||||||
|
throw new ArgumentException($"{nameof(formType)} argument must be a {nameof(FormBase)} type");
|
||||||
|
}
|
||||||
|
|
||||||
|
manager.Add(new StringNavigation(formType, method, action));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+100
@@ -0,0 +1,100 @@
|
|||||||
|
using TelegramBotBase.Base;
|
||||||
|
using TelegramBotBase.Form;
|
||||||
|
|
||||||
|
namespace TelegramBotBase.Experiments.ActionManager.Navigation
|
||||||
|
{
|
||||||
|
public class StringNavigation : IExternalAction
|
||||||
|
{
|
||||||
|
public Type FormType { get; }
|
||||||
|
|
||||||
|
public string Method { get; set; }
|
||||||
|
|
||||||
|
public Action<FormBase, String> SetProperty { get; set; }
|
||||||
|
|
||||||
|
String? _lastValue { get; set; }
|
||||||
|
|
||||||
|
public StringNavigation(Type formType, string method, Action<FormBase, String> setProperty)
|
||||||
|
{
|
||||||
|
FormType = formType;
|
||||||
|
Method = method;
|
||||||
|
SetProperty = setProperty;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool DoesFit(string raw_action)
|
||||||
|
{
|
||||||
|
var cd = CallbackData.Deserialize(raw_action);
|
||||||
|
|
||||||
|
if (cd == null)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (cd.Method != Method)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
_lastValue = cd.Value;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public async Task DoAction(UpdateResult ur, MessageResult mr)
|
||||||
|
{
|
||||||
|
await mr.ConfirmAction();
|
||||||
|
|
||||||
|
var new_form = FormType.GetConstructor(new Type[] { })?.Invoke(new object[] { }) as FormBase;
|
||||||
|
|
||||||
|
if (_lastValue != null)
|
||||||
|
SetProperty(new_form, _lastValue);
|
||||||
|
|
||||||
|
await ur.Device.ActiveForm.NavigateTo(new_form);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static CallbackData GetCallback(string method, String str) => new CallbackData(method, str);
|
||||||
|
}
|
||||||
|
|
||||||
|
public class StringNavigation<TForm> : IExternalAction
|
||||||
|
where TForm : FormBase
|
||||||
|
{
|
||||||
|
public string Method { get; set; }
|
||||||
|
|
||||||
|
public Action<TForm, String> SetProperty { get; set; }
|
||||||
|
|
||||||
|
String? _lastValue { get; set; }
|
||||||
|
|
||||||
|
public StringNavigation(string method, Action<TForm, String> setProperty)
|
||||||
|
{
|
||||||
|
Method = method;
|
||||||
|
SetProperty = setProperty;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool DoesFit(string raw_data)
|
||||||
|
{
|
||||||
|
var cd = CallbackData.Deserialize(raw_data);
|
||||||
|
|
||||||
|
if (cd == null)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (cd.Method != Method)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
_lastValue = cd.Value;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public async Task DoAction(UpdateResult ur, MessageResult mr)
|
||||||
|
{
|
||||||
|
await mr.ConfirmAction();
|
||||||
|
|
||||||
|
var type = typeof(TForm);
|
||||||
|
|
||||||
|
TForm new_form = type.GetConstructor(new Type[] { })?.Invoke(new object[] { }) as TForm;
|
||||||
|
|
||||||
|
if (_lastValue != null)
|
||||||
|
SetProperty(new_form, _lastValue);
|
||||||
|
|
||||||
|
await ur.Device.ActiveForm.NavigateTo(new_form);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+146
@@ -0,0 +1,146 @@
|
|||||||
|
using System.ComponentModel;
|
||||||
|
using Telegram.Bot.Types.Enums;
|
||||||
|
using TelegramBotBase;
|
||||||
|
using TelegramBotBase.Args;
|
||||||
|
using TelegramBotBase.Base;
|
||||||
|
using TelegramBotBase.Interfaces;
|
||||||
|
using TelegramBotBase.Sessions;
|
||||||
|
|
||||||
|
namespace TelegramBotBase.Experiments.ActionManager
|
||||||
|
{
|
||||||
|
public class CustomFormBaseMessageLoop : IMessageLoopFactory
|
||||||
|
{
|
||||||
|
private static readonly object EvUnhandledCall = new();
|
||||||
|
|
||||||
|
private readonly EventHandlerList _events = new();
|
||||||
|
|
||||||
|
public ExternalActionManager ExternalActionManager { get; set; }
|
||||||
|
|
||||||
|
public async Task MessageLoop(BotBase bot, DeviceSession s, UpdateResult ur, MessageResult mr)
|
||||||
|
{
|
||||||
|
var update = ur.RawData;
|
||||||
|
|
||||||
|
if (update.Type != UpdateType.Message
|
||||||
|
&& update.Type != UpdateType.EditedMessage
|
||||||
|
&& update.Type != UpdateType.CallbackQuery)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Remove unecessary parameter from method call in the future
|
||||||
|
var session = ur.Device;
|
||||||
|
|
||||||
|
//Is this a bot command ?
|
||||||
|
if (mr.IsFirstHandler && mr.IsBotCommand && bot.IsKnownBotCommand(mr.BotCommand))
|
||||||
|
{
|
||||||
|
var sce = new BotCommandEventArgs(mr.BotCommand, mr.BotCommandParameters, mr.Message, session.DeviceId,
|
||||||
|
session);
|
||||||
|
await bot.OnBotCommand(sce);
|
||||||
|
|
||||||
|
if (sce.Handled)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mr.Device = session;
|
||||||
|
ur.Device = session;
|
||||||
|
|
||||||
|
var activeForm = session.ActiveForm;
|
||||||
|
|
||||||
|
//Pre Loading Event
|
||||||
|
await activeForm.PreLoad(mr);
|
||||||
|
|
||||||
|
//Send Load event to controls
|
||||||
|
await activeForm.LoadControls(mr);
|
||||||
|
|
||||||
|
//Loading Event
|
||||||
|
await activeForm.Load(mr);
|
||||||
|
|
||||||
|
|
||||||
|
//Is Attachment ? (Photo, Audio, Video, Contact, Location, Document) (Ignore Callback Queries)
|
||||||
|
if (update.Type == UpdateType.Message)
|
||||||
|
{
|
||||||
|
if ((mr.MessageType == MessageType.Contact)
|
||||||
|
| (mr.MessageType == MessageType.Document)
|
||||||
|
| (mr.MessageType == MessageType.Location)
|
||||||
|
| (mr.MessageType == MessageType.Photo)
|
||||||
|
| (mr.MessageType == MessageType.Video)
|
||||||
|
| (mr.MessageType == MessageType.Audio))
|
||||||
|
{
|
||||||
|
await activeForm.SentData(new DataResult(ur));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Message edited ?
|
||||||
|
if (update.Type == UpdateType.EditedMessage)
|
||||||
|
{
|
||||||
|
await activeForm.Edited(mr);
|
||||||
|
}
|
||||||
|
|
||||||
|
//Action Event
|
||||||
|
if (!session.FormSwitched && mr.IsAction)
|
||||||
|
{
|
||||||
|
//Send Action event to controls
|
||||||
|
await activeForm.ActionControls(mr);
|
||||||
|
|
||||||
|
//Send Action event to form itself
|
||||||
|
await activeForm.Action(mr);
|
||||||
|
|
||||||
|
if (!mr.Handled)
|
||||||
|
{
|
||||||
|
var handled = await ExternalActionManager?.ManageCall(ur, mr);
|
||||||
|
|
||||||
|
if (handled)
|
||||||
|
{
|
||||||
|
mr.Handled = true;
|
||||||
|
if (!session.FormSwitched)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var uhc = new UnhandledCallEventArgs(ur.Message.Text, mr.RawData, session.DeviceId, mr.MessageId, ur.Message, session);
|
||||||
|
|
||||||
|
OnUnhandledCall(uhc);
|
||||||
|
|
||||||
|
if (uhc.Handled)
|
||||||
|
{
|
||||||
|
mr.Handled = true;
|
||||||
|
if (!session.FormSwitched)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!session.FormSwitched)
|
||||||
|
{
|
||||||
|
//Render Event
|
||||||
|
await activeForm.RenderControls(mr);
|
||||||
|
|
||||||
|
await activeForm.Render(mr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Will be called if no form handled this call
|
||||||
|
/// </summary>
|
||||||
|
public event EventHandler<UnhandledCallEventArgs> UnhandledCall
|
||||||
|
{
|
||||||
|
add => _events.AddHandler(EvUnhandledCall, value);
|
||||||
|
remove => _events.RemoveHandler(EvUnhandledCall, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnUnhandledCall(UnhandledCallEventArgs e)
|
||||||
|
{
|
||||||
|
(_events[EvUnhandledCall] as EventHandler<UnhandledCallEventArgs>)?.Invoke(this, e);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using TelegramBotBase.Builder.Interfaces;
|
||||||
|
using TelegramBotBase.Interfaces;
|
||||||
|
|
||||||
|
namespace TelegramBotBase.Experiments.ActionManager
|
||||||
|
{
|
||||||
|
public static class StaticExtensions
|
||||||
|
{
|
||||||
|
public static IStartFormSelectionStage ActionMessageLoop(this IMessageLoopSelectionStage loopSelection, ExternalActionManager managerInstance)
|
||||||
|
{
|
||||||
|
var cfb = new CustomFormBaseMessageLoop();
|
||||||
|
|
||||||
|
cfb.ExternalActionManager = managerInstance;
|
||||||
|
|
||||||
|
return loopSelection.CustomMessageLoop(cfb);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
+13
@@ -0,0 +1,13 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net7.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\..\..\TelegramBotBase\TelegramBotBase.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
@@ -1,6 +1,27 @@
|
|||||||
|
# Development Lab
|
||||||
|
|
||||||
|
Attention: This is a lab for **experimental features**. Here we can try out different ideas and scenarios if they will work and what can be improved.
|
||||||
|
There is no guarantee that any of these ideas comes in the main project.
|
||||||
|
|
||||||
|
Please try everything on your own. Nothing can work, so everything can as well.
|
||||||
|
|
||||||
|
You will find all experiments within the ["Experiments"](Experiments) sub folder.
|
||||||
|
|
||||||
|
For feedback please join our Telegram group.
|
||||||
|
|
||||||
|
**Support group: [@tgbotbase](https://t.me/tgbotbase)**
|
||||||
|
|
||||||
|
## current experiments
|
||||||
|
|
||||||
|
- [ExternalActionManager](Experiments/ExternalActionManager/DemoBot) => Manager for unhandled actions from "outside"
|
||||||
|
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
|
||||||
# .NET Telegram Bot Framework - Context based addon
|
# .NET Telegram Bot Framework - Context based addon
|
||||||
|
|
||||||
[](https://www.nuget.org/packages/TelegramBotBase/)
|
[](https://www.nuget.org/packages/TelegramBotBase/)
|
||||||
[](https://www.t.me/tgbotbase)
|
[](https://www.t.me/tgbotbase)
|
||||||
|
|
||||||
[](https://raw.githubusercontent.com/MajMcCloud/TelegramBotFramework/master/LICENCE.md)
|
[](https://raw.githubusercontent.com/MajMcCloud/TelegramBotFramework/master/LICENCE.md)
|
||||||
@@ -723,7 +744,6 @@ The current available languages for controls are:
|
|||||||
- English
|
- English
|
||||||
- German
|
- German
|
||||||
- Persian
|
- Persian
|
||||||
- Russian
|
|
||||||
|
|
||||||
You can add other languages easily by creating a subclass of the [TelegramBotBase/Localizations/Localization.cs](TelegramBotBase/Localizations/Localization.cs) class.
|
You can add other languages easily by creating a subclass of the [TelegramBotBase/Localizations/Localization.cs](TelegramBotBase/Localizations/Localization.cs) class.
|
||||||
|
|
||||||
@@ -1040,8 +1060,6 @@ again at 1 (due to `PopAsync` or `PopToRootAsync` calls) it will replace the con
|
|||||||
you
|
you
|
||||||
have given to the constructor at the beginning.*
|
have given to the constructor at the beginning.*
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Extensions
|
## Extensions
|
||||||
|
|
||||||
### TelegramBotBase.Extensions.Images
|
### TelegramBotBase.Extensions.Images
|
||||||
@@ -1051,28 +1069,7 @@ Extends the base package with some additional image methods like SendPhoto (usin
|
|||||||
[](https://www.nuget.org/packages/TelegramBotBase.Extensions.Images/)
|
[](https://www.nuget.org/packages/TelegramBotBase.Extensions.Images/)
|
||||||
[](https://www.nuget.org/packages/TelegramBotBase.Extensions.Images)
|
[](https://www.nuget.org/packages/TelegramBotBase.Extensions.Images)
|
||||||
|
|
||||||
Source code: [TelegramBotBase.Extensions.Images/](/TelegramBotBase.Extensions.Images/)
|
[https://www.nuget.org/packages/TelegramBotBase.Extensions.Images/](https://www.nuget.org/packages/TelegramBotBase.Extensions.Images/)
|
||||||
|
|
||||||
Nuget package: [https://www.nuget.org/packages/TelegramBotBase.Extensions.Images/](https://www.nuget.org/packages/TelegramBotBase.Extensions.Images/)
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
### TelegramBotBase.Extensions.Images.IronSoftware
|
|
||||||
|
|
||||||
Extends the base package with some additional image methods like SendPhoto (using Bitmap)
|
|
||||||
|
|
||||||
Important: This extension uses the IronSoftware drawing library.
|
|
||||||
|
|
||||||
[](https://www.nuget.org/packages/TelegramBotBase.Extensions.Images.IronSoftware/)
|
|
||||||
[](https://www.nuget.org/packages/TelegramBotBase.Extensions.Images.IronSoftware)
|
|
||||||
|
|
||||||
[https://www.nuget.org/packages/TelegramBotBase.Extensions.Images.IronSoftware/](https://www.nuget.org/packages/TelegramBotBase.Extensions.Images.IronSoftware/)
|
|
||||||
|
|
||||||
Source code: [TelegramBotBase.Extensions.Images.IronSoftware/](TelegramBotBase.Extensions.Images.IronSoftware/)
|
|
||||||
|
|
||||||
Nuget package: [https://www.nuget.org/packages/TelegramBotBase.Extensions.IronSoftware/](https://www.nuget.org/packages/TelegramBotBase.Extensions.IronSoftware/)
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
Project: [open source](TelegramBotBase.Extensions.Images/)
|
Project: [open source](TelegramBotBase.Extensions.Images/)
|
||||||
|
|
||||||
@@ -1096,26 +1093,7 @@ A session serializer for Microsoft SQL Server.
|
|||||||
[](https://www.nuget.org/packages/TelegramBotBase.Extensions.Serializer.Database.MSSQL/)
|
[](https://www.nuget.org/packages/TelegramBotBase.Extensions.Serializer.Database.MSSQL/)
|
||||||
[](https://www.nuget.org/packages/TelegramBotBase.Extensions.Serializer.Database.MSSQL)
|
[](https://www.nuget.org/packages/TelegramBotBase.Extensions.Serializer.Database.MSSQL)
|
||||||
|
|
||||||
Source code: [TelegramBotBase.Extensions.Serializer.Database.MSSQL/](/TelegramBotBase.Extensions.Serializer.Database.MSSQL/)
|
[https://www.nuget.org/packages/TelegramBotBase.Extensions.Serializer.Database.MSSQL/](https://www.nuget.org/packages/TelegramBotBase.Extensions.Serializer.Database.MSSQL/)
|
||||||
|
|
||||||
Nuget package: [https://www.nuget.org/packages/TelegramBotBase.Extensions.Serializer.Database.MSSQL/](https://www.nuget.org/packages/TelegramBotBase.Extensions.Serializer.Database.MSSQL/)
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
### TelegramBotBase.Extensions.Serializer.Database.PostgreSql
|
|
||||||
|
|
||||||
A session serializer for PostgreSql Server.
|
|
||||||
|
|
||||||
[](https://www.nuget.org/packages/TelegramBotBase.Extensions.Serializer.Database.PostgreSql/)
|
|
||||||
[](https://www.nuget.org/packages/TelegramBotBase.Extensions.Serializer.Database.PostgreSql)
|
|
||||||
|
|
||||||
Source code: [TelegramBotBase.Extensions.Serializer.Database.PostgreSql/](/TelegramBotBase.Extensions.Serializer.Database.PostgreSql/)
|
|
||||||
|
|
||||||
Nuget package: [https://www.nuget.org/packages/TelegramBotBase.Extensions.Serializer.Database.PostgreSql/](https://www.nuget.org/packages/TelegramBotBase.Extensions.Serializer.Database.PostgreSql/)
|
|
||||||
|
|
||||||
Credits: [@Kataane](https://github.com/Kataane)
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
Project: [open source](TelegramBotBase.Extensions.Serializer.Database.MSSQL/)
|
Project: [open source](TelegramBotBase.Extensions.Serializer.Database.MSSQL/)
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ using System.IO;
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Telegram.Bot.Types;
|
using Telegram.Bot.Types;
|
||||||
using TelegramBotBase.Form;
|
using TelegramBotBase.Form;
|
||||||
using TelegramBotBase.Interfaces;
|
|
||||||
using TelegramBotBase.Sessions;
|
using TelegramBotBase.Sessions;
|
||||||
using static IronSoftware.Drawing.AnyBitmap;
|
using static IronSoftware.Drawing.AnyBitmap;
|
||||||
using SKImage = SixLabors.ImageSharp.Image;
|
using SKImage = SixLabors.ImageSharp.Image;
|
||||||
@@ -38,7 +37,7 @@ namespace TelegramBotBase.Extensions.Images.IronSoftware
|
|||||||
/// <param name="replyTo"></param>
|
/// <param name="replyTo"></param>
|
||||||
/// <param name="disableNotification"></param>
|
/// <param name="disableNotification"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static async Task<Message> SendPhoto(this IDeviceSession session, AnyBitmap image, string name,
|
public static async Task<Message> SendPhoto(this DeviceSession session, AnyBitmap image, string name,
|
||||||
string caption, ButtonForm buttons = null, int replyTo = 0,
|
string caption, ButtonForm buttons = null, int replyTo = 0,
|
||||||
bool disableNotification = false)
|
bool disableNotification = false)
|
||||||
{
|
{
|
||||||
@@ -59,7 +58,7 @@ namespace TelegramBotBase.Extensions.Images.IronSoftware
|
|||||||
/// <param name="replyTo"></param>
|
/// <param name="replyTo"></param>
|
||||||
/// <param name="disableNotification"></param>
|
/// <param name="disableNotification"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static async Task<Message> SendPhoto(this IDeviceSession session, SKImage image, string name,
|
public static async Task<Message> SendPhoto(this DeviceSession session, SKImage image, string name,
|
||||||
string caption, ButtonForm buttons = null, int replyTo = 0,
|
string caption, ButtonForm buttons = null, int replyTo = 0,
|
||||||
bool disableNotification = false)
|
bool disableNotification = false)
|
||||||
{
|
{
|
||||||
|
|||||||
+3
-6
@@ -1,7 +1,7 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFrameworks>netcoreapp3.1;net6</TargetFrameworks>
|
<TargetFrameworks>netstandard2.0;netcoreapp3.1;net6</TargetFrameworks>
|
||||||
<RepositoryUrl>https://github.com/MajMcCloud/TelegramBotFramework/tree/development/TelegramBotBase.Extensions.Images.IronSoftware</RepositoryUrl>
|
<RepositoryUrl>https://github.com/MajMcCloud/TelegramBotFramework/tree/development/TelegramBotBase.Extensions.Images.IronSoftware</RepositoryUrl>
|
||||||
<PackageProjectUrl>https://github.com/MajMcCloud/TelegramBotFramework/tree/development/TelegramBotBase.Extensions.Images.IronSoftware</PackageProjectUrl>
|
<PackageProjectUrl>https://github.com/MajMcCloud/TelegramBotFramework/tree/development/TelegramBotBase.Extensions.Images.IronSoftware</PackageProjectUrl>
|
||||||
<Copyright>MIT</Copyright>
|
<Copyright>MIT</Copyright>
|
||||||
@@ -14,19 +14,16 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="IronSoftware.System.Drawing" Version="2024.5.1" />
|
<PackageReference Include="IronSoftware.System.Drawing" Version="2024.1.1" />
|
||||||
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1">
|
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1">
|
||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
|
<PackageReference Include="TelegramBotBase" Version="6.0.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Folder Include="Properties\" />
|
<Folder Include="Properties\" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<ProjectReference Include="..\TelegramBotBase\TelegramBotBase.csproj" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ using System.IO;
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Telegram.Bot.Types;
|
using Telegram.Bot.Types;
|
||||||
using TelegramBotBase.Form;
|
using TelegramBotBase.Form;
|
||||||
using TelegramBotBase.Interfaces;
|
using TelegramBotBase.Sessions;
|
||||||
|
|
||||||
namespace TelegramBotBase.Extensions.Images
|
namespace TelegramBotBase.Extensions.Images
|
||||||
{
|
{
|
||||||
@@ -27,7 +27,7 @@ namespace TelegramBotBase.Extensions.Images
|
|||||||
/// <param name="replyTo"></param>
|
/// <param name="replyTo"></param>
|
||||||
/// <param name="disableNotification"></param>
|
/// <param name="disableNotification"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static async Task<Message> SendPhoto(this IDeviceSession session, Image image, string name,
|
public static async Task<Message> SendPhoto(this DeviceSession session, Image image, string name,
|
||||||
string caption, ButtonForm buttons = null, int replyTo = 0,
|
string caption, ButtonForm buttons = null, int replyTo = 0,
|
||||||
bool disableNotification = false)
|
bool disableNotification = false)
|
||||||
{
|
{
|
||||||
@@ -48,7 +48,7 @@ namespace TelegramBotBase.Extensions.Images
|
|||||||
/// <param name="replyTo"></param>
|
/// <param name="replyTo"></param>
|
||||||
/// <param name="disableNotification"></param>
|
/// <param name="disableNotification"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static async Task<Message> SendPhoto(this IDeviceSession session, Bitmap image, string name,
|
public static async Task<Message> SendPhoto(this DeviceSession session, Bitmap image, string name,
|
||||||
string caption, ButtonForm buttons = null, int replyTo = 0,
|
string caption, ButtonForm buttons = null, int replyTo = 0,
|
||||||
bool disableNotification = false)
|
bool disableNotification = false)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFrameworks>netcoreapp3.1;net6;net7;net8</TargetFrameworks>
|
<TargetFrameworks>netstandard2.0;netcoreapp3.1;net6</TargetFrameworks>
|
||||||
<RepositoryUrl>https://github.com/MajMcCloud/TelegramBotFramework/tree/development/TelegramBotBase.Extensions.Images</RepositoryUrl>
|
<RepositoryUrl>https://github.com/MajMcCloud/TelegramBotFramework/tree/development/TelegramBotBase.Extensions.Images</RepositoryUrl>
|
||||||
<PackageProjectUrl>https://github.com/MajMcCloud/TelegramBotFramework/tree/development/TelegramBotBase.Extensions.Images</PackageProjectUrl>
|
<PackageProjectUrl>https://github.com/MajMcCloud/TelegramBotFramework/tree/development/TelegramBotBase.Extensions.Images</PackageProjectUrl>
|
||||||
<Copyright>MIT</Copyright>
|
<Copyright>MIT</Copyright>
|
||||||
@@ -21,14 +21,11 @@
|
|||||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
<PackageReference Include="System.Drawing.Common" Version="6.0.0" />
|
<PackageReference Include="System.Drawing.Common" Version="6.0.0" />
|
||||||
|
<PackageReference Include="TelegramBotBase" Version="6.0.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Folder Include="Properties\" />
|
<Folder Include="Properties\" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<ProjectReference Include="..\TelegramBotBase\TelegramBotBase.csproj" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
+1
-1
@@ -16,7 +16,7 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.2.0" />
|
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.1.3" />
|
||||||
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1">
|
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1">
|
||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
|||||||
@@ -1,92 +0,0 @@
|
|||||||
using System;
|
|
||||||
using TelegramBotBase.Builder;
|
|
||||||
using TelegramBotBase.Builder.Interfaces;
|
|
||||||
|
|
||||||
namespace TelegramBotBase.Extensions.Serializer.Database.PostgreSql
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Provides extension methods for configuring the use of PostgreSQL Server Database for session serialization.
|
|
||||||
/// </summary>
|
|
||||||
public static class BotBaseBuilderExtensions
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Uses an PostgreSQL Server Database to save and restore sessions.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="builder">The session serialization stage builder.</param>
|
|
||||||
/// <param name="connectionString">The connection string to the PostgreSQL database.</param>
|
|
||||||
/// <param name="fallbackForm">The fallback form type.</param>
|
|
||||||
/// <param name="tablePrefix">The prefix for database table names (default is "tgb_").</param>
|
|
||||||
/// <returns>The language selection stage builder.</returns>
|
|
||||||
public static ILanguageSelectionStage UsePostgreSqlDatabase(
|
|
||||||
this ISessionSerializationStage builder,
|
|
||||||
string connectionString, Type fallbackForm = null,
|
|
||||||
string tablePrefix = "tgb_")
|
|
||||||
{
|
|
||||||
var serializer = new PostgreSqlSerializer(connectionString, tablePrefix, fallbackForm);
|
|
||||||
|
|
||||||
builder.UseSerialization(serializer);
|
|
||||||
|
|
||||||
return builder as BotBaseBuilder;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Uses an PostgreSQL Server Database to save and restore sessions.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="builder">The session serialization stage builder.</param>
|
|
||||||
/// <param name="hostOrIp">The host or IP address of the PostgreSQL server.</param>
|
|
||||||
/// <param name="port">The port number for the PostgreSQL server.</param>
|
|
||||||
/// <param name="databaseName">The name of the PostgreSQL database.</param>
|
|
||||||
/// <param name="userId">The user ID for connecting to the PostgreSQL server.</param>
|
|
||||||
/// <param name="password">The password for connecting to the PostgreSQL server.</param>
|
|
||||||
/// <param name="fallbackForm">The fallback form type.</param>
|
|
||||||
/// <param name="tablePrefix">The prefix for database table names (default is "tgb_").</param>
|
|
||||||
/// <returns>The language selection stage builder.</returns>
|
|
||||||
public static ILanguageSelectionStage UsePostgreSqlDatabase(
|
|
||||||
this ISessionSerializationStage builder,
|
|
||||||
string hostOrIp, int port,
|
|
||||||
string databaseName, string userId,
|
|
||||||
string password, Type fallbackForm = null,
|
|
||||||
string tablePrefix = "tgb_")
|
|
||||||
{
|
|
||||||
var connectionString = $"Host={hostOrIp};Port={port};Database={databaseName};Username={userId};Password={password}";
|
|
||||||
|
|
||||||
var serializer = new PostgreSqlSerializer(connectionString, tablePrefix, fallbackForm);
|
|
||||||
|
|
||||||
builder.UseSerialization(serializer);
|
|
||||||
|
|
||||||
return builder as BotBaseBuilder;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Uses an PostgreSQL Server Database with Windows Authentication to save and restore sessions.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="builder">The session serialization stage builder.</param>
|
|
||||||
/// <param name="hostOrIp">The host or IP address of the PostgreSQL server.</param>
|
|
||||||
/// <param name="port">The port number for the PostgreSQL server.</param>
|
|
||||||
/// <param name="databaseName">The name of the PostgreSQL database.</param>
|
|
||||||
/// <param name="integratedSecurity">A flag indicating whether to use Windows Authentication (true) or not (false).</param>
|
|
||||||
/// <param name="fallbackForm">The fallback form type.</param>
|
|
||||||
/// <param name="tablePrefix">The prefix for database table names (default is "tgb_").</param>
|
|
||||||
/// <returns>The language selection stage builder.</returns>
|
|
||||||
public static ILanguageSelectionStage UsePostgreSqlDatabase(
|
|
||||||
this ISessionSerializationStage builder,
|
|
||||||
string hostOrIp, int port,
|
|
||||||
string databaseName, bool integratedSecurity = true,
|
|
||||||
Type fallbackForm = null, string tablePrefix = "tgb_")
|
|
||||||
{
|
|
||||||
if (!integratedSecurity)
|
|
||||||
{
|
|
||||||
throw new ArgumentOutOfRangeException();
|
|
||||||
}
|
|
||||||
|
|
||||||
var connectionString = $"Host={hostOrIp};Port={port};Database={databaseName};Integrated Security=true;";
|
|
||||||
|
|
||||||
var serializer = new PostgreSqlSerializer(connectionString, tablePrefix, fallbackForm);
|
|
||||||
|
|
||||||
builder.UseSerialization(serializer);
|
|
||||||
|
|
||||||
return builder as BotBaseBuilder;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,252 +0,0 @@
|
|||||||
using Npgsql;
|
|
||||||
using System;
|
|
||||||
using System.Data;
|
|
||||||
using NpgsqlTypes;
|
|
||||||
using TelegramBotBase.Args;
|
|
||||||
using TelegramBotBase.Base;
|
|
||||||
using TelegramBotBase.Form;
|
|
||||||
using TelegramBotBase.Interfaces;
|
|
||||||
|
|
||||||
namespace TelegramBotBase.Extensions.Serializer.Database.PostgreSql
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Represents a PostgreSQL implementation of the <see cref="IStateMachine"/> for saving and loading form states.
|
|
||||||
/// </summary>
|
|
||||||
public class PostgreSqlSerializer : IStateMachine
|
|
||||||
{
|
|
||||||
private readonly string insertIntoSessionSql;
|
|
||||||
private readonly string insertIntoSessionsDataSql;
|
|
||||||
private readonly string selectAllDevicesSessionsSql;
|
|
||||||
private readonly string selectAllDevicesSessionsDataSql;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Initializes a new instance of the <see cref="PostgreSqlSerializer"/> class.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="connectionString">The connection string to the PostgreSQL database.</param>
|
|
||||||
/// <param name="tablePrefix">The prefix for database table names (default is "tgb_").</param>
|
|
||||||
/// <param name="fallbackStateForm">The fallback state form type.</param>
|
|
||||||
/// <exception cref="ArgumentNullException">Thrown when <paramref name="connectionString"/> is null.</exception>
|
|
||||||
/// <exception cref="ArgumentException">Thrown when <paramref name="fallbackStateForm"/> is not a subclass of <see cref="FormBase"/>.</exception>
|
|
||||||
public PostgreSqlSerializer(string connectionString, string tablePrefix = "tgb_", Type fallbackStateForm = null)
|
|
||||||
{
|
|
||||||
ConnectionString = connectionString ?? throw new ArgumentNullException(nameof(connectionString));
|
|
||||||
|
|
||||||
TablePrefix = tablePrefix;
|
|
||||||
|
|
||||||
FallbackStateForm = fallbackStateForm;
|
|
||||||
|
|
||||||
if (FallbackStateForm != null && !FallbackStateForm.IsSubclassOf(typeof(FormBase)))
|
|
||||||
{
|
|
||||||
throw new ArgumentException($"{nameof(FallbackStateForm)} is not a subclass of {nameof(FormBase)}");
|
|
||||||
}
|
|
||||||
|
|
||||||
insertIntoSessionSql = "INSERT INTO " + TablePrefix +
|
|
||||||
"devices_sessions (deviceId, deviceTitle, \"FormUri\", \"QualifiedName\") VALUES (@deviceId, @deviceTitle, @FormUri, @QualifiedName)";
|
|
||||||
insertIntoSessionsDataSql = "INSERT INTO " + TablePrefix + "devices_sessions_data (deviceId, key, value, type) VALUES (@deviceId, @key, @value, @type)";
|
|
||||||
|
|
||||||
selectAllDevicesSessionsSql = "SELECT * FROM " + TablePrefix + "devices_sessions";
|
|
||||||
selectAllDevicesSessionsDataSql = "SELECT * FROM " + TablePrefix + "devices_sessions_data WHERE deviceId = @deviceId";
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the connection string to the PostgreSQL database.
|
|
||||||
/// </summary>
|
|
||||||
public string ConnectionString { get; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets or sets the table name prefix for database tables.
|
|
||||||
/// </summary>
|
|
||||||
public string TablePrefix { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets or sets the fallback state form type.
|
|
||||||
/// </summary>
|
|
||||||
public Type FallbackStateForm { get; set; }
|
|
||||||
|
|
||||||
/// <inheritdoc/>
|
|
||||||
/// <summary>
|
|
||||||
/// Saves form states to the PostgreSQL database.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="e">The <see cref="SaveStatesEventArgs"/> containing the states to be saved.</param>
|
|
||||||
public void SaveFormStates(SaveStatesEventArgs e)
|
|
||||||
{
|
|
||||||
var container = e.States;
|
|
||||||
|
|
||||||
//Cleanup old Session data
|
|
||||||
Cleanup();
|
|
||||||
|
|
||||||
using (var connection = new NpgsqlConnection(ConnectionString))
|
|
||||||
{
|
|
||||||
connection.Open();
|
|
||||||
|
|
||||||
//Store session data in database
|
|
||||||
foreach (var state in container.States)
|
|
||||||
{
|
|
||||||
using (var sessionCommand = connection.CreateCommand())
|
|
||||||
{
|
|
||||||
sessionCommand.CommandText = insertIntoSessionSql;
|
|
||||||
|
|
||||||
sessionCommand.Parameters.Add(new NpgsqlParameter("@deviceId", NpgsqlDbType.Bigint){Value = state.DeviceId });
|
|
||||||
sessionCommand.Parameters.Add(new NpgsqlParameter("@deviceTitle", DbType.StringFixedLength){Value = state.ChatTitle ?? string.Empty});
|
|
||||||
sessionCommand.Parameters.Add(new NpgsqlParameter("@FormUri", DbType.StringFixedLength) {Value = state.FormUri});
|
|
||||||
sessionCommand.Parameters.Add(new NpgsqlParameter("@QualifiedName", DbType.StringFixedLength){Value = state.QualifiedName });
|
|
||||||
|
|
||||||
sessionCommand.ExecuteNonQuery();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
using (var connection = new NpgsqlConnection(ConnectionString))
|
|
||||||
{
|
|
||||||
connection.Open();
|
|
||||||
|
|
||||||
foreach (var state in container.States)
|
|
||||||
{
|
|
||||||
SaveSessionsData(state, connection);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc/>
|
|
||||||
/// <summary>
|
|
||||||
/// Loads form states from the PostgreSQL database.
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>A <see cref="StateContainer"/> containing the loaded form states.</returns>
|
|
||||||
public StateContainer LoadFormStates()
|
|
||||||
{
|
|
||||||
var stateContainer = new StateContainer();
|
|
||||||
|
|
||||||
using (var connection = new NpgsqlConnection(ConnectionString))
|
|
||||||
{
|
|
||||||
connection.Open();
|
|
||||||
|
|
||||||
using (var sessionCommand = connection.CreateCommand())
|
|
||||||
{
|
|
||||||
sessionCommand.CommandText = selectAllDevicesSessionsSql;
|
|
||||||
|
|
||||||
var sessionTable = new DataTable();
|
|
||||||
using (var dataAdapter = new NpgsqlDataAdapter(sessionCommand))
|
|
||||||
{
|
|
||||||
dataAdapter.Fill(sessionTable);
|
|
||||||
|
|
||||||
foreach (DataRow row in sessionTable.Rows)
|
|
||||||
{
|
|
||||||
var stateEntry = new StateEntry
|
|
||||||
{
|
|
||||||
DeviceId = (long)row["deviceId"],
|
|
||||||
ChatTitle = row["deviceTitle"].ToString(),
|
|
||||||
FormUri = row["FormUri"].ToString(),
|
|
||||||
QualifiedName = row["QualifiedName"].ToString()
|
|
||||||
};
|
|
||||||
|
|
||||||
stateContainer.States.Add(stateEntry);
|
|
||||||
|
|
||||||
if (stateEntry.DeviceId > 0)
|
|
||||||
{
|
|
||||||
stateContainer.ChatIds.Add(stateEntry.DeviceId);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
stateContainer.GroupIds.Add(stateEntry.DeviceId);
|
|
||||||
}
|
|
||||||
|
|
||||||
LoadDataTable(connection, row, stateEntry);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return stateContainer;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Cleans up old session data in the PostgreSQL database.
|
|
||||||
/// </summary>
|
|
||||||
private void Cleanup()
|
|
||||||
{
|
|
||||||
using (var connection = new NpgsqlConnection(ConnectionString))
|
|
||||||
{
|
|
||||||
connection.Open();
|
|
||||||
|
|
||||||
using (var clearCommand = connection.CreateCommand())
|
|
||||||
{
|
|
||||||
clearCommand.CommandText = $"DELETE FROM {TablePrefix}devices_sessions_data";
|
|
||||||
clearCommand.ExecuteNonQuery();
|
|
||||||
}
|
|
||||||
|
|
||||||
using (var clearCommand = connection.CreateCommand())
|
|
||||||
{
|
|
||||||
clearCommand.CommandText = $"DELETE FROM {TablePrefix}devices_sessions";
|
|
||||||
clearCommand.ExecuteNonQuery();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Saves session data to the PostgreSQL database.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="state">The state entry containing session data to be saved.</param>
|
|
||||||
/// <param name="connection">The NpgsqlConnection used for the database interaction.</param>
|
|
||||||
private void SaveSessionsData(StateEntry state, NpgsqlConnection connection)
|
|
||||||
{
|
|
||||||
foreach (var data in state.Values)
|
|
||||||
{
|
|
||||||
using (var dataCommand = connection.CreateCommand())
|
|
||||||
{
|
|
||||||
dataCommand.CommandText = insertIntoSessionsDataSql;
|
|
||||||
|
|
||||||
dataCommand.Parameters.Add(new NpgsqlParameter("@deviceId", NpgsqlDbType.Bigint) { Value = state.DeviceId });
|
|
||||||
dataCommand.Parameters.Add(new NpgsqlParameter("@key", DbType.StringFixedLength) { Value = data.Key });
|
|
||||||
|
|
||||||
var type = data.Value.GetType();
|
|
||||||
|
|
||||||
if (type.IsPrimitive || type == typeof(string))
|
|
||||||
{
|
|
||||||
dataCommand.Parameters.Add(new NpgsqlParameter("@value", NpgsqlDbType.Text) { Value = data.Value });
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
var json = System.Text.Json.JsonSerializer.Serialize(data.Value);
|
|
||||||
dataCommand.Parameters.Add(new NpgsqlParameter("@value", NpgsqlDbType.Text) { Value = json });
|
|
||||||
}
|
|
||||||
|
|
||||||
dataCommand.Parameters.Add(new NpgsqlParameter("@type", DbType.StringFixedLength) { Value = type.AssemblyQualifiedName });
|
|
||||||
|
|
||||||
dataCommand.ExecuteNonQuery();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Loads session data from the PostgreSQL database.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="connection">The NpgsqlConnection used for the database interaction.</param>
|
|
||||||
/// <param name="row">The DataRow representing a session entry in the main sessions table.</param>
|
|
||||||
/// <param name="stateEntry">The StateEntry object to which session data will be loaded.</param>
|
|
||||||
private void LoadDataTable(NpgsqlConnection connection, DataRow row, StateEntry stateEntry)
|
|
||||||
{
|
|
||||||
using (var sessionCommand = connection.CreateCommand())
|
|
||||||
{
|
|
||||||
sessionCommand.CommandText = selectAllDevicesSessionsDataSql;
|
|
||||||
sessionCommand.Parameters.Add(new NpgsqlParameter("@deviceId", row["deviceId"]));
|
|
||||||
|
|
||||||
var dataCommandTable = new DataTable();
|
|
||||||
using (var npgSqlDataAdapter = new NpgsqlDataAdapter(sessionCommand))
|
|
||||||
{
|
|
||||||
npgSqlDataAdapter.Fill(dataCommandTable);
|
|
||||||
|
|
||||||
foreach (DataRow dataRow in dataCommandTable.Rows)
|
|
||||||
{
|
|
||||||
var key = dataRow["key"].ToString();
|
|
||||||
var type = Type.GetType(dataRow["type"].ToString());
|
|
||||||
|
|
||||||
var value = System.Text.Json.JsonSerializer.Deserialize(dataRow["value"].ToString(), type);
|
|
||||||
|
|
||||||
stateEntry.Values.Add(key, value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,27 +0,0 @@
|
|||||||
# TelegramBotBase.Extensions.Serializer.Database.PostgreSQL
|
|
||||||
|
|
||||||
[](https://www.nuget.org/packages/TelegramBotBase.Extensions.Serializer.Database.PostgreSQL/)
|
|
||||||
[](https://www.t.me/tgbotbase)
|
|
||||||
|
|
||||||
[](https://raw.githubusercontent.com/MajMcCloud/TelegramBotFramework/master/LICENCE.md)
|
|
||||||
[](https://www.nuget.org/packages/TelegramBotBase.Extensions.Serializer.Database.PostgreSQL)
|
|
||||||
|
|
||||||
## How to use
|
|
||||||
|
|
||||||
```csharp
|
|
||||||
using TelegramBotBase.Extensions.Serializer.Database.PostgreSQL;
|
|
||||||
|
|
||||||
|
|
||||||
var bot = BotBaseBuilder
|
|
||||||
.Create()
|
|
||||||
.WithAPIKey(APIKey)
|
|
||||||
.DefaultMessageLoop()
|
|
||||||
.WithStartForm<Start>()
|
|
||||||
.NoProxy()
|
|
||||||
.OnlyStart()
|
|
||||||
.UsePostgreSqlDatabase("localhost", "8181", "telegram_bot")
|
|
||||||
.UseEnglish()
|
|
||||||
.Build();
|
|
||||||
|
|
||||||
bot.Start();
|
|
||||||
```
|
|
||||||
-28
@@ -1,28 +0,0 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
|
||||||
|
|
||||||
<PropertyGroup>
|
|
||||||
<TargetFrameworks>netstandard2.0;netcoreapp3.1;net6</TargetFrameworks>
|
|
||||||
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
|
|
||||||
<RepositoryUrl>https://github.com/MajMcCloud/TelegramBotFramework</RepositoryUrl>
|
|
||||||
<PackageProjectUrl>https://github.com/MajMcCloud/TelegramBotFramework</PackageProjectUrl>
|
|
||||||
<Copyright>MIT</Copyright>
|
|
||||||
<IncludeSymbols>true</IncludeSymbols>
|
|
||||||
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
|
|
||||||
<AssemblyVersion>1.0.1</AssemblyVersion>
|
|
||||||
<FileVersion>1.0.1</FileVersion>
|
|
||||||
<Version>1.0.1</Version>
|
|
||||||
<Description>
|
|
||||||
A session serializer for PostgreSQL Server.
|
|
||||||
</Description>
|
|
||||||
</PropertyGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1">
|
|
||||||
<PrivateAssets>all</PrivateAssets>
|
|
||||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
|
||||||
</PackageReference>
|
|
||||||
<PackageReference Include="TelegramBotBase" Version="6.0.0" />
|
|
||||||
<PackageReference Include="Npgsql" Version="8.0.3" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
</Project>
|
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
-- Enable uuid-ossp extension
|
|
||||||
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
|
|
||||||
|
|
||||||
-- Create table tgb_devices_sessions
|
|
||||||
CREATE TABLE tgb_devices_sessions (
|
|
||||||
deviceId bigint NOT NULL,
|
|
||||||
deviceTitle character varying(512) NOT NULL,
|
|
||||||
"FormUri" character varying(512) NOT NULL,
|
|
||||||
"QualifiedName" character varying(512) NOT NULL,
|
|
||||||
CONSTRAINT PK_tgb_devices_sessions_1 PRIMARY KEY (deviceId)
|
|
||||||
);
|
|
||||||
|
|
||||||
-- Create table tgb_devices_sessions_data
|
|
||||||
CREATE TABLE tgb_devices_sessions_data (
|
|
||||||
Id uuid DEFAULT uuid_generate_v4() NOT NULL,
|
|
||||||
deviceId bigint NOT NULL,
|
|
||||||
key character varying(512) NOT NULL,
|
|
||||||
"value" text NOT NULL,
|
|
||||||
"type" character varying(512) NOT NULL,
|
|
||||||
CONSTRAINT PK_tgb_devices_session_data PRIMARY KEY (Id)
|
|
||||||
);
|
|
||||||
|
|
||||||
-- Add default constraint for Id column in tgb_devices_sessions_data
|
|
||||||
ALTER TABLE tgb_devices_sessions_data
|
|
||||||
ALTER COLUMN Id SET DEFAULT uuid_generate_v4();
|
|
||||||
-41
@@ -1,41 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.IO;
|
|
||||||
using TelegramBotBase.Builder;
|
|
||||||
using TelegramBotBase.Builder.Interfaces;
|
|
||||||
|
|
||||||
namespace TelegramBotBase.Extensions.Serializer.Legacy.NewtonsoftJson
|
|
||||||
{
|
|
||||||
public static class BotBaseBuilderExtensions
|
|
||||||
{
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Using the complex version of .Net JSON, which can serialize all objects.
|
|
||||||
/// Saves in application directory.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="path"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public static ILanguageSelectionStage UseNewtonsoftJson(this ISessionSerializationStage builder)
|
|
||||||
{
|
|
||||||
var path = Path.Combine(Directory.GetCurrentDirectory(), "states.json");
|
|
||||||
|
|
||||||
builder.UseNewtonsoftJson(path);
|
|
||||||
|
|
||||||
return builder as BotBaseBuilder;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Using the complex version of .Net JSON, which can serialize all objects.
|
|
||||||
/// Saves in application directory.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="path"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public static ILanguageSelectionStage UseNewtonsoftJson(this ISessionSerializationStage builder, String path)
|
|
||||||
{
|
|
||||||
var _stateMachine = new NewtonsoftJsonStateMachine(path);
|
|
||||||
|
|
||||||
builder.UseSerialization(_stateMachine);
|
|
||||||
|
|
||||||
return builder as BotBaseBuilder;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-94
@@ -1,94 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Data;
|
|
||||||
using System.IO;
|
|
||||||
using Newtonsoft.Json;
|
|
||||||
using TelegramBotBase.Args;
|
|
||||||
using TelegramBotBase.Base;
|
|
||||||
using TelegramBotBase.Form;
|
|
||||||
using TelegramBotBase.Interfaces;
|
|
||||||
|
|
||||||
namespace TelegramBotBase.Extensions.Serializer.Legacy.NewtonsoftJson
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Is used for all complex data types. Use if other default machines are not working.
|
|
||||||
/// </summary>
|
|
||||||
public class NewtonsoftJsonStateMachine : IStateMachine
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Will initialize the state machine.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="file">Path of the file and name where to save the session details.</param>
|
|
||||||
/// <param name="fallbackStateForm">
|
|
||||||
/// Type of Form which will be saved instead of Form which has
|
|
||||||
/// <seealso cref="Attributes.IgnoreState" /> attribute declared. Needs to be subclass of
|
|
||||||
/// <seealso cref="Form.FormBase" />.
|
|
||||||
/// </param>
|
|
||||||
/// <param name="overwrite">Declares of the file could be overwritten.</param>
|
|
||||||
public NewtonsoftJsonStateMachine(string file, Type fallbackStateForm = null, bool overwrite = true)
|
|
||||||
{
|
|
||||||
FallbackStateForm = fallbackStateForm;
|
|
||||||
|
|
||||||
if (FallbackStateForm != null && !FallbackStateForm.IsSubclassOf(typeof(FormBase)))
|
|
||||||
{
|
|
||||||
throw new ArgumentException($"{nameof(FallbackStateForm)} is not a subclass of {nameof(FormBase)}");
|
|
||||||
}
|
|
||||||
|
|
||||||
FilePath = file ?? throw new ArgumentNullException(nameof(file));
|
|
||||||
Overwrite = overwrite;
|
|
||||||
}
|
|
||||||
|
|
||||||
public string FilePath { get; set; }
|
|
||||||
|
|
||||||
public bool Overwrite { get; set; }
|
|
||||||
|
|
||||||
public Type FallbackStateForm { get; }
|
|
||||||
|
|
||||||
public StateContainer LoadFormStates()
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var content = File.ReadAllText(FilePath);
|
|
||||||
|
|
||||||
var sc = JsonConvert.DeserializeObject<StateContainer>(content, new JsonSerializerSettings
|
|
||||||
{
|
|
||||||
TypeNameHandling = TypeNameHandling.All,
|
|
||||||
TypeNameAssemblyFormatHandling = TypeNameAssemblyFormatHandling.Simple
|
|
||||||
});
|
|
||||||
|
|
||||||
return sc;
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
return new StateContainer();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void SaveFormStates(SaveStatesEventArgs e)
|
|
||||||
{
|
|
||||||
if (File.Exists(FilePath))
|
|
||||||
{
|
|
||||||
if (!Overwrite)
|
|
||||||
{
|
|
||||||
throw new Exception("File exists already.");
|
|
||||||
}
|
|
||||||
|
|
||||||
File.Delete(FilePath);
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var content = JsonConvert.SerializeObject(e.States, Formatting.Indented, new JsonSerializerSettings
|
|
||||||
{
|
|
||||||
TypeNameHandling = TypeNameHandling.All,
|
|
||||||
TypeNameAssemblyFormatHandling = TypeNameAssemblyFormatHandling.Simple
|
|
||||||
});
|
|
||||||
|
|
||||||
File.WriteAllText(FilePath, content);
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,31 +0,0 @@
|
|||||||
# TelegramBotBase.Extensions.Serializer.Legacy.NewtonsoftJson
|
|
||||||
|
|
||||||
[](https://www.nuget.org/packages/TelegramBotBase.Extensions.Serializer.Legacy.NewtonsoftJson/)
|
|
||||||
[](https://www.t.me/tgbotbase)
|
|
||||||
|
|
||||||
[](https://raw.githubusercontent.com/MajMcCloud/TelegramBotFramework/master/LICENCE.md)
|
|
||||||
[](https://www.nuget.org/packages/TelegramBotBase.Extensions.Serializer.Legacy.NewtonsoftJson)
|
|
||||||
|
|
||||||
|
|
||||||
### Legacy version to recover with old dependencies using Newtonsoft.Json for session serialization
|
|
||||||
|
|
||||||
|
|
||||||
## How to use
|
|
||||||
|
|
||||||
```csharp
|
|
||||||
using TelegramBotBase.Extensions.Serializer.Legacy.NewtonsoftJson;
|
|
||||||
|
|
||||||
|
|
||||||
var bot = BotBaseBuilder
|
|
||||||
.Create()
|
|
||||||
.WithAPIKey(APIKey)
|
|
||||||
.DefaultMessageLoop()
|
|
||||||
.WithStartForm<Start>()
|
|
||||||
.NoProxy()
|
|
||||||
.OnlyStart()
|
|
||||||
.UseNewtonsoftJson()
|
|
||||||
.UseEnglish()
|
|
||||||
.Build();
|
|
||||||
|
|
||||||
bot.Start();
|
|
||||||
```
|
|
||||||
-27
@@ -1,27 +0,0 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
|
||||||
|
|
||||||
<PropertyGroup>
|
|
||||||
<TargetFrameworks>netstandard2.0;netcoreapp3.1;net6;net7;net8;net9</TargetFrameworks>
|
|
||||||
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
|
|
||||||
<RepositoryUrl>https://github.com/MajMcCloud/TelegramBotFramework</RepositoryUrl>
|
|
||||||
<PackageProjectUrl>https://github.com/MajMcCloud/TelegramBotFramework</PackageProjectUrl>
|
|
||||||
<Copyright>MIT</Copyright>
|
|
||||||
<IncludeSymbols>true</IncludeSymbols>
|
|
||||||
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
|
|
||||||
<AssemblyVersion>1.0.1</AssemblyVersion>
|
|
||||||
<FileVersion>1.0.1</FileVersion>
|
|
||||||
<Version>1.0.1</Version>
|
|
||||||
<Description>A session serializer for Newtonsoft Json.
|
|
||||||
</Description>
|
|
||||||
</PropertyGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1">
|
|
||||||
<PrivateAssets>all</PrivateAssets>
|
|
||||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
|
||||||
</PackageReference>
|
|
||||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
|
||||||
<PackageReference Include="TelegramBotBase" Version="6.0.0" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
</Project>
|
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -1,25 +0,0 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
|
||||||
|
|
||||||
<PropertyGroup>
|
|
||||||
<TargetFramework>netstandard2.0</TargetFramework>
|
|
||||||
<ImplicitUsings>disable</ImplicitUsings>
|
|
||||||
<Nullable>enable</Nullable>
|
|
||||||
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
|
|
||||||
<OutputItemType>Analyzer</OutputItemType>
|
|
||||||
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
|
|
||||||
<LangVersion>latest</LangVersion>
|
|
||||||
</PropertyGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<EmbeddedResource Include="Resources\Telegram.Bot.xml" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4">
|
|
||||||
<PrivateAssets>all</PrivateAssets>
|
|
||||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
|
||||||
</PackageReference>
|
|
||||||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.10.0" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
</Project>
|
|
||||||
@@ -1,251 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Diagnostics;
|
|
||||||
using System.IO;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Reflection;
|
|
||||||
using System.Reflection.Metadata;
|
|
||||||
using System.Resources;
|
|
||||||
using System.Security.Cryptography.X509Certificates;
|
|
||||||
using System.Text;
|
|
||||||
using Microsoft.CodeAnalysis;
|
|
||||||
using Microsoft.CodeAnalysis.CSharp;
|
|
||||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
|
||||||
using Microsoft.CodeAnalysis.Text;
|
|
||||||
using TelegramBotBase.SourceGenerators;
|
|
||||||
|
|
||||||
namespace TelegramBotBase
|
|
||||||
{
|
|
||||||
|
|
||||||
[Generator(LanguageNames.CSharp)]
|
|
||||||
public class TelegramDeviceExtensionGenerator : IIncrementalGenerator
|
|
||||||
{
|
|
||||||
static XmlDocumentationLoader xml;
|
|
||||||
|
|
||||||
|
|
||||||
public void Initialize(IncrementalGeneratorInitializationContext context)
|
|
||||||
{
|
|
||||||
var provider = context.SyntaxProvider.CreateSyntaxProvider(
|
|
||||||
predicate: (c, _) => c is ClassDeclarationSyntax,
|
|
||||||
transform: (n, _) => (ClassDeclarationSyntax)n.Node)
|
|
||||||
.Where(a => a is not null);
|
|
||||||
|
|
||||||
|
|
||||||
var compilation = context.CompilationProvider;
|
|
||||||
|
|
||||||
context.RegisterSourceOutput(compilation, (spc, source) => Execute(spc, source));
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private void Execute(SourceProductionContext context, Compilation compilation)
|
|
||||||
{
|
|
||||||
//if (!Debugger.IsAttached) Debugger.Launch();
|
|
||||||
|
|
||||||
StringBuilder sb = new StringBuilder();
|
|
||||||
sb.AppendLine();
|
|
||||||
|
|
||||||
//Search for reference library
|
|
||||||
var telegram_package = compilation.References.FirstOrDefault(a => a.Display != null && a.Display.Contains("Telegram.Bot"));
|
|
||||||
if (telegram_package == null)
|
|
||||||
return;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//Load only once
|
|
||||||
if (xml == null)
|
|
||||||
{
|
|
||||||
xml = new XmlDocumentationLoader();
|
|
||||||
xml.ReadEmbeddedXml("Telegram.Bot.xml");
|
|
||||||
}
|
|
||||||
|
|
||||||
var assemblySymbol = compilation.GetAssemblyOrModuleSymbol(telegram_package) as IAssemblySymbol;
|
|
||||||
|
|
||||||
if (assemblySymbol == null)
|
|
||||||
return;
|
|
||||||
|
|
||||||
if (assemblySymbol.Name != "Telegram.Bot")
|
|
||||||
return;
|
|
||||||
|
|
||||||
//Get class which includes the existing methods
|
|
||||||
var apiClass = assemblySymbol.GetTypeByMetadataName("Telegram.Bot.TelegramBotClientExtensions");
|
|
||||||
if (apiClass == null)
|
|
||||||
return;
|
|
||||||
|
|
||||||
//Get existing list of methods
|
|
||||||
var methods = apiClass.GetMembers().OfType<IMethodSymbol>().ToList();
|
|
||||||
|
|
||||||
|
|
||||||
foreach (var method in methods)
|
|
||||||
{
|
|
||||||
if (!method.Parameters.Any(a => a.Type.Name == "ITelegramBotClient"))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if (!method.Parameters.Any(a => a.Type.Name == "ChatId"))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if (method.Name == ".ctor")
|
|
||||||
continue;
|
|
||||||
|
|
||||||
String parameters = "";
|
|
||||||
String subCallParameters = "";
|
|
||||||
foreach (var par in method.Parameters)
|
|
||||||
{
|
|
||||||
if (par.Name == "botClient")
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(parameters))
|
|
||||||
{
|
|
||||||
parameters += ", ";
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(subCallParameters))
|
|
||||||
{
|
|
||||||
subCallParameters += ", ";
|
|
||||||
}
|
|
||||||
|
|
||||||
if (par.Name == "chatId")
|
|
||||||
{
|
|
||||||
subCallParameters += $"device.DeviceId";
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
subCallParameters += $"{par.Name}";
|
|
||||||
parameters += $"{par.Type.ToDisplayString()} {par.Name}";
|
|
||||||
|
|
||||||
if (par.HasExplicitDefaultValue)
|
|
||||||
{
|
|
||||||
var defaultValue = par.ExplicitDefaultValue;
|
|
||||||
|
|
||||||
// Handle specific default value cases
|
|
||||||
if (defaultValue == null)
|
|
||||||
{
|
|
||||||
if(par.Name == "cancellationToken")
|
|
||||||
{
|
|
||||||
parameters += " = default";
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
parameters += " = null";
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
else if (defaultValue is string)
|
|
||||||
{
|
|
||||||
parameters += $" = \"{defaultValue}\""; // Add quotes around string default values
|
|
||||||
}
|
|
||||||
else if (defaultValue is bool)
|
|
||||||
{
|
|
||||||
parameters += $" = {defaultValue.ToString().ToLower()}"; // Use lower case for booleans (true/false)
|
|
||||||
}
|
|
||||||
else if (defaultValue is char)
|
|
||||||
{
|
|
||||||
parameters += $" = '{defaultValue}'"; // Use single quotes for char
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
parameters += $" = {defaultValue}"; // General case for other types (numbers, enums, etc.)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
var returnStatement = "";
|
|
||||||
|
|
||||||
if (method.ReturnType is INamedTypeSymbol namedType && namedType.IsGenericType && namedType.ConstructedFrom.Name == "Task" && namedType.ContainingNamespace.ToDisplayString() == "System.Threading.Tasks")
|
|
||||||
{
|
|
||||||
returnStatement = "return await";
|
|
||||||
}
|
|
||||||
else if (method.ReturnType.Name == "Task" && method.ReturnType.ContainingNamespace.ToDisplayString() == "System.Threading.Tasks")
|
|
||||||
{
|
|
||||||
returnStatement = "await";
|
|
||||||
}
|
|
||||||
else if (method.ReturnsVoid)
|
|
||||||
{
|
|
||||||
returnStatement = "";
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
returnStatement = "return ";
|
|
||||||
}
|
|
||||||
|
|
||||||
String tmp = GenerateMethod(method, parameters, subCallParameters, returnStatement);
|
|
||||||
|
|
||||||
sb.Append(tmp);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//The generated source
|
|
||||||
var sourceCode = $$"""
|
|
||||||
using System;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using TelegramBotBase.Interfaces;
|
|
||||||
using TelegramBotBase.Sessions;
|
|
||||||
using Telegram.Bot;
|
|
||||||
using Telegram.Bot.Extensions;
|
|
||||||
using Telegram.Bot.Requests;
|
|
||||||
using Telegram.Bot.Types.Enums;
|
|
||||||
using Telegram.Bot.Types.InlineQueryResults;
|
|
||||||
using Telegram.Bot.Types.Payments;
|
|
||||||
using Telegram.Bot.Types.ReplyMarkups;
|
|
||||||
using File = Telegram.Bot.Types.File;
|
|
||||||
|
|
||||||
#nullable enable
|
|
||||||
namespace TelegramBotBase;
|
|
||||||
|
|
||||||
public static class DeviceExtensions
|
|
||||||
{
|
|
||||||
{{sb.ToString()}}
|
|
||||||
}
|
|
||||||
|
|
||||||
""";
|
|
||||||
|
|
||||||
//Cleanup
|
|
||||||
sourceCode = sourceCode.Replace("System.Threading.Tasks.", "");
|
|
||||||
|
|
||||||
|
|
||||||
context.AddSource("DeviceExtensions.g.cs", SourceText.From(sourceCode, Encoding.UTF8));
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Test
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="method"></param>
|
|
||||||
/// <param name="parameters"></param>
|
|
||||||
/// <param name="subCallParameters"></param>
|
|
||||||
/// <param name="returnStatement"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
private String GenerateMethod(IMethodSymbol? method, string parameters, string subCallParameters, string returnStatement)
|
|
||||||
{
|
|
||||||
//Adding xml comments from embedded xml file (Workaround)
|
|
||||||
String xml_comments = xml?.GetDocumentationLinesForSymbol(method);
|
|
||||||
|
|
||||||
StringBuilder sb = new StringBuilder();
|
|
||||||
|
|
||||||
sb.AppendLine(xml_comments);
|
|
||||||
|
|
||||||
sb.AppendLine($" public static async {method.ReturnType.ToDisplayString()} {method.Name}(this IDeviceSession device, {parameters})");
|
|
||||||
|
|
||||||
sb.AppendLine($" {{");
|
|
||||||
|
|
||||||
sb.AppendLine($" {returnStatement} device.Client.TelegramClient.{method.Name}({subCallParameters});");
|
|
||||||
|
|
||||||
sb.AppendLine($" }}");
|
|
||||||
|
|
||||||
sb.AppendLine();
|
|
||||||
|
|
||||||
sb.AppendLine();
|
|
||||||
|
|
||||||
return sb.ToString();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,89 +0,0 @@
|
|||||||
using Microsoft.CodeAnalysis;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Reflection;
|
|
||||||
using System.Text;
|
|
||||||
using System.Xml.Linq;
|
|
||||||
using System.Xml.XPath;
|
|
||||||
|
|
||||||
namespace TelegramBotBase.SourceGenerators
|
|
||||||
{
|
|
||||||
public class XmlDocumentationLoader
|
|
||||||
{
|
|
||||||
XDocument xDocument;
|
|
||||||
|
|
||||||
public string GetDocumentationLinesForSymbol(ISymbol symbol)
|
|
||||||
{
|
|
||||||
var docElement = xDocument?.Descendants("member")
|
|
||||||
.FirstOrDefault(e => e.Attribute("name")?.Value == GetDocumentationCommentId(symbol));
|
|
||||||
|
|
||||||
|
|
||||||
StringBuilder sb = new StringBuilder();
|
|
||||||
|
|
||||||
XNode first = docElement.FirstNode;
|
|
||||||
do
|
|
||||||
{
|
|
||||||
sb.AppendLine(first.ToString());
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
first = first.NextNode;
|
|
||||||
|
|
||||||
}
|
|
||||||
while (first.NextNode != null);
|
|
||||||
|
|
||||||
var lines = sb.ToString().Split('\n');
|
|
||||||
|
|
||||||
sb = new StringBuilder();
|
|
||||||
|
|
||||||
foreach (var line in lines)
|
|
||||||
{
|
|
||||||
if (line == "")
|
|
||||||
continue;
|
|
||||||
|
|
||||||
sb.AppendLine($" /// {line.Trim()}");
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
return sb.ToString().Trim();
|
|
||||||
}
|
|
||||||
|
|
||||||
private string GetDocumentationCommentId(ISymbol symbol)
|
|
||||||
{
|
|
||||||
// Returns the documentation comment ID for a symbol
|
|
||||||
return symbol.GetDocumentationCommentId();
|
|
||||||
}
|
|
||||||
|
|
||||||
public XDocument ReadEmbeddedXml(string resourceName)
|
|
||||||
{
|
|
||||||
// Get the assembly where the resource is embedded
|
|
||||||
Assembly assembly = Assembly.GetExecutingAssembly();
|
|
||||||
|
|
||||||
// Construct the full resource name
|
|
||||||
string fullResourceName = $"{assembly.GetName().Name}.Resources.{resourceName}";
|
|
||||||
|
|
||||||
var names = assembly.GetManifestResourceNames();
|
|
||||||
|
|
||||||
if (!names.Contains(fullResourceName))
|
|
||||||
return null;
|
|
||||||
|
|
||||||
// Open a stream to the embedded resource
|
|
||||||
using (Stream stream = assembly.GetManifestResourceStream(fullResourceName))
|
|
||||||
{
|
|
||||||
if (stream == null)
|
|
||||||
{
|
|
||||||
//throw new FileNotFoundException("Resource not found", fullResourceName);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
xDocument = XDocument.Load(stream);
|
|
||||||
// Load the stream into an XDocument
|
|
||||||
return xDocument;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -8,11 +8,8 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="TelegramBotBase" Version="6.4.0-preview.1" />
|
||||||
<PackageReference Include="TelegramBotBase.Extensions.Images" Version="1.1.2" />
|
<PackageReference Include="TelegramBotBase.Extensions.Images" Version="1.1.2" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<ProjectReference Include="..\TelegramBotBase\TelegramBotBase.csproj" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ using System.Collections.Generic;
|
|||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text.Json;
|
using Newtonsoft.Json;
|
||||||
using TelegramBotBase.Controls.Hybrid;
|
using TelegramBotBase.Controls.Hybrid;
|
||||||
using TelegramBotBase.DataSources;
|
using TelegramBotBase.DataSources;
|
||||||
using TelegramBotBase.Form;
|
using TelegramBotBase.Form;
|
||||||
@@ -36,7 +36,7 @@ public class CustomDataSource : ButtonFormDataSource
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var list = JsonSerializer.Deserialize<List<string>>(File.ReadAllText("countries.json"));
|
var list = JsonConvert.DeserializeObject<List<string>>(File.ReadAllText("countries.json"));
|
||||||
|
|
||||||
|
|
||||||
Countries = list;
|
Countries = list;
|
||||||
@@ -56,7 +56,7 @@ public class CustomDataSource : ButtonFormDataSource
|
|||||||
|
|
||||||
Countries = countries;
|
Countries = countries;
|
||||||
|
|
||||||
var tmp = JsonSerializer.Serialize(countries);
|
var tmp = JsonConvert.SerializeObject(countries);
|
||||||
|
|
||||||
File.WriteAllText(AppContext.BaseDirectory + "countries.json", tmp);
|
File.WriteAllText(AppContext.BaseDirectory + "countries.json", tmp);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using Telegram.Bot.Types;
|
using Telegram.Bot.Types;
|
||||||
using TelegramBotBase.Interfaces;
|
using TelegramBotBase.Sessions;
|
||||||
|
|
||||||
namespace TelegramBotBase.Args;
|
namespace TelegramBotBase.Args;
|
||||||
|
|
||||||
@@ -15,7 +15,7 @@ public class BotCommandEventArgs : EventArgs
|
|||||||
}
|
}
|
||||||
|
|
||||||
public BotCommandEventArgs(string command, List<string> parameters, Message message, long deviceId,
|
public BotCommandEventArgs(string command, List<string> parameters, Message message, long deviceId,
|
||||||
IDeviceSession device)
|
DeviceSession device)
|
||||||
{
|
{
|
||||||
Command = command;
|
Command = command;
|
||||||
Parameters = parameters;
|
Parameters = parameters;
|
||||||
@@ -30,7 +30,7 @@ public class BotCommandEventArgs : EventArgs
|
|||||||
|
|
||||||
public long DeviceId { get; set; }
|
public long DeviceId { get; set; }
|
||||||
|
|
||||||
public IDeviceSession Device { get; set; }
|
public DeviceSession Device { get; set; }
|
||||||
|
|
||||||
public bool Handled { get; set; } = false;
|
public bool Handled { get; set; } = false;
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
using System;
|
using System;
|
||||||
using TelegramBotBase.Interfaces;
|
using TelegramBotBase.Sessions;
|
||||||
|
|
||||||
namespace TelegramBotBase.Base;
|
namespace TelegramBotBase.Base;
|
||||||
|
|
||||||
public class MessageIncomeEventArgs : EventArgs
|
public class MessageIncomeEventArgs : EventArgs
|
||||||
{
|
{
|
||||||
public MessageIncomeEventArgs(long deviceId, IDeviceSession device, MessageResult message)
|
public MessageIncomeEventArgs(long deviceId, DeviceSession device, MessageResult message)
|
||||||
{
|
{
|
||||||
DeviceId = deviceId;
|
DeviceId = deviceId;
|
||||||
Device = device;
|
Device = device;
|
||||||
@@ -14,7 +14,7 @@ public class MessageIncomeEventArgs : EventArgs
|
|||||||
|
|
||||||
public long DeviceId { get; set; }
|
public long DeviceId { get; set; }
|
||||||
|
|
||||||
public IDeviceSession Device { get; set; }
|
public DeviceSession Device { get; set; }
|
||||||
|
|
||||||
public MessageResult Message { get; set; }
|
public MessageResult Message { get; set; }
|
||||||
}
|
}
|
||||||
@@ -1,11 +1,11 @@
|
|||||||
using System;
|
using System;
|
||||||
using TelegramBotBase.Interfaces;
|
using TelegramBotBase.Sessions;
|
||||||
|
|
||||||
namespace TelegramBotBase.Base;
|
namespace TelegramBotBase.Base;
|
||||||
|
|
||||||
public class SessionBeginEventArgs : EventArgs
|
public class SessionBeginEventArgs : EventArgs
|
||||||
{
|
{
|
||||||
public SessionBeginEventArgs(long deviceId, IDeviceSession device)
|
public SessionBeginEventArgs(long deviceId, DeviceSession device)
|
||||||
{
|
{
|
||||||
DeviceId = deviceId;
|
DeviceId = deviceId;
|
||||||
Device = device;
|
Device = device;
|
||||||
@@ -13,5 +13,5 @@ public class SessionBeginEventArgs : EventArgs
|
|||||||
|
|
||||||
public long DeviceId { get; set; }
|
public long DeviceId { get; set; }
|
||||||
|
|
||||||
public IDeviceSession Device { get; set; }
|
public DeviceSession Device { get; set; }
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
using TelegramBotBase.Interfaces;
|
using TelegramBotBase.Sessions;
|
||||||
|
|
||||||
namespace TelegramBotBase.Args;
|
namespace TelegramBotBase.Args;
|
||||||
|
|
||||||
@@ -9,7 +9,7 @@ public class SystemExceptionEventArgs : EventArgs
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
public SystemExceptionEventArgs(string command, long deviceId, IDeviceSession device, Exception error)
|
public SystemExceptionEventArgs(string command, long deviceId, DeviceSession device, Exception error)
|
||||||
{
|
{
|
||||||
Command = command;
|
Command = command;
|
||||||
DeviceId = deviceId;
|
DeviceId = deviceId;
|
||||||
@@ -21,7 +21,7 @@ public class SystemExceptionEventArgs : EventArgs
|
|||||||
|
|
||||||
public long DeviceId { get; set; }
|
public long DeviceId { get; set; }
|
||||||
|
|
||||||
public IDeviceSession Device { get; set; }
|
public DeviceSession Device { get; set; }
|
||||||
|
|
||||||
public Exception Error { get; set; }
|
public Exception Error { get; set; }
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using Telegram.Bot.Types;
|
using Telegram.Bot.Types;
|
||||||
using TelegramBotBase.Interfaces;
|
using TelegramBotBase.Sessions;
|
||||||
|
|
||||||
namespace TelegramBotBase.Args;
|
namespace TelegramBotBase.Args;
|
||||||
|
|
||||||
@@ -12,7 +12,7 @@ public class UnhandledCallEventArgs : EventArgs
|
|||||||
}
|
}
|
||||||
|
|
||||||
public UnhandledCallEventArgs(string command, string rawData, long deviceId, int messageId, Message message,
|
public UnhandledCallEventArgs(string command, string rawData, long deviceId, int messageId, Message message,
|
||||||
IDeviceSession device) : this()
|
DeviceSession device) : this()
|
||||||
{
|
{
|
||||||
Command = command;
|
Command = command;
|
||||||
RawData = rawData;
|
RawData = rawData;
|
||||||
@@ -26,7 +26,7 @@ public class UnhandledCallEventArgs : EventArgs
|
|||||||
|
|
||||||
public long DeviceId { get; set; }
|
public long DeviceId { get; set; }
|
||||||
|
|
||||||
public IDeviceSession Device { get; set; }
|
public DeviceSession Device { get; set; }
|
||||||
|
|
||||||
public string RawData { get; set; }
|
public string RawData { get; set; }
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using TelegramBotBase.Interfaces;
|
using TelegramBotBase.Sessions;
|
||||||
|
|
||||||
namespace TelegramBotBase.Base;
|
namespace TelegramBotBase.Base;
|
||||||
|
|
||||||
@@ -8,7 +8,7 @@ namespace TelegramBotBase.Base;
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class ControlBase
|
public class ControlBase
|
||||||
{
|
{
|
||||||
public IDeviceSession Device { get; set; }
|
public DeviceSession Device { get; set; }
|
||||||
|
|
||||||
public int Id { get; set; }
|
public int Id { get; set; }
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ using System.Threading.Tasks;
|
|||||||
using TelegramBotBase.Args;
|
using TelegramBotBase.Args;
|
||||||
using TelegramBotBase.Base;
|
using TelegramBotBase.Base;
|
||||||
using TelegramBotBase.Form.Navigation;
|
using TelegramBotBase.Form.Navigation;
|
||||||
using TelegramBotBase.Interfaces;
|
using TelegramBotBase.Sessions;
|
||||||
using static TelegramBotBase.Base.Async;
|
using static TelegramBotBase.Base.Async;
|
||||||
|
|
||||||
namespace TelegramBotBase.Form;
|
namespace TelegramBotBase.Form;
|
||||||
@@ -28,7 +28,7 @@ public class FormBase : IDisposable
|
|||||||
|
|
||||||
public NavigationController NavigationController { get; set; }
|
public NavigationController NavigationController { get; set; }
|
||||||
|
|
||||||
public IDeviceSession Device { get; set; }
|
public DeviceSession Device { get; set; }
|
||||||
|
|
||||||
public MessageClient Client { get; set; }
|
public MessageClient Client { get; set; }
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ using System.Collections.Generic;
|
|||||||
using System.ComponentModel;
|
using System.ComponentModel;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Runtime.InteropServices;
|
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Telegram.Bot;
|
using Telegram.Bot;
|
||||||
@@ -11,7 +10,6 @@ using Telegram.Bot.Exceptions;
|
|||||||
using Telegram.Bot.Polling;
|
using Telegram.Bot.Polling;
|
||||||
using Telegram.Bot.Types;
|
using Telegram.Bot.Types;
|
||||||
|
|
||||||
|
|
||||||
namespace TelegramBotBase.Base;
|
namespace TelegramBotBase.Base;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -19,18 +17,22 @@ namespace TelegramBotBase.Base;
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class MessageClient
|
public class MessageClient
|
||||||
{
|
{
|
||||||
private EventHandlerList Events { get; } = new();
|
|
||||||
|
|
||||||
private static readonly object EvOnMessageLoop = new();
|
private static readonly object EvOnMessageLoop = new();
|
||||||
private static readonly object EvOnReceiveError = new();
|
private static readonly object EvOnReceiveError = new();
|
||||||
|
|
||||||
protected CancellationTokenSource _cancellationTokenSource;
|
private static object __evOnMessage = new();
|
||||||
|
|
||||||
|
private static object __evOnMessageEdit = new();
|
||||||
|
|
||||||
|
private static object __evCallbackQuery = new();
|
||||||
|
|
||||||
|
private CancellationTokenSource _cancellationTokenSource;
|
||||||
|
|
||||||
public string ApiKey { get; }
|
public string ApiKey { get; }
|
||||||
|
|
||||||
public ITelegramBotClient TelegramClient { get; set; }
|
public ITelegramBotClient TelegramClient { get; set; }
|
||||||
|
|
||||||
|
private EventHandlerList Events { get; } = new();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Indicates if all pending Telegram.Bot.Types.Updates should be thrown out before
|
/// Indicates if all pending Telegram.Bot.Types.Updates should be thrown out before
|
||||||
@@ -40,17 +42,27 @@ public class MessageClient
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public bool ThrowPendingUpdates { get; set; }
|
public bool ThrowPendingUpdates { get; set; }
|
||||||
|
|
||||||
|
public bool UseThreadPool { get; set; } = false;
|
||||||
|
|
||||||
|
public int ThreadPool_WorkerThreads { get; set; } = 1;
|
||||||
|
|
||||||
|
public int ThreadPool_IOThreads { get; set; } = 1;
|
||||||
|
|
||||||
|
|
||||||
public MessageClient(string apiKey)
|
public MessageClient(string apiKey)
|
||||||
{
|
{
|
||||||
ApiKey = apiKey;
|
ApiKey = apiKey;
|
||||||
TelegramClient = new TelegramBotClient(apiKey);
|
TelegramClient = new TelegramBotClient(apiKey);
|
||||||
|
|
||||||
|
Prepare();
|
||||||
}
|
}
|
||||||
|
|
||||||
public MessageClient(string apiKey, HttpClient proxy)
|
public MessageClient(string apiKey, HttpClient proxy)
|
||||||
{
|
{
|
||||||
ApiKey = apiKey;
|
ApiKey = apiKey;
|
||||||
TelegramClient = new TelegramBotClient(apiKey, proxy);
|
TelegramClient = new TelegramBotClient(apiKey, proxy);
|
||||||
|
|
||||||
|
Prepare();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -68,6 +80,8 @@ public class MessageClient
|
|||||||
);
|
);
|
||||||
|
|
||||||
TelegramClient = new TelegramBotClient(apiKey, httpClient);
|
TelegramClient = new TelegramBotClient(apiKey, httpClient);
|
||||||
|
|
||||||
|
Prepare();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -87,6 +101,8 @@ public class MessageClient
|
|||||||
);
|
);
|
||||||
|
|
||||||
TelegramClient = new TelegramBotClient(apiKey, httpClient);
|
TelegramClient = new TelegramBotClient(apiKey, httpClient);
|
||||||
|
|
||||||
|
Prepare();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -94,10 +110,20 @@ public class MessageClient
|
|||||||
{
|
{
|
||||||
ApiKey = apiKey;
|
ApiKey = apiKey;
|
||||||
TelegramClient = client;
|
TelegramClient = client;
|
||||||
|
|
||||||
|
Prepare();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public virtual void StartReceiving()
|
|
||||||
|
|
||||||
|
public void Prepare()
|
||||||
|
{
|
||||||
|
TelegramClient.Timeout = new TimeSpan(0, 0, 30);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void StartReceiving()
|
||||||
{
|
{
|
||||||
_cancellationTokenSource = new CancellationTokenSource();
|
_cancellationTokenSource = new CancellationTokenSource();
|
||||||
|
|
||||||
@@ -105,30 +131,66 @@ public class MessageClient
|
|||||||
|
|
||||||
receiverOptions.ThrowPendingUpdates = ThrowPendingUpdates;
|
receiverOptions.ThrowPendingUpdates = ThrowPendingUpdates;
|
||||||
|
|
||||||
TelegramClient.StartReceiving(HandleUpdateAsync, HandleErrorAsync, receiverOptions, _cancellationTokenSource.Token);
|
if (UseThreadPool)
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public virtual void StopReceiving()
|
|
||||||
{
|
{
|
||||||
_cancellationTokenSource?.Cancel();
|
ThreadPool.SetMaxThreads(ThreadPool_WorkerThreads, ThreadPool_IOThreads);
|
||||||
|
|
||||||
|
TelegramClient.StartReceiving(HandleUpdateAsyncThreadPool, HandleErrorAsyncThreadPool, receiverOptions,
|
||||||
|
_cancellationTokenSource.Token);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
TelegramClient.StartReceiving(HandleUpdateAsync, HandleErrorAsync, receiverOptions,
|
||||||
|
_cancellationTokenSource.Token);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
private async Task HandleUpdateAsync(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken)
|
public void StopReceiving()
|
||||||
|
{
|
||||||
|
_cancellationTokenSource.Cancel();
|
||||||
|
}
|
||||||
|
|
||||||
|
#region "Single Thread"
|
||||||
|
|
||||||
|
public async Task HandleUpdateAsync(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
await OnMessageLoop(new UpdateResult(update, null));
|
await OnMessageLoop(new UpdateResult(update, null));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task HandleErrorAsync(ITelegramBotClient botClient, Exception exception,
|
||||||
private async Task HandleErrorAsync(ITelegramBotClient botClient, Exception exception,
|
|
||||||
CancellationToken cancellationToken)
|
CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
await OnReceiveError(new ErrorResult(exception));
|
await OnReceiveError(new ErrorResult(exception));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region "Thread Pool"
|
||||||
|
|
||||||
|
public Task HandleUpdateAsyncThreadPool(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
ThreadPool.QueueUserWorkItem(async a =>
|
||||||
|
{
|
||||||
|
await OnMessageLoop(new UpdateResult(update, null));
|
||||||
|
});
|
||||||
|
|
||||||
|
return Task.CompletedTask;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task HandleErrorAsyncThreadPool(ITelegramBotClient botClient, Exception exception,
|
||||||
|
CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
ThreadPool.QueueUserWorkItem(async a =>
|
||||||
|
{
|
||||||
|
await OnReceiveError(new ErrorResult(exception));
|
||||||
|
});
|
||||||
|
|
||||||
|
return Task.CompletedTask;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
#region "BotCommands"
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// This will return the current list of bot commands.
|
/// This will return the current list of bot commands.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -160,8 +222,6 @@ public class MessageClient
|
|||||||
await TelegramClient.DeleteMyCommandsAsync(scope, languageCode);
|
await TelegramClient.DeleteMyCommandsAsync(scope, languageCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
|
|
||||||
#region "Events"
|
#region "Events"
|
||||||
|
|
||||||
@@ -171,7 +231,6 @@ public class MessageClient
|
|||||||
remove => Events.RemoveHandler(EvOnMessageLoop, value);
|
remove => Events.RemoveHandler(EvOnMessageLoop, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public async Task OnMessageLoop(UpdateResult update)
|
public async Task OnMessageLoop(UpdateResult update)
|
||||||
{
|
{
|
||||||
var eventHandlers = (Events[EvOnMessageLoop] as Async.AsyncEventHandler<UpdateResult>)?.Invoke(this, update);
|
var eventHandlers = (Events[EvOnMessageLoop] as Async.AsyncEventHandler<UpdateResult>)?.Invoke(this, update);
|
||||||
@@ -189,7 +248,6 @@ public class MessageClient
|
|||||||
remove => Events.RemoveHandler(EvOnReceiveError, value);
|
remove => Events.RemoveHandler(EvOnReceiveError, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public async Task OnReceiveError(ErrorResult update)
|
public async Task OnReceiveError(ErrorResult update)
|
||||||
{
|
{
|
||||||
var eventHandlers = (Events[EvOnReceiveError] as Async.AsyncEventHandler<ErrorResult>)?.Invoke(this, update);
|
var eventHandlers = (Events[EvOnReceiveError] as Async.AsyncEventHandler<ErrorResult>)?.Invoke(this, update);
|
||||||
@@ -213,5 +271,4 @@ public class MessageClient
|
|||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
using System;
|
using System.Collections.Generic;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text.Json;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using Newtonsoft.Json;
|
||||||
using Telegram.Bot.Types;
|
using Telegram.Bot.Types;
|
||||||
using Telegram.Bot.Types.Enums;
|
using Telegram.Bot.Types.Enums;
|
||||||
|
|
||||||
@@ -10,36 +9,16 @@ namespace TelegramBotBase.Base;
|
|||||||
|
|
||||||
public class MessageResult : ResultBase
|
public class MessageResult : ResultBase
|
||||||
{
|
{
|
||||||
|
internal MessageResult()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
public MessageResult(Update update)
|
public MessageResult(Update update)
|
||||||
{
|
{
|
||||||
UpdateData = update;
|
UpdateData = update;
|
||||||
|
|
||||||
init();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void init()
|
public Update UpdateData { get; set; }
|
||||||
{
|
|
||||||
IsAction = UpdateData.CallbackQuery != null;
|
|
||||||
|
|
||||||
if (Message == null)
|
|
||||||
return;
|
|
||||||
|
|
||||||
IsBotCommand = Message.Entities?.Any(a => a.Type == MessageEntityType.BotCommand) ?? false;
|
|
||||||
|
|
||||||
if (!IsBotCommand)
|
|
||||||
return;
|
|
||||||
|
|
||||||
BotCommand = MessageText.Split(' ')[0];
|
|
||||||
|
|
||||||
IsBotGroupCommand = BotCommand.Contains("@");
|
|
||||||
|
|
||||||
if (IsBotGroupCommand)
|
|
||||||
{
|
|
||||||
BotCommand = BotCommand.Substring(0, BotCommand.LastIndexOf('@'));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public Update UpdateData { get; private set; }
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns the Device/ChatId
|
/// Returns the Device/ChatId
|
||||||
@@ -76,17 +55,12 @@ public class MessageResult : ResultBase
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Is this an action ? (i.e. button click)
|
/// Is this an action ? (i.e. button click)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool IsAction { get; private set; }
|
public bool IsAction => UpdateData.CallbackQuery != null;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Is this a command ? Starts with a slash '/' and a command
|
/// Is this a command ? Starts with a slash '/' and a command
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool IsBotCommand { get; private set; }
|
public bool IsBotCommand => MessageText.StartsWith("/");
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Is this a bot command sent from a group via @BotId ?
|
|
||||||
/// </summary>
|
|
||||||
public bool IsBotGroupCommand { get; private set; }
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns a List of all parameters which has been sent with the command itself (i.e. /start 123 456 789 =>
|
/// Returns a List of all parameters which has been sent with the command itself (i.e. /start 123 456 789 =>
|
||||||
@@ -109,7 +83,18 @@ public class MessageResult : ResultBase
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns just the command (i.e. /start 1 2 3 => /start)
|
/// Returns just the command (i.e. /start 1 2 3 => /start)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string BotCommand { get; private set; }
|
public string BotCommand
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
if (!IsBotCommand)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return MessageText.Split(' ')[0];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns if this message will be used on the first form or not.
|
/// Returns if this message will be used on the first form or not.
|
||||||
@@ -126,7 +111,7 @@ public class MessageResult : ResultBase
|
|||||||
T cd = null;
|
T cd = null;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
cd = JsonSerializer.Deserialize<T>(RawData);
|
cd = JsonConvert.DeserializeObject<T>(RawData);
|
||||||
|
|
||||||
return cd;
|
return cd;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,13 +2,13 @@
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Telegram.Bot;
|
using Telegram.Bot;
|
||||||
using Telegram.Bot.Types;
|
using Telegram.Bot.Types;
|
||||||
using TelegramBotBase.Interfaces;
|
using TelegramBotBase.Sessions;
|
||||||
|
|
||||||
namespace TelegramBotBase.Base;
|
namespace TelegramBotBase.Base;
|
||||||
|
|
||||||
public class ResultBase : EventArgs
|
public class ResultBase : EventArgs
|
||||||
{
|
{
|
||||||
public IDeviceSession Device { get; set; }
|
public DeviceSession Device { get; set; }
|
||||||
|
|
||||||
public virtual long DeviceId { get; set; }
|
public virtual long DeviceId { get; set; }
|
||||||
|
|
||||||
|
|||||||
@@ -1,109 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.ComponentModel;
|
|
||||||
using System.Net;
|
|
||||||
using System.Net.Http;
|
|
||||||
using System.Threading;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Telegram.Bot;
|
|
||||||
using Telegram.Bot.Exceptions;
|
|
||||||
using Telegram.Bot.Polling;
|
|
||||||
using Telegram.Bot.Types;
|
|
||||||
using TelegramBotBase.Interfaces;
|
|
||||||
|
|
||||||
namespace TelegramBotBase.Base;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Base class for message handling
|
|
||||||
/// </summary>
|
|
||||||
public class ThreadPoolMessageClient : MessageClient
|
|
||||||
{
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Indicates if all pending Telegram.Bot.Types.Updates should be thrown out before
|
|
||||||
// start polling. If set to true Telegram.Bot.Polling.ReceiverOptions.AllowedUpdates
|
|
||||||
// should be set to not null, otherwise Telegram.Bot.Polling.ReceiverOptions.AllowedUpdates
|
|
||||||
// will effectively be set to receive all Telegram.Bot.Types.Updates.
|
|
||||||
/// </summary>
|
|
||||||
|
|
||||||
public int ThreadPool_WorkerThreads { get; set; } = 1;
|
|
||||||
|
|
||||||
public int ThreadPool_IOThreads { get; set; } = 1;
|
|
||||||
|
|
||||||
|
|
||||||
public ThreadPoolMessageClient(string apiKey) : base(apiKey)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public ThreadPoolMessageClient(string apiKey, HttpClient proxy) : base(apiKey, proxy)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public ThreadPoolMessageClient(string apiKey, Uri proxyUrl, NetworkCredential credential = null) : base(apiKey, proxyUrl, credential)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Initializes the client with a proxy
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="apiKey"></param>
|
|
||||||
/// <param name="proxyHost">i.e. 127.0.0.1</param>
|
|
||||||
/// <param name="proxyPort">i.e. 10000</param>
|
|
||||||
public ThreadPoolMessageClient(string apiKey, string proxyHost, int proxyPort) : base(apiKey, proxyHost, proxyPort)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public ThreadPoolMessageClient(string apiKey, TelegramBotClient client) : base(apiKey, client)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public override void StartReceiving()
|
|
||||||
{
|
|
||||||
_cancellationTokenSource = new CancellationTokenSource();
|
|
||||||
|
|
||||||
var receiverOptions = new ReceiverOptions();
|
|
||||||
|
|
||||||
receiverOptions.ThrowPendingUpdates = ThrowPendingUpdates;
|
|
||||||
|
|
||||||
ThreadPool.SetMaxThreads(ThreadPool_WorkerThreads, ThreadPool_IOThreads);
|
|
||||||
|
|
||||||
TelegramClient.StartReceiving(HandleUpdateAsyncThreadPool, HandleErrorAsyncThreadPool, receiverOptions, _cancellationTokenSource.Token);
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void StopReceiving()
|
|
||||||
{
|
|
||||||
_cancellationTokenSource?.Cancel();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public Task HandleUpdateAsyncThreadPool(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken)
|
|
||||||
{
|
|
||||||
ThreadPool.QueueUserWorkItem(async a =>
|
|
||||||
{
|
|
||||||
await OnMessageLoop(new UpdateResult(update, null));
|
|
||||||
});
|
|
||||||
|
|
||||||
return Task.CompletedTask;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Task HandleErrorAsyncThreadPool(ITelegramBotClient botClient, Exception exception,
|
|
||||||
CancellationToken cancellationToken)
|
|
||||||
{
|
|
||||||
ThreadPool.QueueUserWorkItem(async a =>
|
|
||||||
{
|
|
||||||
await OnReceiveError(new ErrorResult(exception));
|
|
||||||
});
|
|
||||||
|
|
||||||
return Task.CompletedTask;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,11 +1,11 @@
|
|||||||
using Telegram.Bot.Types;
|
using Telegram.Bot.Types;
|
||||||
using TelegramBotBase.Interfaces;
|
using TelegramBotBase.Sessions;
|
||||||
|
|
||||||
namespace TelegramBotBase.Base;
|
namespace TelegramBotBase.Base;
|
||||||
|
|
||||||
public class UpdateResult : ResultBase
|
public class UpdateResult : ResultBase
|
||||||
{
|
{
|
||||||
public UpdateResult(Update rawData, IDeviceSession device)
|
public UpdateResult(Update rawData, DeviceSession device)
|
||||||
{
|
{
|
||||||
RawData = rawData;
|
RawData = rawData;
|
||||||
Device = device;
|
Device = device;
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ using Telegram.Bot.Types;
|
|||||||
using TelegramBotBase.Args;
|
using TelegramBotBase.Args;
|
||||||
using TelegramBotBase.Base;
|
using TelegramBotBase.Base;
|
||||||
using TelegramBotBase.Enums;
|
using TelegramBotBase.Enums;
|
||||||
using TelegramBotBase.Exceptions;
|
|
||||||
using TelegramBotBase.Interfaces;
|
using TelegramBotBase.Interfaces;
|
||||||
using TelegramBotBase.Sessions;
|
using TelegramBotBase.Sessions;
|
||||||
using Console = TelegramBotBase.Tools.Console;
|
using Console = TelegramBotBase.Tools.Console;
|
||||||
@@ -116,7 +115,7 @@ public sealed class BotBase
|
|||||||
if (ds == null)
|
if (ds == null)
|
||||||
{
|
{
|
||||||
ds = await Sessions.StartSession(e.DeviceId);
|
ds = await Sessions.StartSession(e.DeviceId);
|
||||||
ds.LastMessage = e.Message;
|
ds.LastMessage = e.RawData.Message;
|
||||||
|
|
||||||
OnSessionBegins(new SessionBeginEventArgs(e.DeviceId, ds));
|
OnSessionBegins(new SessionBeginEventArgs(e.DeviceId, ds));
|
||||||
}
|
}
|
||||||
@@ -140,13 +139,6 @@ public sealed class BotBase
|
|||||||
mr.IsFirstHandler = false;
|
mr.IsFirstHandler = false;
|
||||||
} while (ds.FormSwitched && i < GetSetting(ESettings.NavigationMaximum, 10));
|
} while (ds.FormSwitched && i < GetSetting(ESettings.NavigationMaximum, 10));
|
||||||
}
|
}
|
||||||
catch (InvalidServiceProviderConfiguration ex)
|
|
||||||
{
|
|
||||||
var ds = Sessions.GetSession(e.DeviceId);
|
|
||||||
OnException(new SystemExceptionEventArgs(e.Message.Text, e.DeviceId, ds, ex));
|
|
||||||
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
var ds = Sessions.GetSession(e.DeviceId);
|
var ds = Sessions.GetSession(e.DeviceId);
|
||||||
@@ -197,10 +189,13 @@ public sealed class BotBase
|
|||||||
/// <param name="deviceId">Contains the device/chat id of the device to update.</param>
|
/// <param name="deviceId">Contains the device/chat id of the device to update.</param>
|
||||||
public async Task InvokeMessageLoop(long deviceId)
|
public async Task InvokeMessageLoop(long deviceId)
|
||||||
{
|
{
|
||||||
var mr = new MessageResult(new Update
|
var mr = new MessageResult
|
||||||
|
{
|
||||||
|
UpdateData = new Update
|
||||||
{
|
{
|
||||||
Message = new Message()
|
Message = new Message()
|
||||||
});
|
}
|
||||||
|
};
|
||||||
|
|
||||||
await InvokeMessageLoop(deviceId, mr);
|
await InvokeMessageLoop(deviceId, mr);
|
||||||
}
|
}
|
||||||
@@ -216,6 +211,7 @@ public sealed class BotBase
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
var ds = Sessions.GetSession(deviceId);
|
var ds = Sessions.GetSession(deviceId);
|
||||||
|
|
||||||
e.Device = ds;
|
e.Device = ds;
|
||||||
|
|
||||||
await MessageLoopFactory.MessageLoop(this, ds, new UpdateResult(e.UpdateData, ds), e);
|
await MessageLoopFactory.MessageLoop(this, ds, new UpdateResult(e.UpdateData, ds), e);
|
||||||
@@ -265,7 +261,7 @@ public sealed class BotBase
|
|||||||
{
|
{
|
||||||
foreach (var scope in BotCommandScopes)
|
foreach (var scope in BotCommandScopes)
|
||||||
{
|
{
|
||||||
if (scope.Value.Any(a => Constants.Telegram.BotCommandIndicator + a.Command == command))
|
if (scope.Value.Any(a => "/" + a.Command == command))
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,7 +32,6 @@ public class BotBaseBuilder : IAPIKeySelectionStage, IMessageLoopSelectionStage,
|
|||||||
|
|
||||||
private BotBaseBuilder()
|
private BotBaseBuilder()
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -73,14 +72,14 @@ public class BotBaseBuilder : IAPIKeySelectionStage, IMessageLoopSelectionStage,
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public IBuildingStage QuickStart(string apiKey, Type startForm, bool throwPendingUpdates = false)
|
public IBuildingStage QuickStart(string apiKey, Type startForm)
|
||||||
{
|
{
|
||||||
_apiKey = apiKey;
|
_apiKey = apiKey;
|
||||||
_factory = new DefaultStartFormFactory(startForm);
|
_factory = new DefaultStartFormFactory(startForm);
|
||||||
|
|
||||||
DefaultMessageLoop();
|
DefaultMessageLoop();
|
||||||
|
|
||||||
NoProxy(throwPendingUpdates);
|
NoProxy();
|
||||||
|
|
||||||
OnlyStart();
|
OnlyStart();
|
||||||
|
|
||||||
@@ -94,7 +93,7 @@ public class BotBaseBuilder : IAPIKeySelectionStage, IMessageLoopSelectionStage,
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public IBuildingStage QuickStart<T>(string apiKey, bool throwPendingUpdates = false)
|
public IBuildingStage QuickStart<T>(string apiKey)
|
||||||
where T : FormBase
|
where T : FormBase
|
||||||
{
|
{
|
||||||
_apiKey = apiKey;
|
_apiKey = apiKey;
|
||||||
@@ -102,7 +101,7 @@ public class BotBaseBuilder : IAPIKeySelectionStage, IMessageLoopSelectionStage,
|
|||||||
|
|
||||||
DefaultMessageLoop();
|
DefaultMessageLoop();
|
||||||
|
|
||||||
NoProxy(throwPendingUpdates);
|
NoProxy();
|
||||||
|
|
||||||
OnlyStart();
|
OnlyStart();
|
||||||
|
|
||||||
@@ -115,14 +114,14 @@ public class BotBaseBuilder : IAPIKeySelectionStage, IMessageLoopSelectionStage,
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IBuildingStage QuickStart(string apiKey, IStartFormFactory startFormFactory, bool throwPendingUpdates = false)
|
public IBuildingStage QuickStart(string apiKey, IStartFormFactory startFormFactory)
|
||||||
{
|
{
|
||||||
_apiKey = apiKey;
|
_apiKey = apiKey;
|
||||||
_factory = startFormFactory;
|
_factory = startFormFactory;
|
||||||
|
|
||||||
DefaultMessageLoop();
|
DefaultMessageLoop();
|
||||||
|
|
||||||
NoProxy(throwPendingUpdates);
|
NoProxy();
|
||||||
|
|
||||||
OnlyStart();
|
OnlyStart();
|
||||||
|
|
||||||
@@ -214,14 +213,14 @@ public class BotBaseBuilder : IAPIKeySelectionStage, IMessageLoopSelectionStage,
|
|||||||
|
|
||||||
#region "Step 4 (Network Settings)"
|
#region "Step 4 (Network Settings)"
|
||||||
|
|
||||||
public IBotCommandsStage WithProxy(string proxyAddress, bool throwPendingUpdates = false, int timeoutInSeconds = 60)
|
public IBotCommandsStage WithProxy(string proxyAddress, bool throwPendingUpdates = false)
|
||||||
{
|
{
|
||||||
var url = new Uri(proxyAddress);
|
var url = new Uri(proxyAddress);
|
||||||
_client = new MessageClient(_apiKey, url)
|
_client = new MessageClient(_apiKey, url)
|
||||||
{
|
{
|
||||||
TelegramClient =
|
TelegramClient =
|
||||||
{
|
{
|
||||||
Timeout = TimeSpan.FromSeconds(timeoutInSeconds)
|
Timeout = new TimeSpan(0, 1, 0)
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
_client.ThrowPendingUpdates = throwPendingUpdates;
|
_client.ThrowPendingUpdates = throwPendingUpdates;
|
||||||
@@ -229,13 +228,13 @@ public class BotBaseBuilder : IAPIKeySelectionStage, IMessageLoopSelectionStage,
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public IBotCommandsStage NoProxy(bool throwPendingUpdates = false, int timeoutInSeconds = 60)
|
public IBotCommandsStage NoProxy(bool throwPendingUpdates = false)
|
||||||
{
|
{
|
||||||
_client = new MessageClient(_apiKey)
|
_client = new MessageClient(_apiKey)
|
||||||
{
|
{
|
||||||
TelegramClient =
|
TelegramClient =
|
||||||
{
|
{
|
||||||
Timeout = TimeSpan.FromSeconds(timeoutInSeconds)// new TimeSpan(0, 1, 0)
|
Timeout = new TimeSpan(0, 1, 0)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
_client.ThrowPendingUpdates = throwPendingUpdates;
|
_client.ThrowPendingUpdates = throwPendingUpdates;
|
||||||
@@ -243,13 +242,13 @@ public class BotBaseBuilder : IAPIKeySelectionStage, IMessageLoopSelectionStage,
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public IBotCommandsStage WithBotClient(TelegramBotClient tgclient, bool throwPendingUpdates = false, int timeoutInSeconds = 60)
|
public IBotCommandsStage WithBotClient(TelegramBotClient tgclient, bool throwPendingUpdates = false)
|
||||||
{
|
{
|
||||||
_client = new MessageClient(_apiKey, tgclient)
|
_client = new MessageClient(_apiKey, tgclient)
|
||||||
{
|
{
|
||||||
TelegramClient =
|
TelegramClient =
|
||||||
{
|
{
|
||||||
Timeout = TimeSpan.FromSeconds(timeoutInSeconds)// new TimeSpan(0, 1, 0)
|
Timeout = new TimeSpan(0, 1, 0)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
_client.ThrowPendingUpdates = throwPendingUpdates;
|
_client.ThrowPendingUpdates = throwPendingUpdates;
|
||||||
@@ -257,26 +256,26 @@ public class BotBaseBuilder : IAPIKeySelectionStage, IMessageLoopSelectionStage,
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public IBotCommandsStage WithHostAndPort(string proxyHost, int proxyPort, bool throwPendingUpdates = false, int timeoutInSeconds = 60)
|
public IBotCommandsStage WithHostAndPort(string proxyHost, int proxyPort, bool throwPendingUpdates = false)
|
||||||
{
|
{
|
||||||
_client = new MessageClient(_apiKey, proxyHost, proxyPort)
|
_client = new MessageClient(_apiKey, proxyHost, proxyPort)
|
||||||
{
|
{
|
||||||
TelegramClient =
|
TelegramClient =
|
||||||
{
|
{
|
||||||
Timeout = TimeSpan.FromSeconds(timeoutInSeconds)// new TimeSpan(0, 1, 0)
|
Timeout = new TimeSpan(0, 1, 0)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
_client.ThrowPendingUpdates = throwPendingUpdates;
|
_client.ThrowPendingUpdates = throwPendingUpdates;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IBotCommandsStage WithHttpClient(HttpClient tgclient, bool throwPendingUpdates = false, int timeoutInSeconds = 60)
|
public IBotCommandsStage WithHttpClient(HttpClient tgclient, bool throwPendingUpdates = false)
|
||||||
{
|
{
|
||||||
_client = new MessageClient(_apiKey, tgclient)
|
_client = new MessageClient(_apiKey, tgclient)
|
||||||
{
|
{
|
||||||
TelegramClient =
|
TelegramClient =
|
||||||
{
|
{
|
||||||
Timeout = TimeSpan.FromSeconds(timeoutInSeconds)// new TimeSpan(0, 1, 0)
|
Timeout = new TimeSpan(0, 1, 0)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
_client.ThrowPendingUpdates = throwPendingUpdates;
|
_client.ThrowPendingUpdates = throwPendingUpdates;
|
||||||
@@ -398,41 +397,29 @@ public class BotBaseBuilder : IAPIKeySelectionStage, IMessageLoopSelectionStage,
|
|||||||
|
|
||||||
#region "Step 7 (Language)"
|
#region "Step 7 (Language)"
|
||||||
|
|
||||||
/// <inheritdoc cref="ILanguageSelectionStage.DefaultLanguage"/>
|
|
||||||
public IThreadingStage DefaultLanguage()
|
public IThreadingStage DefaultLanguage()
|
||||||
{
|
{
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc cref="ILanguageSelectionStage.UseEnglish"/>
|
|
||||||
public IThreadingStage UseEnglish()
|
public IThreadingStage UseEnglish()
|
||||||
{
|
{
|
||||||
Default.Language = new English();
|
Default.Language = new English();
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc cref="ILanguageSelectionStage.UseGerman"/>
|
|
||||||
public IThreadingStage UseGerman()
|
public IThreadingStage UseGerman()
|
||||||
{
|
{
|
||||||
Default.Language = new German();
|
Default.Language = new German();
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc cref="ILanguageSelectionStage.UsePersian"/>
|
|
||||||
public IThreadingStage UsePersian()
|
public IThreadingStage UsePersian()
|
||||||
{
|
{
|
||||||
Default.Language = new Persian();
|
Default.Language = new Persian();
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc cref="ILanguageSelectionStage.UseRussian"/>
|
|
||||||
public IThreadingStage UseRussian()
|
|
||||||
{
|
|
||||||
Default.Language = new Russian();
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc cref="ILanguageSelectionStage.Custom"/>
|
|
||||||
public IThreadingStage Custom(Localization language)
|
public IThreadingStage Custom(Localization language)
|
||||||
{
|
{
|
||||||
Default.Language = language;
|
Default.Language = language;
|
||||||
@@ -442,27 +429,26 @@ public class BotBaseBuilder : IAPIKeySelectionStage, IMessageLoopSelectionStage,
|
|||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region "Step 8 (Threading)"
|
#region "Step 8 (Threading)"
|
||||||
|
|
||||||
public IBuildingStage UseSingleThread()
|
public IBuildingStage UseSingleThread()
|
||||||
{
|
{
|
||||||
|
_client.UseThreadPool = false;
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IBuildingStage UseThreadPool(int workerThreads = 2, int ioThreads = 1)
|
public IBuildingStage UseThreadPool(int workerThreads = 2, int ioThreads = 1)
|
||||||
{
|
{
|
||||||
var c = new ThreadPoolMessageClient(_apiKey, (TelegramBotClient)_client.TelegramClient);
|
_client.UseThreadPool = true;
|
||||||
|
_client.ThreadPool_WorkerThreads = workerThreads;
|
||||||
c.ThreadPool_WorkerThreads = workerThreads;
|
_client.ThreadPool_IOThreads = ioThreads;
|
||||||
c.ThreadPool_IOThreads = ioThreads;
|
|
||||||
c.ThrowPendingUpdates = _client.ThrowPendingUpdates;
|
|
||||||
|
|
||||||
_client = c;
|
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,18 +20,16 @@ public interface IAPIKeySelectionStage
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="apiKey"></param>
|
/// <param name="apiKey"></param>
|
||||||
/// <param name="StartForm"></param>
|
/// <param name="StartForm"></param>
|
||||||
/// <param name="throwPendingUpdates">Indicates if all pending Telegram.Bot.Types.Updates should be thrown out before start polling.</param>
|
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
IBuildingStage QuickStart(string apiKey, Type StartForm, bool throwPendingUpdates = false);
|
IBuildingStage QuickStart(string apiKey, Type StartForm);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Quick and easy way to create a BotBase instance.
|
/// Quick and easy way to create a BotBase instance.
|
||||||
/// Uses: DefaultMessageLoop, NoProxy, OnlyStart, NoSerialization, DefaultLanguage
|
/// Uses: DefaultMessageLoop, NoProxy, OnlyStart, NoSerialization, DefaultLanguage
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="apiKey"></param>
|
/// <param name="apiKey"></param>
|
||||||
/// <param name="throwPendingUpdates">Indicates if all pending Telegram.Bot.Types.Updates should be thrown out before start polling.</param>
|
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
IBuildingStage QuickStart<T>(string apiKey , bool throwPendingUpdates = false) where T : FormBase;
|
IBuildingStage QuickStart<T>(string apiKey) where T : FormBase;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Quick and easy way to create a BotBase instance.
|
/// Quick and easy way to create a BotBase instance.
|
||||||
@@ -39,7 +37,6 @@ public interface IAPIKeySelectionStage
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="apiKey"></param>
|
/// <param name="apiKey"></param>
|
||||||
/// <param name="StartFormFactory"></param>
|
/// <param name="StartFormFactory"></param>
|
||||||
/// <param name="throwPendingUpdates">Indicates if all pending Telegram.Bot.Types.Updates should be thrown out before start polling.</param>
|
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
IBuildingStage QuickStart(string apiKey, IStartFormFactory StartFormFactory, bool throwPendingUpdates = false);
|
IBuildingStage QuickStart(string apiKey, IStartFormFactory StartFormFactory);
|
||||||
}
|
}
|
||||||
@@ -2,44 +2,35 @@
|
|||||||
|
|
||||||
namespace TelegramBotBase.Builder.Interfaces;
|
namespace TelegramBotBase.Builder.Interfaces;
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Represents the language selection stage in the localization process.
|
|
||||||
/// </summary>
|
|
||||||
public interface ILanguageSelectionStage
|
public interface ILanguageSelectionStage
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Selects the default language for control usage. (English)
|
/// Selects the default language for control usage. (English)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>The next stage in the building process.</returns>
|
/// <returns></returns>
|
||||||
IThreadingStage DefaultLanguage();
|
IThreadingStage DefaultLanguage();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Selects english as the default language for control labels.
|
/// Selects english as the default language for control labels.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>The next stage in the building process.</returns>
|
/// <returns></returns>
|
||||||
IThreadingStage UseEnglish();
|
IThreadingStage UseEnglish();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Selects german as the default language for control labels.
|
/// Selects german as the default language for control labels.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>The next stage in the building process.</returns>
|
/// <returns></returns>
|
||||||
IThreadingStage UseGerman();
|
IThreadingStage UseGerman();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Selects persian as the default language for control labels.
|
/// Selects persian as the default language for control labels.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>The next stage in the building process.</returns>
|
/// <returns></returns>
|
||||||
IThreadingStage UsePersian();
|
IThreadingStage UsePersian();
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Selects russian as the default language for control labels.
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>The next stage in the building process.</returns>
|
|
||||||
IThreadingStage UseRussian();
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Selects a custom language as the default language for control labels.
|
/// Selects a custom language as the default language for control labels.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>The next stage in the building process.</returns>
|
/// <returns></returns>
|
||||||
IThreadingStage Custom(Localization language);
|
IThreadingStage Custom(Localization language);
|
||||||
}
|
}
|
||||||
@@ -11,14 +11,14 @@ public interface INetworkingSelectionStage
|
|||||||
/// <param name="proxyAddress"></param>
|
/// <param name="proxyAddress"></param>
|
||||||
/// <param name="throwPendingUpdates">Indicates if all pending Telegram.Bot.Types.Updates should be thrown out before start polling.</param>
|
/// <param name="throwPendingUpdates">Indicates if all pending Telegram.Bot.Types.Updates should be thrown out before start polling.</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
IBotCommandsStage WithProxy(string proxyAddress, bool throwPendingUpdates = false, int timeoutInSeconds = 60);
|
IBotCommandsStage WithProxy(string proxyAddress, bool throwPendingUpdates = false);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Do not choose a proxy as network configuration.
|
/// Do not choose a proxy as network configuration.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="throwPendingUpdates">Indicates if all pending Telegram.Bot.Types.Updates should be thrown out before start polling.</param>
|
/// <param name="throwPendingUpdates">Indicates if all pending Telegram.Bot.Types.Updates should be thrown out before start polling.</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
IBotCommandsStage NoProxy(bool throwPendingUpdates = false, int timeoutInSeconds = 60);
|
IBotCommandsStage NoProxy(bool throwPendingUpdates = false);
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -27,7 +27,7 @@ public interface INetworkingSelectionStage
|
|||||||
/// <param name="client"></param>
|
/// <param name="client"></param>
|
||||||
/// <param name="throwPendingUpdates">Indicates if all pending Telegram.Bot.Types.Updates should be thrown out before start polling.</param>
|
/// <param name="throwPendingUpdates">Indicates if all pending Telegram.Bot.Types.Updates should be thrown out before start polling.</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
IBotCommandsStage WithBotClient(TelegramBotClient client, bool throwPendingUpdates = false, int timeoutInSeconds = 60);
|
IBotCommandsStage WithBotClient(TelegramBotClient client, bool throwPendingUpdates = false);
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -37,7 +37,7 @@ public interface INetworkingSelectionStage
|
|||||||
/// <param name="Port"></param>
|
/// <param name="Port"></param>
|
||||||
/// <param name="throwPendingUpdates">Indicates if all pending Telegram.Bot.Types.Updates should be thrown out before start polling.</param>
|
/// <param name="throwPendingUpdates">Indicates if all pending Telegram.Bot.Types.Updates should be thrown out before start polling.</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
IBotCommandsStage WithHostAndPort(string proxyHost, int Port, bool throwPendingUpdates = false, int timeoutInSeconds = 60);
|
IBotCommandsStage WithHostAndPort(string proxyHost, int Port, bool throwPendingUpdates = false);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Uses a custom http client.
|
/// Uses a custom http client.
|
||||||
@@ -45,5 +45,5 @@ public interface INetworkingSelectionStage
|
|||||||
/// <param name="client"></param>
|
/// <param name="client"></param>
|
||||||
/// <param name="throwPendingUpdates">Indicates if all pending Telegram.Bot.Types.Updates should be thrown out before start polling.</param>
|
/// <param name="throwPendingUpdates">Indicates if all pending Telegram.Bot.Types.Updates should be thrown out before start polling.</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
IBotCommandsStage WithHttpClient(HttpClient client, bool throwPendingUpdates = false, int timeoutInSeconds = 60);
|
IBotCommandsStage WithHttpClient(HttpClient client, bool throwPendingUpdates = false);
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
using System;
|
using TelegramBotBase.Interfaces;
|
||||||
using TelegramBotBase.Interfaces;
|
|
||||||
|
|
||||||
namespace TelegramBotBase.Builder.Interfaces;
|
namespace TelegramBotBase.Builder.Interfaces;
|
||||||
|
|
||||||
@@ -23,32 +22,15 @@ public interface ISessionSerializationStage
|
|||||||
/// Using the complex version of .Net JSON, which can serialize all objects.
|
/// Using the complex version of .Net JSON, which can serialize all objects.
|
||||||
/// Saves in application directory.
|
/// Saves in application directory.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <remarks>
|
|
||||||
/// <para>
|
|
||||||
/// Has been changed lately to not use Newtonsoft.Json anymore.
|
|
||||||
/// For the legacy version add the nuget package below and use the <seealso cref="UseNewtonsoftJson"></seealso> method.
|
|
||||||
/// </para>
|
|
||||||
/// <seealso href="https://www.nuget.org/packages/TelegramBotBase.Extensions.Serializer.Legacy.NewtonsoftJson/">TelegramBotBase.Extensions.Serializer.Legacy.NewtonsoftJson</seealso>
|
|
||||||
/// </remarks>
|
|
||||||
/// <param name="path"></param>
|
/// <param name="path"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
/// <seealso cref="UseNewtonsoftJson"/>
|
|
||||||
ILanguageSelectionStage UseJSON();
|
ILanguageSelectionStage UseJSON();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Using the complex version of .Net JSON, which can serialize all objects.
|
/// Using the complex version of .Net JSON, which can serialize all objects.
|
||||||
/// Saves in application directory.
|
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <remarks>
|
|
||||||
/// <para>
|
|
||||||
/// Has been changed lately to not use Newtonsoft.Json anymore.
|
|
||||||
/// For the legacy version add the nuget package below and use the <seealso cref="UseNewtonsoftJson"></seealso> method.
|
|
||||||
/// </para>
|
|
||||||
/// <seealso href="https://www.nuget.org/packages/TelegramBotBase.Extensions.Serializer.Legacy.NewtonsoftJson/">TelegramBotBase.Extensions.Serializer.Legacy.NewtonsoftJson</seealso>
|
|
||||||
/// </remarks>
|
|
||||||
/// <param name="path"></param>
|
/// <param name="path"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
/// <seealso cref="UseNewtonsoftJson"/>
|
|
||||||
ILanguageSelectionStage UseJSON(string path);
|
ILanguageSelectionStage UseJSON(string path);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -57,7 +39,6 @@ public interface ISessionSerializationStage
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="path"></param>
|
/// <param name="path"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[Obsolete("Use UseJSON instead.")]
|
|
||||||
ILanguageSelectionStage UseSimpleJSON();
|
ILanguageSelectionStage UseSimpleJSON();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -65,7 +46,6 @@ public interface ISessionSerializationStage
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="path"></param>
|
/// <param name="path"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[Obsolete("Use UseJSON instead.")]
|
|
||||||
ILanguageSelectionStage UseSimpleJSON(string path);
|
ILanguageSelectionStage UseSimpleJSON(string path);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
using System;
|
using System.Collections.Generic;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Telegram.Bot.Types;
|
using Telegram.Bot.Types;
|
||||||
|
|
||||||
@@ -21,16 +20,6 @@ public static class Extensions
|
|||||||
scope = BotCommandScope.Default();
|
scope = BotCommandScope.Default();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (string.IsNullOrEmpty(command))
|
|
||||||
{
|
|
||||||
throw new ArgumentNullException(nameof(command), $"{nameof(command)} parameter can not be null or empty");
|
|
||||||
}
|
|
||||||
|
|
||||||
if(command.StartsWith(Constants.Telegram.BotCommandIndicator))
|
|
||||||
{
|
|
||||||
throw new ArgumentException($"{nameof(command)} parameter does not have to start with a slash, please remove.", $"{nameof(command)}");
|
|
||||||
}
|
|
||||||
|
|
||||||
var item = cmds.FirstOrDefault(a => a.Key.Type == scope.Type);
|
var item = cmds.FirstOrDefault(a => a.Key.Type == scope.Type);
|
||||||
|
|
||||||
if (item.Value != null)
|
if (item.Value != null)
|
||||||
|
|||||||
@@ -21,10 +21,4 @@ public static class Telegram
|
|||||||
/// The maximum length of callback data. Will raise an exception of it exceeds it.
|
/// The maximum length of callback data. Will raise an exception of it exceeds it.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public const int MaxCallBackDataBytes = 64;
|
public const int MaxCallBackDataBytes = 64;
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// The slash constant which indicates a bot command.
|
|
||||||
/// </summary>
|
|
||||||
public const string BotCommandIndicator = "/";
|
|
||||||
}
|
}
|
||||||
@@ -51,24 +51,7 @@ public class ButtonGrid : ControlBase
|
|||||||
DataSource = new ButtonFormDataSource(form);
|
DataSource = new ButtonFormDataSource(form);
|
||||||
}
|
}
|
||||||
|
|
||||||
string m_Title = Default.Language["ButtonGrid_Title"];
|
public string Title { get; set; } = Default.Language["ButtonGrid_Title"];
|
||||||
|
|
||||||
public string Title
|
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
return m_Title;
|
|
||||||
}
|
|
||||||
set
|
|
||||||
{
|
|
||||||
if (string.IsNullOrEmpty(value))
|
|
||||||
{
|
|
||||||
throw new ArgumentNullException($"{nameof(Title)}", $"{nameof(Title)} property must have been a value unequal to null/empty");
|
|
||||||
}
|
|
||||||
|
|
||||||
m_Title = value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public string ConfirmationText { get; set; } = "";
|
public string ConfirmationText { get; set; } = "";
|
||||||
|
|
||||||
@@ -443,6 +426,8 @@ public class ButtonGrid : ControlBase
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await result.ConfirmAction(ConfirmationText ?? "");
|
||||||
|
|
||||||
ButtonRow match = null;
|
ButtonRow match = null;
|
||||||
var index = -1;
|
var index = -1;
|
||||||
|
|
||||||
@@ -477,8 +462,6 @@ public class ButtonGrid : ControlBase
|
|||||||
check:
|
check:
|
||||||
if (match != null)
|
if (match != null)
|
||||||
{
|
{
|
||||||
await result.ConfirmAction(ConfirmationText ?? "");
|
|
||||||
|
|
||||||
await OnButtonClicked(new ButtonClickedEventArgs(match.GetButtonMatch(result.RawData, false), index,
|
await OnButtonClicked(new ButtonClickedEventArgs(match.GetButtonMatch(result.RawData, false), index,
|
||||||
match));
|
match));
|
||||||
|
|
||||||
|
|||||||
@@ -13,7 +13,6 @@ using TelegramBotBase.Enums;
|
|||||||
using TelegramBotBase.Exceptions;
|
using TelegramBotBase.Exceptions;
|
||||||
using TelegramBotBase.Form;
|
using TelegramBotBase.Form;
|
||||||
using TelegramBotBase.Localizations;
|
using TelegramBotBase.Localizations;
|
||||||
using static System.Net.Mime.MediaTypeNames;
|
|
||||||
using static TelegramBotBase.Base.Async;
|
using static TelegramBotBase.Base.Async;
|
||||||
|
|
||||||
namespace TelegramBotBase.Controls.Hybrid;
|
namespace TelegramBotBase.Controls.Hybrid;
|
||||||
@@ -52,24 +51,7 @@ public class CheckedButtonList : ControlBase
|
|||||||
DataSource = new ButtonFormDataSource(form);
|
DataSource = new ButtonFormDataSource(form);
|
||||||
}
|
}
|
||||||
|
|
||||||
string m_Title = Default.Language["ButtonGrid_Title"];
|
public string Title { get; set; } = Default.Language["ButtonGrid_Title"];
|
||||||
|
|
||||||
public string Title
|
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
return m_Title;
|
|
||||||
}
|
|
||||||
set
|
|
||||||
{
|
|
||||||
if (string.IsNullOrEmpty(value))
|
|
||||||
{
|
|
||||||
throw new ArgumentNullException($"{nameof(Title)}", $"{nameof(Title)} property must have been a value unequal to null/empty");
|
|
||||||
}
|
|
||||||
|
|
||||||
m_Title = value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public string ConfirmationText { get; set; } = "";
|
public string ConfirmationText { get; set; } = "";
|
||||||
|
|
||||||
|
|||||||
@@ -29,6 +29,8 @@ public class TaggedButtonGrid : MultiView
|
|||||||
|
|
||||||
public string BackLabel = Default.Language["ButtonGrid_Back"];
|
public string BackLabel = Default.Language["ButtonGrid_Back"];
|
||||||
|
|
||||||
|
public string CheckAllLabel = Default.Language["ButtonGrid_CheckAll"];
|
||||||
|
|
||||||
public string NextPageLabel = Default.Language["ButtonGrid_NextPage"];
|
public string NextPageLabel = Default.Language["ButtonGrid_NextPage"];
|
||||||
|
|
||||||
public string NoItemsLabel = Default.Language["ButtonGrid_NoItems"];
|
public string NoItemsLabel = Default.Language["ButtonGrid_NoItems"];
|
||||||
@@ -37,17 +39,11 @@ public class TaggedButtonGrid : MultiView
|
|||||||
|
|
||||||
public string SearchLabel = Default.Language["ButtonGrid_SearchFeature"];
|
public string SearchLabel = Default.Language["ButtonGrid_SearchFeature"];
|
||||||
|
|
||||||
public string TotalTagsLabel = Default.Language["TaggedButtonGrid_TotalTags"];
|
public string UncheckAllLabel = Default.Language["ButtonGrid_UncheckAll"];
|
||||||
|
|
||||||
public string CheckedTagsLabel = Default.Language["TaggedButtonGrid_CheckedTags"];
|
|
||||||
|
|
||||||
public string CheckAllLabel = Default.Language["TaggedButtonGrid_CheckAll"];
|
|
||||||
|
|
||||||
public string UncheckAllLabel = Default.Language["TaggedButtonGrid_UncheckAll"];
|
|
||||||
|
|
||||||
public string SearchIcon = Default.Language["ButtonGrid_SearchIcon"];
|
public string SearchIcon = Default.Language["ButtonGrid_SearchIcon"];
|
||||||
|
|
||||||
public string TagIcon = Default.Language["TaggedButtonGrid_TagIcon"];
|
public string TagIcon = Default.Language["ButtonGrid_TagIcon"];
|
||||||
|
|
||||||
public TaggedButtonGrid()
|
public TaggedButtonGrid()
|
||||||
{
|
{
|
||||||
@@ -67,24 +63,7 @@ public class TaggedButtonGrid : MultiView
|
|||||||
DataSource = new ButtonFormDataSource(form);
|
DataSource = new ButtonFormDataSource(form);
|
||||||
}
|
}
|
||||||
|
|
||||||
string m_Title = Default.Language["ButtonGrid_Title"];
|
public string Title { get; set; } = Default.Language["ButtonGrid_Title"];
|
||||||
|
|
||||||
public string Title
|
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
return m_Title;
|
|
||||||
}
|
|
||||||
set
|
|
||||||
{
|
|
||||||
if (string.IsNullOrEmpty(value))
|
|
||||||
{
|
|
||||||
throw new ArgumentNullException($"{nameof(Title)}", $"{nameof(Title)} property must have been a value unequal to null/empty");
|
|
||||||
}
|
|
||||||
|
|
||||||
m_Title = value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public string ConfirmationText { get; set; }
|
public string ConfirmationText { get; set; }
|
||||||
|
|
||||||
@@ -400,6 +379,14 @@ public class TaggedButtonGrid : MultiView
|
|||||||
index = br.Item2;
|
index = br.Item2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//var button = HeadLayoutButtonRow?. .FirstOrDefault(a => a.Text.Trim() == result.MessageText)
|
||||||
|
// ?? SubHeadLayoutButtonRow?.FirstOrDefault(a => a.Text.Trim() == result.MessageText);
|
||||||
|
|
||||||
|
// bf.ToList().FirstOrDefault(a => a.Text.Trim() == result.MessageText)
|
||||||
|
|
||||||
|
//var index = bf.FindRowByButton(button);
|
||||||
|
|
||||||
check:
|
check:
|
||||||
|
|
||||||
|
|
||||||
@@ -503,16 +490,10 @@ public class TaggedButtonGrid : MultiView
|
|||||||
if (result.MessageText == CheckAllLabel)
|
if (result.MessageText == CheckAllLabel)
|
||||||
{
|
{
|
||||||
CheckAllTags();
|
CheckAllTags();
|
||||||
Updated();
|
|
||||||
result.Handled = true;
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
else if (result.MessageText == UncheckAllLabel)
|
else if (result.MessageText == UncheckAllLabel)
|
||||||
{
|
{
|
||||||
UncheckAllTags();
|
UncheckAllTags();
|
||||||
Updated();
|
|
||||||
result.Handled = true;
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var i = result.MessageText.LastIndexOf(" ");
|
var i = result.MessageText.LastIndexOf(" ");
|
||||||
@@ -588,6 +569,15 @@ public class TaggedButtonGrid : MultiView
|
|||||||
index = br.Item2;
|
index = br.Item2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//var bf = DataSource.ButtonForm;
|
||||||
|
|
||||||
|
//var button = HeadLayoutButtonRow?.FirstOrDefault(a => a.Value == result.RawData)
|
||||||
|
// ?? SubHeadLayoutButtonRow?.FirstOrDefault(a => a.Value == result.RawData)
|
||||||
|
// ?? bf.ToList().FirstOrDefault(a => a.Value == result.RawData);
|
||||||
|
|
||||||
|
//var index = bf.FindRowByButton(button);
|
||||||
|
|
||||||
check:
|
check:
|
||||||
if (match != null)
|
if (match != null)
|
||||||
{
|
{
|
||||||
@@ -629,48 +619,23 @@ public class TaggedButtonGrid : MultiView
|
|||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
||||||
default:
|
|
||||||
|
|
||||||
if (SelectedViewIndex != 1)
|
|
||||||
return;
|
|
||||||
|
|
||||||
|
|
||||||
switch (result.RawData)
|
|
||||||
{
|
|
||||||
case "$back$":
|
case "$back$":
|
||||||
|
|
||||||
SelectedViewIndex = 0;
|
SelectedViewIndex = 0;
|
||||||
Updated();
|
Updated();
|
||||||
|
|
||||||
return;
|
break;
|
||||||
|
|
||||||
case "$checkall$":
|
case "$checkall$":
|
||||||
|
|
||||||
CheckAllTags();
|
CheckAllTags();
|
||||||
|
|
||||||
return;
|
break;
|
||||||
|
|
||||||
case "$uncheckall$":
|
case "$uncheckall$":
|
||||||
|
|
||||||
UncheckAllTags();
|
UncheckAllTags();
|
||||||
|
|
||||||
return;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
if (SelectedTags.Contains(result.RawData))
|
|
||||||
{
|
|
||||||
SelectedTags.Remove(result.RawData);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
SelectedTags.Add(result.RawData);
|
|
||||||
}
|
|
||||||
|
|
||||||
Updated();
|
|
||||||
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -758,10 +723,8 @@ public class TaggedButtonGrid : MultiView
|
|||||||
|
|
||||||
if (EnableCheckAllTools)
|
if (EnableCheckAllTools)
|
||||||
{
|
{
|
||||||
TagsSubHeadLayoutButtonRow = new ButtonRow(new ButtonBase(string.Format(TotalTagsLabel, Tags.Count), "$total$"),
|
TagsSubHeadLayoutButtonRow = new ButtonRow(new ButtonBase(CheckAllLabel, "$checkall$"),
|
||||||
new ButtonBase(CheckAllLabel, "$checkall$"),
|
new ButtonBase(UncheckAllLabel, "$uncheckall$"));
|
||||||
new ButtonBase(UncheckAllLabel, "$uncheckall$"),
|
|
||||||
new ButtonBase(string.Format(CheckedTagsLabel, SelectedTags.Count), "checked$"));
|
|
||||||
bf.AddButtonRow(TagsSubHeadLayoutButtonRow);
|
bf.AddButtonRow(TagsSubHeadLayoutButtonRow);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -793,6 +756,10 @@ public class TaggedButtonGrid : MultiView
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//if (bf.Count == 0)
|
||||||
|
// return;
|
||||||
|
|
||||||
|
|
||||||
var rkm = (ReplyKeyboardMarkup)bf;
|
var rkm = (ReplyKeyboardMarkup)bf;
|
||||||
rkm.ResizeKeyboard = ResizeKeyboard;
|
rkm.ResizeKeyboard = ResizeKeyboard;
|
||||||
rkm.OneTimeKeyboard = OneTimeKeyboard;
|
rkm.OneTimeKeyboard = OneTimeKeyboard;
|
||||||
@@ -914,20 +881,21 @@ public class TaggedButtonGrid : MultiView
|
|||||||
{
|
{
|
||||||
Message m = null;
|
Message m = null;
|
||||||
|
|
||||||
ButtonForm form = null;
|
var form = DataSource.PickItems(CurrentPageIndex * ItemRowsPerPage, ItemRowsPerPage,
|
||||||
|
EnableSearch ? SearchQuery : null);
|
||||||
|
|
||||||
|
//if (this.EnableSearch && this.SearchQuery != null && this.SearchQuery != "")
|
||||||
|
//{
|
||||||
|
// form = form.FilterDuplicate(this.SearchQuery, true);
|
||||||
|
//}
|
||||||
|
//else
|
||||||
|
//{
|
||||||
|
// form = form.Duplicate();
|
||||||
|
//}
|
||||||
|
|
||||||
if (Tags != null && SelectedTags != null)
|
if (Tags != null && SelectedTags != null)
|
||||||
{
|
{
|
||||||
form = DataSource.PickAllItems(EnableSearch ? SearchQuery : null); //CurrentPageIndex * ItemRowsPerPage, ItemRowsPerPage,
|
|
||||||
|
|
||||||
form = form.TagDuplicate(SelectedTags);
|
form = form.TagDuplicate(SelectedTags);
|
||||||
|
|
||||||
form = new ButtonForm(form.ToRowList().Skip(CurrentPageIndex * ItemRowsPerPage).Take(ItemRowsPerPage));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
form = DataSource.PickItems(CurrentPageIndex * ItemRowsPerPage, ItemRowsPerPage,
|
|
||||||
EnableSearch ? SearchQuery : null);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (EnablePaging)
|
if (EnablePaging)
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ public class Label : ControlBase
|
|||||||
{
|
{
|
||||||
private bool _renderNecessary = true;
|
private bool _renderNecessary = true;
|
||||||
|
|
||||||
private string _text = Default.Language["Label_Text"];
|
private string _text = string.Empty;
|
||||||
|
|
||||||
public String Text
|
public String Text
|
||||||
{
|
{
|
||||||
@@ -29,10 +29,6 @@ public class Label : ControlBase
|
|||||||
if (_text == value)
|
if (_text == value)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (string.IsNullOrEmpty(value))
|
|
||||||
{
|
|
||||||
throw new ArgumentNullException($"{nameof(Text)}", $"{nameof(Text)} property must have been a value unequal to null/empty");
|
|
||||||
}
|
|
||||||
|
|
||||||
_text = value;
|
_text = value;
|
||||||
_renderNecessary = true;
|
_renderNecessary = true;
|
||||||
@@ -40,8 +36,6 @@ public class Label : ControlBase
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private ParseMode _parseMode = ParseMode.Markdown;
|
private ParseMode _parseMode = ParseMode.Markdown;
|
||||||
|
|
||||||
public ParseMode ParseMode
|
public ParseMode ParseMode
|
||||||
|
|||||||
@@ -32,32 +32,10 @@ public class MultiToggleButton : ControlBase
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public string ChangedString { get; set; } = Default.Language["MultiToggleButton_Changed"];
|
public string ChangedString { get; set; } = Default.Language["MultiToggleButton_Changed"];
|
||||||
|
|
||||||
private string _title = Default.Language["MultiToggleButton_Title"];
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// This holds the title of the control.
|
/// This holds the title of the control.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public String Title
|
public string Title { get; set; } = Default.Language["MultiToggleButton_Title"];
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
return _title;
|
|
||||||
}
|
|
||||||
set
|
|
||||||
{
|
|
||||||
if (_title == value)
|
|
||||||
return;
|
|
||||||
|
|
||||||
if (string.IsNullOrEmpty(value))
|
|
||||||
{
|
|
||||||
throw new ArgumentNullException($"{nameof(Title)}", $"{nameof(Title)} must have been a value unequal to null/empty");
|
|
||||||
}
|
|
||||||
|
|
||||||
_title = value;
|
|
||||||
_renderNecessary = true;
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public int? MessageId { get; set; }
|
public int? MessageId { get; set; }
|
||||||
|
|
||||||
|
|||||||
@@ -36,31 +36,7 @@ public class ToggleButton : ControlBase
|
|||||||
|
|
||||||
public string ChangedString { get; set; } = Default.Language["ToggleButton_Changed"];
|
public string ChangedString { get; set; } = Default.Language["ToggleButton_Changed"];
|
||||||
|
|
||||||
private string _title = Default.Language["ToggleButton_Title"];
|
public string Title { get; set; } = Default.Language["ToggleButton_Title"];
|
||||||
|
|
||||||
public String Title
|
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
return _title;
|
|
||||||
}
|
|
||||||
set
|
|
||||||
{
|
|
||||||
if (_title == value)
|
|
||||||
return;
|
|
||||||
|
|
||||||
if (string.IsNullOrEmpty(value))
|
|
||||||
{
|
|
||||||
throw new ArgumentNullException($"{nameof(Title)}", $"{nameof(Title)} property must have been a value unequal to null/empty");
|
|
||||||
}
|
|
||||||
|
|
||||||
_title = value;
|
|
||||||
_renderNecessary = true;
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public int? MessageId { get; set; }
|
public int? MessageId { get; set; }
|
||||||
|
|
||||||
|
|||||||
@@ -5,31 +5,17 @@ namespace TelegramBotBase.Exceptions;
|
|||||||
|
|
||||||
public sealed class CallbackDataTooLongException : Exception
|
public sealed class CallbackDataTooLongException : Exception
|
||||||
{
|
{
|
||||||
|
//public override string Message =>
|
||||||
|
// $"You have exceeded the maximum {Constants.Telegram.MaxCallBackDataBytes} bytes.";
|
||||||
|
|
||||||
static ApiRequestException _innerException = new Telegram.Bot.Exceptions.ApiRequestException("Bad Request: BUTTON_DATA_INVALID", 400);
|
static ApiRequestException _innerException = new Telegram.Bot.Exceptions.ApiRequestException("Bad Request: BUTTON_DATA_INVALID", 400);
|
||||||
|
|
||||||
static String _message = $"You have exceeded the maximum {Constants.Telegram.MaxCallBackDataBytes} bytes of callback data.\r\nThis is a pre-sending message from the TelegramBotBase framework.\r\nread more: https://core.telegram.org/bots/api#inlinekeyboardbutton";
|
static String _message = $"You have exceeded the maximum {Constants.Telegram.MaxCallBackDataBytes} bytes of callback data.\r\nThis is a pre-sending message from the TelegramBotBase framework.\r\nread more: https://core.telegram.org/bots/api#inlinekeyboardbutton";
|
||||||
|
|
||||||
static String _message_with_bytes = $"You have exceeded the maximum {Constants.Telegram.MaxCallBackDataBytes} bytes of callback data with @current_callback_bytes@ bytes.\r\nThis is a pre-sending message from the TelegramBotBase framework.\r\nread more: https://core.telegram.org/bots/api#inlinekeyboardbutton";
|
|
||||||
|
|
||||||
public CallbackDataTooLongException() : base(_message, _innerException)
|
public CallbackDataTooLongException() : base(_message, _innerException)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
///
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="current_callback_bytes">The amount of callback bytes generated.</param>
|
|
||||||
public CallbackDataTooLongException(int current_callback_bytes) : base(getMessage(current_callback_bytes), _innerException)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
static String getMessage(int current_callback_bytes = -1)
|
|
||||||
{
|
|
||||||
if (current_callback_bytes == -1)
|
|
||||||
return _message;
|
|
||||||
|
|
||||||
return _message_with_bytes.Replace("@current_callback_bytes@", current_callback_bytes.ToString());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Text;
|
|
||||||
|
|
||||||
namespace TelegramBotBase.Exceptions
|
|
||||||
{
|
|
||||||
public sealed class InvalidServiceProviderConfiguration : Exception
|
|
||||||
{
|
|
||||||
public InvalidServiceProviderConfiguration(string message, Exception innerException) : base(message, innerException) { }
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using TelegramBotBase.DependencyInjection;
|
using TelegramBotBase.DependencyInjection;
|
||||||
using TelegramBotBase.Exceptions;
|
|
||||||
using TelegramBotBase.Form;
|
using TelegramBotBase.Form;
|
||||||
using TelegramBotBase.Interfaces;
|
using TelegramBotBase.Interfaces;
|
||||||
|
|
||||||
@@ -25,16 +24,7 @@ public class ServiceProviderStartFormFactory : IStartFormFactory
|
|||||||
|
|
||||||
public FormBase CreateForm()
|
public FormBase CreateForm()
|
||||||
{
|
{
|
||||||
FormBase fb = null;
|
var fb = (FormBase)ActivatorUtilities.CreateInstance(_serviceProvider, _startFormClass);
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
fb = (FormBase)ActivatorUtilities.CreateInstance(_serviceProvider, _startFormClass);
|
|
||||||
}
|
|
||||||
catch(InvalidOperationException ex)
|
|
||||||
{
|
|
||||||
throw new InvalidServiceProviderConfiguration(ex.Message, ex);
|
|
||||||
}
|
|
||||||
|
|
||||||
//Sets an internal field for future ServiceProvider navigation
|
//Sets an internal field for future ServiceProvider navigation
|
||||||
fb.SetServiceProvider(_serviceProvider);
|
fb.SetServiceProvider(_serviceProvider);
|
||||||
|
|||||||
@@ -23,11 +23,6 @@ public class ButtonForm
|
|||||||
DependencyControl = control;
|
DependencyControl = control;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ButtonForm(IEnumerable<ButtonRow> rows)
|
|
||||||
{
|
|
||||||
_buttons = rows.ToList();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public IReplyMarkup Markup { get; set; }
|
public IReplyMarkup Markup { get; set; }
|
||||||
|
|
||||||
@@ -154,11 +149,6 @@ public class ButtonForm
|
|||||||
.Aggregate((a, b) => a.Union(b).ToList());
|
.Aggregate((a, b) => a.Union(b).ToList());
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<ButtonRow> ToRowList()
|
|
||||||
{
|
|
||||||
return _buttons;
|
|
||||||
}
|
|
||||||
|
|
||||||
public InlineKeyboardButton[][] ToInlineButtonArray()
|
public InlineKeyboardButton[][] ToInlineButtonArray()
|
||||||
{
|
{
|
||||||
var ikb = _buttons.Select(a => a.ToArray().Select(b => b.ToInlineButton(this)).ToArray()).ToArray();
|
var ikb = _buttons.Select(a => a.ToArray().Select(b => b.ToInlineButton(this)).ToArray()).ToArray();
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
using System.Text.Json;
|
using Newtonsoft.Json;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using TelegramBotBase.Exceptions;
|
using TelegramBotBase.Exceptions;
|
||||||
using System.Text.Json.Serialization;
|
|
||||||
|
|
||||||
namespace TelegramBotBase.Form;
|
namespace TelegramBotBase.Form;
|
||||||
|
|
||||||
@@ -12,7 +11,6 @@ public class CallbackData
|
|||||||
{
|
{
|
||||||
public CallbackData()
|
public CallbackData()
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public CallbackData(string method, string value)
|
public CallbackData(string method, string value)
|
||||||
@@ -21,13 +19,13 @@ public class CallbackData
|
|||||||
Value = value;
|
Value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
[JsonPropertyName("m")] public string Method { get; set; }
|
[JsonProperty("m")] public string Method { get; set; }
|
||||||
|
|
||||||
[JsonPropertyName("v")] public string Value { get; set; }
|
[JsonProperty("v")] public string Value { get; set; }
|
||||||
|
|
||||||
public static string Create(string method, string value)
|
public static string Create(string method, string value)
|
||||||
{
|
{
|
||||||
return new CallbackData(method, value).Serialize(true);
|
return new CallbackData(method, value).Serialize();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -36,16 +34,24 @@ public class CallbackData
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public string Serialize(bool throwExceptionOnOverflow = false)
|
public string Serialize(bool throwExceptionOnOverflow = false)
|
||||||
{
|
{
|
||||||
var s = string.Empty;
|
var s = "";
|
||||||
|
try
|
||||||
s = JsonSerializer.Serialize(this);
|
|
||||||
|
|
||||||
//Is data over 64 bytes ?
|
|
||||||
int byte_count = Encoding.UTF8.GetByteCount(s);
|
|
||||||
if (throwExceptionOnOverflow && byte_count > Constants.Telegram.MaxCallBackDataBytes)
|
|
||||||
{
|
{
|
||||||
throw new CallbackDataTooLongException(byte_count);
|
s = JsonConvert.SerializeObject(this);
|
||||||
}
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
#if DEBUG
|
||||||
|
|
||||||
|
//Data is over 64 bytes
|
||||||
|
if(throwExceptionOnOverflow && Encoding.UTF8.GetByteCount(s) > Constants.Telegram.MaxCallBackDataBytes)
|
||||||
|
{
|
||||||
|
throw new CallbackDataTooLongException();
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
@@ -57,7 +63,18 @@ public class CallbackData
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static CallbackData Deserialize(string data)
|
public static CallbackData Deserialize(string data)
|
||||||
{
|
{
|
||||||
return JsonSerializer.Deserialize<CallbackData>(data);
|
CallbackData cd = null;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
cd = JsonConvert.DeserializeObject<CallbackData>(data);
|
||||||
|
|
||||||
|
return cd;
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static implicit operator string(CallbackData callbackData) => callbackData.Serialize(true);
|
public static implicit operator string(CallbackData callbackData) => callbackData.Serialize(true);
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
using System;
|
using System.Threading.Tasks;
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Telegram.Bot.Types.Enums;
|
using Telegram.Bot.Types.Enums;
|
||||||
using TelegramBotBase.Args;
|
using TelegramBotBase.Args;
|
||||||
using TelegramBotBase.Base;
|
using TelegramBotBase.Base;
|
||||||
@@ -8,8 +7,6 @@ namespace TelegramBotBase.Form;
|
|||||||
|
|
||||||
public class GroupForm : FormBase
|
public class GroupForm : FormBase
|
||||||
{
|
{
|
||||||
//Prior V21
|
|
||||||
[Obsolete("Check Telegram.Bot nuget package changes.")]
|
|
||||||
public override async Task Load(MessageResult message)
|
public override async Task Load(MessageResult message)
|
||||||
{
|
{
|
||||||
switch (message.MessageType)
|
switch (message.MessageType)
|
||||||
@@ -49,46 +46,6 @@ public class GroupForm : FormBase
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Past V21
|
|
||||||
//public override async Task Load(MessageResult message)
|
|
||||||
//{
|
|
||||||
// switch (message.MessageType)
|
|
||||||
// {
|
|
||||||
// case MessageType.NewChatMembers:
|
|
||||||
|
|
||||||
// await OnMemberChanges(new MemberChangeEventArgs(MessageType.NewChatMembers, message,
|
|
||||||
// message.Message.NewChatMembers));
|
|
||||||
|
|
||||||
// break;
|
|
||||||
// case MessageType.LeftChatMember:
|
|
||||||
|
|
||||||
// await OnMemberChanges(new MemberChangeEventArgs(MessageType.LeftChatMember, message,
|
|
||||||
// message.Message.LeftChatMember));
|
|
||||||
|
|
||||||
// break;
|
|
||||||
|
|
||||||
// case MessageType.NewChatPhoto:
|
|
||||||
// case MessageType.DeleteChatPhoto:
|
|
||||||
// case MessageType.NewChatTitle:
|
|
||||||
// case MessageType.MigrateFromChatId:
|
|
||||||
// case MessageType.MigrateToChatId:
|
|
||||||
// case MessageType.PinnedMessage:
|
|
||||||
// case MessageType.GroupChatCreated:
|
|
||||||
// case MessageType.SupergroupChatCreated:
|
|
||||||
// case MessageType.ChannelChatCreated:
|
|
||||||
|
|
||||||
// await OnGroupChanged(new GroupChangedEventArgs(message.MessageType, message));
|
|
||||||
|
|
||||||
// break;
|
|
||||||
|
|
||||||
// default:
|
|
||||||
|
|
||||||
// await OnMessage(message);
|
|
||||||
|
|
||||||
// break;
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
|
|
||||||
public override async Task Edited(MessageResult message)
|
public override async Task Edited(MessageResult message)
|
||||||
{
|
{
|
||||||
await OnMessageEdit(message);
|
await OnMessageEdit(message);
|
||||||
|
|||||||
@@ -82,7 +82,7 @@ public class PromptDialog : ModalDialog
|
|||||||
{
|
{
|
||||||
var bf = new ButtonForm();
|
var bf = new ButtonForm();
|
||||||
bf.AddButtonRow(new ButtonBase(BackLabel, "back"));
|
bf.AddButtonRow(new ButtonBase(BackLabel, "back"));
|
||||||
await Device.Send(Message, (IReplyMarkup)bf);
|
await Device.Send(Message, (ReplyMarkupBase)bf);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,24 +1,10 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Telegram.Bot.Types.Enums;
|
|
||||||
using Telegram.Bot.Types;
|
|
||||||
using TelegramBotBase.Form;
|
using TelegramBotBase.Form;
|
||||||
using Telegram.Bot.Types.ReplyMarkups;
|
|
||||||
using TelegramBotBase.Args;
|
|
||||||
using TelegramBotBase.Base;
|
|
||||||
using Telegram.Bot;
|
|
||||||
using TelegramBotBase.Sessions;
|
|
||||||
|
|
||||||
namespace TelegramBotBase.Interfaces;
|
namespace TelegramBotBase.Interfaces;
|
||||||
|
|
||||||
public interface IDeviceSession : IDeviceSessionMethods
|
internal interface IDeviceSession
|
||||||
{
|
{
|
||||||
MessageClient Client => ActiveForm.Client;
|
|
||||||
|
|
||||||
int LastMessageId => LastMessage?.MessageId ?? -1;
|
|
||||||
|
|
||||||
Message LastMessage { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Device or chat id
|
/// Device or chat id
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -49,6 +35,4 @@ public interface IDeviceSession : IDeviceSessionMethods
|
|||||||
/// contains if the form has been switched (navigated)
|
/// contains if the form has been switched (navigated)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
bool FormSwitched { get; set; }
|
bool FormSwitched { get; set; }
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,117 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Telegram.Bot.Types.Enums;
|
|
||||||
using Telegram.Bot.Types.ReplyMarkups;
|
|
||||||
using Telegram.Bot.Types;
|
|
||||||
using TelegramBotBase.Form;
|
|
||||||
using Telegram.Bot;
|
|
||||||
using TelegramBotBase.Base;
|
|
||||||
using TelegramBotBase.Args;
|
|
||||||
|
|
||||||
namespace TelegramBotBase.Interfaces
|
|
||||||
{
|
|
||||||
public interface IDeviceSessionMethods
|
|
||||||
{
|
|
||||||
|
|
||||||
string GetChatTitle();
|
|
||||||
|
|
||||||
Task BanUser(long userId, DateTime until = default);
|
|
||||||
|
|
||||||
Task UnbanUser(long userId);
|
|
||||||
|
|
||||||
Task ChangeChatPermissions(ChatPermissions permissions);
|
|
||||||
|
|
||||||
Task RestrictUser(long userId, ChatPermissions permissions, bool? useIndependentGroupPermission = null, DateTime until = default);
|
|
||||||
|
|
||||||
Task ConfirmAction(string callbackQueryId, string message = "", bool showAlert = false,
|
|
||||||
string urlToOpen = null);
|
|
||||||
|
|
||||||
Task<bool> DeleteMessage(int messageId = -1);
|
|
||||||
|
|
||||||
Task<bool> DeleteMessage(Message message);
|
|
||||||
|
|
||||||
Task<Message> HideReplyKeyboard(string closedMsg = "Closed", bool autoDeleteResponse = true);
|
|
||||||
|
|
||||||
Task<Message> Send(string text, ButtonForm buttons = null, int replyTo = 0,
|
|
||||||
bool disableNotification = false, ParseMode parseMode = ParseMode.Markdown,
|
|
||||||
bool markdownV2AutoEscape = true);
|
|
||||||
|
|
||||||
Task<Message> Send(string text, IReplyMarkup markup, int replyTo = 0,
|
|
||||||
bool disableNotification = false, ParseMode parseMode = ParseMode.Markdown,
|
|
||||||
bool markdownV2AutoEscape = true);
|
|
||||||
|
|
||||||
Task<Message> Send(string text, InlineKeyboardMarkup markup, int replyTo = 0,
|
|
||||||
bool disableNotification = false, ParseMode parseMode = ParseMode.Markdown,
|
|
||||||
bool markdownV2AutoEscape = true);
|
|
||||||
|
|
||||||
Task SetAction(ChatAction action);
|
|
||||||
|
|
||||||
Task<Message> SendTextFile(string filename, string textcontent, Encoding encoding = null,
|
|
||||||
string caption = "", ButtonForm buttons = null, int replyTo = 0,
|
|
||||||
bool disableNotification = false);
|
|
||||||
|
|
||||||
Task<Message> SendDocument(InputFile document, string caption = "",
|
|
||||||
ButtonForm buttons = null, int replyTo = 0,
|
|
||||||
bool disableNotification = false);
|
|
||||||
|
|
||||||
|
|
||||||
Task<Message> SendPhoto(InputFile file, string caption = null, ButtonForm buttons = null,
|
|
||||||
int replyTo = 0, bool disableNotification = false,
|
|
||||||
ParseMode parseMode = ParseMode.Markdown);
|
|
||||||
|
|
||||||
Task<Message> SendVideo(InputFile file, string caption = null, ButtonForm buttons = null,
|
|
||||||
int replyTo = 0, bool disableNotification = false,
|
|
||||||
ParseMode parseMode = ParseMode.Markdown);
|
|
||||||
|
|
||||||
|
|
||||||
Task<Message> SendVideo(string url, ButtonForm buttons = null, int replyTo = 0,
|
|
||||||
bool disableNotification = false, ParseMode parseMode = ParseMode.Markdown);
|
|
||||||
|
|
||||||
Task<Message> SendVideo(string filename, byte[] video, ButtonForm buttons = null, int replyTo = 0,
|
|
||||||
bool disableNotification = false, ParseMode parseMode = ParseMode.Markdown);
|
|
||||||
|
|
||||||
Task<Message> SendLocalVideo(string filepath, ButtonForm buttons = null, int replyTo = 0,
|
|
||||||
bool disableNotification = false,
|
|
||||||
ParseMode parseMode = ParseMode.Markdown);
|
|
||||||
|
|
||||||
Task<Message> Edit(int messageId, string text, ButtonForm buttons = null,
|
|
||||||
ParseMode parseMode = ParseMode.Markdown);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Task<Message> Edit(int messageId, string text, InlineKeyboardMarkup markup,
|
|
||||||
ParseMode parseMode = ParseMode.Markdown);
|
|
||||||
|
|
||||||
Task<Message> Edit(Message message, ButtonForm buttons = null,
|
|
||||||
ParseMode parseMode = ParseMode.Markdown);
|
|
||||||
|
|
||||||
Task<Message> EditReplyMarkup(int messageId, ButtonForm bf);
|
|
||||||
|
|
||||||
Task<Message> RequestContact(string buttonText = "Send your contact",
|
|
||||||
string requestMessage = "Give me your phone number!",
|
|
||||||
bool oneTimeOnly = true);
|
|
||||||
|
|
||||||
Task<Message> RequestLocation(string buttonText = "Send your location",
|
|
||||||
string requestMessage = "Give me your location!",
|
|
||||||
bool oneTimeOnly = true);
|
|
||||||
|
|
||||||
|
|
||||||
Task<ChatMember> GetChatUser(long userId);
|
|
||||||
|
|
||||||
|
|
||||||
event Async.AsyncEventHandler<MessageSentEventArgs> MessageSent;
|
|
||||||
|
|
||||||
event EventHandler<MessageReceivedEventArgs> MessageReceived;
|
|
||||||
|
|
||||||
event EventHandler<MessageDeletedEventArgs> MessageDeleted;
|
|
||||||
|
|
||||||
Task Api(Func<ITelegramBotClient, Task> call);
|
|
||||||
|
|
||||||
Task<T> Api<T>(Func<ITelegramBotClient, Task<T>> call);
|
|
||||||
|
|
||||||
T Raw<T>(Func<ITelegramBotClient, T> call);
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user