Compare commits
18 Commits
master
...
developmen
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
98199f2e9b | ||
|
|
9146788ab0 | ||
|
|
d72ee38349 | ||
|
|
667ca5ba6c | ||
|
|
19d8a23b98 | ||
|
|
db5dd7862c | ||
|
|
bc1f1fc93d | ||
|
|
4f23c85d16 | ||
|
|
b37b1e2a64 | ||
|
|
fb88d15485 | ||
|
|
06d4fda9a4 | ||
|
|
0e8e96f2d8 | ||
|
|
f21dec5f1c | ||
|
|
a1c4629351 | ||
|
|
9bdea52df9 | ||
|
|
80eecabda4 | ||
|
|
9f16cb4969 | ||
|
|
eaa742e7df |
@ -1,48 +0,0 @@
|
|||||||
name: build nuget workflow for TelegramBotBase project
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
branches:
|
|
||||||
- master
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
Build-TelegramBotBase:
|
|
||||||
env:
|
|
||||||
APP_PROJECT_NAME: TelegramBotBase
|
|
||||||
PACKAGE_VERSION: "123.1.6"
|
|
||||||
strategy:
|
|
||||||
matrix:
|
|
||||||
os:
|
|
||||||
- linux
|
|
||||||
# - win
|
|
||||||
arch:
|
|
||||||
- x64
|
|
||||||
#- x32
|
|
||||||
#- arch64
|
|
||||||
runs-on: [ "${{ matrix.os }}" ]
|
|
||||||
steps:
|
|
||||||
- name: Check out repository code
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
|
|
||||||
- name: Setup dotnet
|
|
||||||
uses: actions/setup-dotnet@v3
|
|
||||||
with:
|
|
||||||
dotnet-version: '8.0.x'
|
|
||||||
|
|
||||||
- name: Restore dependencies
|
|
||||||
run: dotnet restore $APP_PROJECT_NAME /p:Version=$PACKAGE_VERSION
|
|
||||||
|
|
||||||
- name: Build app
|
|
||||||
run: dotnet build -c Release --version-suffix $PACKAGE_VERSION --no-restore $APP_PROJECT_NAME /p:Version=$PACKAGE_VERSION
|
|
||||||
|
|
||||||
- name: Pack app
|
|
||||||
run: dotnet pack --no-build $APP_PROJECT_NAME /p:Version=$PACKAGE_VERSION
|
|
||||||
|
|
||||||
- name: disconnect old source
|
|
||||||
run: dotnet nuget remove source gitea
|
|
||||||
continue-on-error: true
|
|
||||||
|
|
||||||
- name: Connect source
|
|
||||||
run: dotnet nuget add source --name gitea https://git.kosyakmakc.ru/api/packages/kosyakmakc/nuget/index.json
|
|
||||||
|
|
||||||
- name: Upload nuget package
|
|
||||||
run: dotnet nuget push --source gitea --api-key ${{ secrets.kosyakmakc_nuget_publish }} ${{ gitea.workspace }}/${{ env.APP_PROJECT_NAME }}/bin/Release/$APP_PROJECT_NAME.$PACKAGE_VERSION.nupkg
|
|
||||||
56
README.md
56
README.md
@ -14,10 +14,6 @@
|
|||||||
|
|
||||||
**Releases: [GitHub](https://github.com/MajMcCloud/TelegramBotFramework/releases)**
|
**Releases: [GitHub](https://github.com/MajMcCloud/TelegramBotFramework/releases)**
|
||||||
|
|
||||||
**Need your own bot? Get in touch https://t.me/botbasebuilder**
|
|
||||||
|
|
||||||
**on X: @florian_zevedei**
|
|
||||||
|
|
||||||
## Donate
|
## Donate
|
||||||
|
|
||||||
Paypal: [https://paypal.me/majmccloud](https://paypal.me/majmccloud)
|
Paypal: [https://paypal.me/majmccloud](https://paypal.me/majmccloud)
|
||||||
@ -106,7 +102,6 @@ var bot = BotBaseBuilder
|
|||||||
})
|
})
|
||||||
.NoSerialization()
|
.NoSerialization()
|
||||||
.UseEnglish()
|
.UseEnglish()
|
||||||
.UseSingleThread()
|
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
// Upload bot commands to BotFather
|
// Upload bot commands to BotFather
|
||||||
@ -127,32 +122,8 @@ like ChatId and other stuff your carrying.
|
|||||||
From there you build up your bots:
|
From there you build up your bots:
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
public class Start : FormBase
|
public class StartForm : FormBase
|
||||||
{
|
{
|
||||||
|
|
||||||
public Start()
|
|
||||||
{
|
|
||||||
//Additional event handlers
|
|
||||||
Init += Start_Init;
|
|
||||||
Opened += Start_Opened;
|
|
||||||
Closed += Start_Closed;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Gets invoked on initialization, before navigation
|
|
||||||
private async Task Start_Init(object sender, Args.InitEventArgs e)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
// Gets invoked after opened
|
|
||||||
private async Task Start_Opened(object sender, EventArgs e)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
// Gets invoked after form has been closed
|
|
||||||
private async Task Start_Closed(object sender, EventArgs e)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
// Gets invoked during Navigation to this form
|
// Gets invoked during Navigation to this form
|
||||||
public override async Task PreLoad(MessageResult message)
|
public override async Task PreLoad(MessageResult message)
|
||||||
{
|
{
|
||||||
@ -249,7 +220,6 @@ var bot = BotBaseBuilder
|
|||||||
})
|
})
|
||||||
.NoSerialization()
|
.NoSerialization()
|
||||||
.UseEnglish()
|
.UseEnglish()
|
||||||
.UseSingleThread()
|
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
bot.BotCommand += async (s, en) =>
|
bot.BotCommand += async (s, en) =>
|
||||||
@ -290,15 +260,15 @@ public class SimpleForm : AutoCleanForm
|
|||||||
{
|
{
|
||||||
public SimpleForm()
|
public SimpleForm()
|
||||||
{
|
{
|
||||||
DeleteSide = EDeleteSide.Both;
|
this.DeleteSide = TelegramBotBase.Enums.eDeleteSide.Both;
|
||||||
DeleteMode = EDeleteMode.OnLeavingForm;
|
this.DeleteMode = TelegramBotBase.Enums.eDeleteMode.OnLeavingForm;
|
||||||
|
|
||||||
Opened += SimpleForm_Opened;
|
this.Opened += SimpleForm_Opened;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task SimpleForm_Opened(object sender, EventArgs e)
|
private async Task SimpleForm_Opened(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
await Device.Send("Hello world! (send 'back' to get back to Start)\r\nOr\r\nhi, hello, maybe, bye and ciao");
|
await this.Device.Send("Hello world! (send 'back' to get back to Start)\r\nOr\r\nhi, hello, maybe, bye and ciao");
|
||||||
}
|
}
|
||||||
|
|
||||||
public override async Task Load(MessageResult message)
|
public override async Task Load(MessageResult message)
|
||||||
@ -336,13 +306,7 @@ public class SimpleForm : AutoCleanForm
|
|||||||
```csharp
|
```csharp
|
||||||
public class ButtonTestForm : AutoCleanForm
|
public class ButtonTestForm : AutoCleanForm
|
||||||
{
|
{
|
||||||
public ButtonTestForm()
|
public override async Task Opened()
|
||||||
{
|
|
||||||
this.DeleteMode = eDeleteMode.OnLeavingForm;
|
|
||||||
Opened += ButtonTestForm_Opened;
|
|
||||||
}
|
|
||||||
|
|
||||||
private async Task ButtonTestForm_Opened(object sender, EventArgs e)
|
|
||||||
{
|
{
|
||||||
await this.Device.Send("Hello world! (Click 'back' to get back to Start)");
|
await this.Device.Send("Hello world! (Click 'back' to get back to Start)");
|
||||||
}
|
}
|
||||||
@ -412,10 +376,9 @@ public class ProgressTest : AutoCleanForm
|
|||||||
public ProgressTest()
|
public ProgressTest()
|
||||||
{
|
{
|
||||||
this.DeleteMode = eDeleteMode.OnLeavingForm;
|
this.DeleteMode = eDeleteMode.OnLeavingForm;
|
||||||
Opened += ProgressTest_Opened;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task ProgressTest_Opened(object sender, EventArgs e)
|
public override async Task Opened()
|
||||||
{
|
{
|
||||||
await this.Device.Send("Welcome to ProgressTest");
|
await this.Device.Send("Welcome to ProgressTest");
|
||||||
}
|
}
|
||||||
@ -895,7 +858,6 @@ var bot = BotBaseBuilder
|
|||||||
})
|
})
|
||||||
.UseSimpleJSON(AppContext.BaseDirectory + "config\\states.json")
|
.UseSimpleJSON(AppContext.BaseDirectory + "config\\states.json")
|
||||||
.UseEnglish()
|
.UseEnglish()
|
||||||
.UseSingleThread()
|
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
await bot.Start();
|
await bot.Start();
|
||||||
@ -919,7 +881,6 @@ var bot = BotBaseBuilder
|
|||||||
})
|
})
|
||||||
.UseJSON(AppContext.BaseDirectory + "config\\states.json")
|
.UseJSON(AppContext.BaseDirectory + "config\\states.json")
|
||||||
.UseEnglish()
|
.UseEnglish()
|
||||||
.UseSingleThread()
|
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
await bot.Start();
|
await bot.Start();
|
||||||
@ -942,7 +903,6 @@ var bot = BotBaseBuilder
|
|||||||
})
|
})
|
||||||
.UseXML(AppContext.BaseDirectory + "config\\states.xml")
|
.UseXML(AppContext.BaseDirectory + "config\\states.xml")
|
||||||
.UseEnglish()
|
.UseEnglish()
|
||||||
.UseSingleThread()
|
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
await bot.Start();
|
await bot.Start();
|
||||||
@ -956,7 +916,7 @@ datatype and one for implementing into a form which should be invoked with event
|
|||||||
#### IStateMachine
|
#### IStateMachine
|
||||||
|
|
||||||
Is the basic StateMachine interface, it has two methods `SaveFormStates(SaveStatesEventArgs e)`
|
Is the basic StateMachine interface, it has two methods `SaveFormStates(SaveStatesEventArgs e)`
|
||||||
and `LoadFormStates()`, nothing fancy, just simple calls. Implement both methods with your own
|
and `StateContainerLoadFormStates()`, nothing fancy, just simple calls. Implement both methods with your own
|
||||||
serialization process.
|
serialization process.
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
|
|||||||
@ -4,6 +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;
|
using TelegramBotBase.Sessions;
|
||||||
using static IronSoftware.Drawing.AnyBitmap;
|
using static IronSoftware.Drawing.AnyBitmap;
|
||||||
using SKImage = SixLabors.ImageSharp.Image;
|
using SKImage = SixLabors.ImageSharp.Image;
|
||||||
@ -37,7 +38,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 DeviceSession session, AnyBitmap image, string name,
|
public static async Task<Message> SendPhoto(this IDeviceSession 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)
|
||||||
{
|
{
|
||||||
@ -58,7 +59,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 DeviceSession session, SKImage image, string name,
|
public static async Task<Message> SendPhoto(this IDeviceSession 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)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFrameworks>netstandard2.0;netcoreapp3.1;net6</TargetFrameworks>
|
<TargetFrameworks>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>
|
||||||
@ -19,11 +19,14 @@
|
|||||||
<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.Sessions;
|
using TelegramBotBase.Interfaces;
|
||||||
|
|
||||||
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 DeviceSession session, Image image, string name,
|
public static async Task<Message> SendPhoto(this IDeviceSession 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 DeviceSession session, Bitmap image, string name,
|
public static async Task<Message> SendPhoto(this IDeviceSession 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>netstandard2.0;netcoreapp3.1;net6</TargetFrameworks>
|
<TargetFrameworks>netcoreapp3.1;net6;net7;net8</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,11 +21,14 @@
|
|||||||
<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>
|
||||||
|
|||||||
@ -0,0 +1,41 @@
|
|||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,94 @@
|
|||||||
|
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
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,31 @@
|
|||||||
|
# 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();
|
||||||
|
```
|
||||||
@ -0,0 +1,27 @@
|
|||||||
|
<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>
|
||||||
15301
TelegramBotBase.SourceGenerators/Resources/Telegram.Bot.xml
Normal file
15301
TelegramBotBase.SourceGenerators/Resources/Telegram.Bot.xml
Normal file
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,25 @@
|
|||||||
|
<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>
|
||||||
@ -0,0 +1,251 @@
|
|||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
89
TelegramBotBase.SourceGenerators/XmlDocumentationLoader.cs
Normal file
89
TelegramBotBase.SourceGenerators/XmlDocumentationLoader.cs
Normal file
@ -0,0 +1,89 @@
|
|||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -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 Newtonsoft.Json;
|
using System.Text.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 = JsonConvert.DeserializeObject<List<string>>(File.ReadAllText("countries.json"));
|
var list = JsonSerializer.Deserialize<List<string>>(File.ReadAllText("countries.json"));
|
||||||
|
|
||||||
|
|
||||||
Countries = list;
|
Countries = list;
|
||||||
@ -56,7 +56,7 @@ public class CustomDataSource : ButtonFormDataSource
|
|||||||
|
|
||||||
Countries = countries;
|
Countries = countries;
|
||||||
|
|
||||||
var tmp = JsonConvert.SerializeObject(countries);
|
var tmp = JsonSerializer.Serialize(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.Sessions;
|
using TelegramBotBase.Interfaces;
|
||||||
|
|
||||||
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,
|
||||||
DeviceSession device)
|
IDeviceSession 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 DeviceSession Device { get; set; }
|
public IDeviceSession 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.Sessions;
|
using TelegramBotBase.Interfaces;
|
||||||
|
|
||||||
namespace TelegramBotBase.Base;
|
namespace TelegramBotBase.Base;
|
||||||
|
|
||||||
public class MessageIncomeEventArgs : EventArgs
|
public class MessageIncomeEventArgs : EventArgs
|
||||||
{
|
{
|
||||||
public MessageIncomeEventArgs(long deviceId, DeviceSession device, MessageResult message)
|
public MessageIncomeEventArgs(long deviceId, IDeviceSession 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 DeviceSession Device { get; set; }
|
public IDeviceSession Device { get; set; }
|
||||||
|
|
||||||
public MessageResult Message { get; set; }
|
public MessageResult Message { get; set; }
|
||||||
}
|
}
|
||||||
@ -1,11 +1,11 @@
|
|||||||
using System;
|
using System;
|
||||||
using TelegramBotBase.Sessions;
|
using TelegramBotBase.Interfaces;
|
||||||
|
|
||||||
namespace TelegramBotBase.Base;
|
namespace TelegramBotBase.Base;
|
||||||
|
|
||||||
public class SessionBeginEventArgs : EventArgs
|
public class SessionBeginEventArgs : EventArgs
|
||||||
{
|
{
|
||||||
public SessionBeginEventArgs(long deviceId, DeviceSession device)
|
public SessionBeginEventArgs(long deviceId, IDeviceSession 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 DeviceSession Device { get; set; }
|
public IDeviceSession Device { get; set; }
|
||||||
}
|
}
|
||||||
@ -1,5 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
using TelegramBotBase.Sessions;
|
using TelegramBotBase.Interfaces;
|
||||||
|
|
||||||
namespace TelegramBotBase.Args;
|
namespace TelegramBotBase.Args;
|
||||||
|
|
||||||
@ -9,7 +9,7 @@ public class SystemExceptionEventArgs : EventArgs
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
public SystemExceptionEventArgs(string command, long deviceId, DeviceSession device, Exception error)
|
public SystemExceptionEventArgs(string command, long deviceId, IDeviceSession 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 DeviceSession Device { get; set; }
|
public IDeviceSession 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.Sessions;
|
using TelegramBotBase.Interfaces;
|
||||||
|
|
||||||
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,
|
||||||
DeviceSession device) : this()
|
IDeviceSession 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 DeviceSession Device { get; set; }
|
public IDeviceSession 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.Sessions;
|
using TelegramBotBase.Interfaces;
|
||||||
|
|
||||||
namespace TelegramBotBase.Base;
|
namespace TelegramBotBase.Base;
|
||||||
|
|
||||||
@ -8,7 +8,7 @@ namespace TelegramBotBase.Base;
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class ControlBase
|
public class ControlBase
|
||||||
{
|
{
|
||||||
public DeviceSession Device { get; set; }
|
public IDeviceSession Device { get; set; }
|
||||||
|
|
||||||
public int Id { get; set; }
|
public int Id { get; set; }
|
||||||
|
|
||||||
|
|||||||
@ -55,7 +55,7 @@ public class DataResult : ResultBase
|
|||||||
{
|
{
|
||||||
var encryptedContent = new MemoryStream();
|
var encryptedContent = new MemoryStream();
|
||||||
encryptedContent.SetLength(Document.FileSize.Value);
|
encryptedContent.SetLength(Document.FileSize.Value);
|
||||||
var file = await Device.Client.TelegramClient.GetInfoAndDownloadFile(Document.FileId,
|
var file = await Device.Client.TelegramClient.GetInfoAndDownloadFileAsync(Document.FileId,
|
||||||
encryptedContent);
|
encryptedContent);
|
||||||
|
|
||||||
return InputFile.FromStream(encryptedContent, Document.FileName);
|
return InputFile.FromStream(encryptedContent, Document.FileName);
|
||||||
@ -69,9 +69,9 @@ public class DataResult : ResultBase
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task DownloadDocument(string path)
|
public async Task DownloadDocument(string path)
|
||||||
{
|
{
|
||||||
var file = await Device.Client.TelegramClient.GetFile(Document.FileId);
|
var file = await Device.Client.TelegramClient.GetFileAsync(Document.FileId);
|
||||||
var fs = new FileStream(path, FileMode.Create);
|
var fs = new FileStream(path, FileMode.Create);
|
||||||
await Device.Client.TelegramClient.DownloadFile(file.FilePath, fs);
|
await Device.Client.TelegramClient.DownloadFileAsync(file.FilePath, fs);
|
||||||
fs.Close();
|
fs.Close();
|
||||||
fs.Dispose();
|
fs.Dispose();
|
||||||
}
|
}
|
||||||
@ -83,7 +83,7 @@ public class DataResult : ResultBase
|
|||||||
public async Task<byte[]> DownloadRawDocument()
|
public async Task<byte[]> DownloadRawDocument()
|
||||||
{
|
{
|
||||||
var ms = new MemoryStream();
|
var ms = new MemoryStream();
|
||||||
await Device.Client.TelegramClient.GetInfoAndDownloadFile(Document.FileId, ms);
|
await Device.Client.TelegramClient.GetInfoAndDownloadFileAsync(Document.FileId, ms);
|
||||||
return ms.ToArray();
|
return ms.ToArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -103,7 +103,7 @@ public class DataResult : ResultBase
|
|||||||
public async Task<string> DownloadRawTextDocument(Encoding encoding)
|
public async Task<string> DownloadRawTextDocument(Encoding encoding)
|
||||||
{
|
{
|
||||||
var ms = new MemoryStream();
|
var ms = new MemoryStream();
|
||||||
await Device.Client.TelegramClient.GetInfoAndDownloadFile(Document.FileId, ms);
|
await Device.Client.TelegramClient.GetInfoAndDownloadFileAsync(Document.FileId, ms);
|
||||||
|
|
||||||
ms.Position = 0;
|
ms.Position = 0;
|
||||||
|
|
||||||
@ -116,16 +116,16 @@ public class DataResult : ResultBase
|
|||||||
{
|
{
|
||||||
var encryptedContent = new MemoryStream();
|
var encryptedContent = new MemoryStream();
|
||||||
encryptedContent.SetLength(Video.FileSize.Value);
|
encryptedContent.SetLength(Video.FileSize.Value);
|
||||||
var file = await Device.Client.TelegramClient.GetInfoAndDownloadFile(Video.FileId, encryptedContent);
|
var file = await Device.Client.TelegramClient.GetInfoAndDownloadFileAsync(Video.FileId, encryptedContent);
|
||||||
|
|
||||||
return InputFile.FromStream(encryptedContent, "");
|
return InputFile.FromStream(encryptedContent, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task DownloadVideo(string path)
|
public async Task DownloadVideo(string path)
|
||||||
{
|
{
|
||||||
var file = await Device.Client.TelegramClient.GetFile(Video.FileId);
|
var file = await Device.Client.TelegramClient.GetFileAsync(Video.FileId);
|
||||||
var fs = new FileStream(path, FileMode.Create);
|
var fs = new FileStream(path, FileMode.Create);
|
||||||
await Device.Client.TelegramClient.DownloadFile(file.FilePath, fs);
|
await Device.Client.TelegramClient.DownloadFileAsync(file.FilePath, fs);
|
||||||
fs.Close();
|
fs.Close();
|
||||||
fs.Dispose();
|
fs.Dispose();
|
||||||
}
|
}
|
||||||
@ -134,16 +134,16 @@ public class DataResult : ResultBase
|
|||||||
{
|
{
|
||||||
var encryptedContent = new MemoryStream();
|
var encryptedContent = new MemoryStream();
|
||||||
encryptedContent.SetLength(Audio.FileSize.Value);
|
encryptedContent.SetLength(Audio.FileSize.Value);
|
||||||
var file = await Device.Client.TelegramClient.GetInfoAndDownloadFile(Audio.FileId, encryptedContent);
|
var file = await Device.Client.TelegramClient.GetInfoAndDownloadFileAsync(Audio.FileId, encryptedContent);
|
||||||
|
|
||||||
return InputFile.FromStream(encryptedContent, "");
|
return InputFile.FromStream(encryptedContent, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task DownloadAudio(string path)
|
public async Task DownloadAudio(string path)
|
||||||
{
|
{
|
||||||
var file = await Device.Client.TelegramClient.GetFile(Audio.FileId);
|
var file = await Device.Client.TelegramClient.GetFileAsync(Audio.FileId);
|
||||||
var fs = new FileStream(path, FileMode.Create);
|
var fs = new FileStream(path, FileMode.Create);
|
||||||
await Device.Client.TelegramClient.DownloadFile(file.FilePath, fs);
|
await Device.Client.TelegramClient.DownloadFileAsync(file.FilePath, fs);
|
||||||
fs.Close();
|
fs.Close();
|
||||||
fs.Dispose();
|
fs.Dispose();
|
||||||
}
|
}
|
||||||
@ -153,7 +153,7 @@ public class DataResult : ResultBase
|
|||||||
var photo = Photos[index];
|
var photo = Photos[index];
|
||||||
var encryptedContent = new MemoryStream();
|
var encryptedContent = new MemoryStream();
|
||||||
encryptedContent.SetLength(photo.FileSize.Value);
|
encryptedContent.SetLength(photo.FileSize.Value);
|
||||||
var file = await Device.Client.TelegramClient.GetInfoAndDownloadFile(photo.FileId, encryptedContent);
|
var file = await Device.Client.TelegramClient.GetInfoAndDownloadFileAsync(photo.FileId, encryptedContent);
|
||||||
|
|
||||||
return InputFile.FromStream(encryptedContent, "");
|
return InputFile.FromStream(encryptedContent, "");
|
||||||
}
|
}
|
||||||
@ -161,9 +161,9 @@ public class DataResult : ResultBase
|
|||||||
public async Task DownloadPhoto(int index, string path)
|
public async Task DownloadPhoto(int index, string path)
|
||||||
{
|
{
|
||||||
var photo = Photos[index];
|
var photo = Photos[index];
|
||||||
var file = await Device.Client.TelegramClient.GetFile(photo.FileId);
|
var file = await Device.Client.TelegramClient.GetFileAsync(photo.FileId);
|
||||||
var fs = new FileStream(path, FileMode.Create);
|
var fs = new FileStream(path, FileMode.Create);
|
||||||
await Device.Client.TelegramClient.DownloadFile(file.FilePath, fs);
|
await Device.Client.TelegramClient.DownloadFileAsync(file.FilePath, fs);
|
||||||
fs.Close();
|
fs.Close();
|
||||||
fs.Dispose();
|
fs.Dispose();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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.Sessions;
|
using TelegramBotBase.Interfaces;
|
||||||
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 DeviceSession Device { get; set; }
|
public IDeviceSession Device { get; set; }
|
||||||
|
|
||||||
public MessageClient Client { get; set; }
|
public MessageClient Client { get; set; }
|
||||||
|
|
||||||
@ -446,8 +446,8 @@ public class FormBase : IDisposable
|
|||||||
{
|
{
|
||||||
c.Cleanup().Wait();
|
c.Cleanup().Wait();
|
||||||
|
|
||||||
|
Controls.Remove(c);
|
||||||
}
|
}
|
||||||
Controls.Clear();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@ -103,7 +103,7 @@ public class MessageClient
|
|||||||
|
|
||||||
var receiverOptions = new ReceiverOptions();
|
var receiverOptions = new ReceiverOptions();
|
||||||
|
|
||||||
receiverOptions.DropPendingUpdates = ThrowPendingUpdates;
|
receiverOptions.ThrowPendingUpdates = ThrowPendingUpdates;
|
||||||
|
|
||||||
TelegramClient.StartReceiving(HandleUpdateAsync, HandleErrorAsync, receiverOptions, _cancellationTokenSource.Token);
|
TelegramClient.StartReceiving(HandleUpdateAsync, HandleErrorAsync, receiverOptions, _cancellationTokenSource.Token);
|
||||||
}
|
}
|
||||||
@ -111,7 +111,7 @@ public class MessageClient
|
|||||||
|
|
||||||
public virtual void StopReceiving()
|
public virtual void StopReceiving()
|
||||||
{
|
{
|
||||||
_cancellationTokenSource.Cancel();
|
_cancellationTokenSource?.Cancel();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -1,8 +1,8 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
|
using System.Threading.Tasks;
|
||||||
using Telegram.Bot.Types;
|
using Telegram.Bot.Types;
|
||||||
using Telegram.Bot.Types.Enums;
|
using Telegram.Bot.Types.Enums;
|
||||||
|
|
||||||
|
|||||||
@ -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.Sessions;
|
using TelegramBotBase.Interfaces;
|
||||||
|
|
||||||
namespace TelegramBotBase.Base;
|
namespace TelegramBotBase.Base;
|
||||||
|
|
||||||
public class ResultBase : EventArgs
|
public class ResultBase : EventArgs
|
||||||
{
|
{
|
||||||
public DeviceSession Device { get; set; }
|
public IDeviceSession Device { get; set; }
|
||||||
|
|
||||||
public virtual long DeviceId { get; set; }
|
public virtual long DeviceId { get; set; }
|
||||||
|
|
||||||
|
|||||||
@ -72,7 +72,7 @@ public class ThreadPoolMessageClient : MessageClient
|
|||||||
|
|
||||||
var receiverOptions = new ReceiverOptions();
|
var receiverOptions = new ReceiverOptions();
|
||||||
|
|
||||||
receiverOptions.DropPendingUpdates = ThrowPendingUpdates;
|
receiverOptions.ThrowPendingUpdates = ThrowPendingUpdates;
|
||||||
|
|
||||||
ThreadPool.SetMaxThreads(ThreadPool_WorkerThreads, ThreadPool_IOThreads);
|
ThreadPool.SetMaxThreads(ThreadPool_WorkerThreads, ThreadPool_IOThreads);
|
||||||
|
|
||||||
@ -81,7 +81,7 @@ public class ThreadPoolMessageClient : MessageClient
|
|||||||
|
|
||||||
public override void StopReceiving()
|
public override void StopReceiving()
|
||||||
{
|
{
|
||||||
_cancellationTokenSource.Cancel();
|
_cancellationTokenSource?.Cancel();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -1,11 +1,11 @@
|
|||||||
using Telegram.Bot.Types;
|
using Telegram.Bot.Types;
|
||||||
using TelegramBotBase.Sessions;
|
using TelegramBotBase.Interfaces;
|
||||||
|
|
||||||
namespace TelegramBotBase.Base;
|
namespace TelegramBotBase.Base;
|
||||||
|
|
||||||
public class UpdateResult : ResultBase
|
public class UpdateResult : ResultBase
|
||||||
{
|
{
|
||||||
public UpdateResult(Update rawData, DeviceSession device)
|
public UpdateResult(Update rawData, IDeviceSession device)
|
||||||
{
|
{
|
||||||
RawData = rawData;
|
RawData = rawData;
|
||||||
Device = device;
|
Device = device;
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
using TelegramBotBase.Interfaces;
|
using System;
|
||||||
|
using TelegramBotBase.Interfaces;
|
||||||
|
|
||||||
namespace TelegramBotBase.Builder.Interfaces;
|
namespace TelegramBotBase.Builder.Interfaces;
|
||||||
|
|
||||||
@ -22,15 +23,32 @@ 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>
|
||||||
@ -39,6 +57,7 @@ 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>
|
||||||
@ -46,6 +65,7 @@ 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>
|
||||||
|
|||||||
@ -14,7 +14,7 @@ namespace TelegramBotBase.Controls.Inline;
|
|||||||
[DebuggerDisplay("{Text}")]
|
[DebuggerDisplay("{Text}")]
|
||||||
public class Label : ControlBase
|
public class Label : ControlBase
|
||||||
{
|
{
|
||||||
protected bool _renderNecessary = true;
|
private bool _renderNecessary = true;
|
||||||
|
|
||||||
private string _text = Default.Language["Label_Text"];
|
private string _text = Default.Language["Label_Text"];
|
||||||
|
|
||||||
|
|||||||
@ -23,15 +23,13 @@ public class ServiceProviderStartFormFactory : IStartFormFactory
|
|||||||
_serviceProvider = serviceProvider;
|
_serviceProvider = serviceProvider;
|
||||||
}
|
}
|
||||||
|
|
||||||
public FormBase CreateForm() => CreateForm(null);
|
public FormBase CreateForm()
|
||||||
|
|
||||||
public FormBase CreateForm(Type? specifiedStartFrom = null)
|
|
||||||
{
|
{
|
||||||
FormBase fb = null;
|
FormBase fb = null;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
fb = (FormBase)ActivatorUtilities.CreateInstance(_serviceProvider, specifiedStartFrom ?? _startFormClass);
|
fb = (FormBase)ActivatorUtilities.CreateInstance(_serviceProvider, _startFormClass);
|
||||||
}
|
}
|
||||||
catch(InvalidOperationException ex)
|
catch(InvalidOperationException ex)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -24,7 +24,7 @@ public class AutoCleanForm : FormBase
|
|||||||
DeleteMode = EDeleteMode.OnEveryCall;
|
DeleteMode = EDeleteMode.OnEveryCall;
|
||||||
DeleteSide = EDeleteSide.BotOnly;
|
DeleteSide = EDeleteSide.BotOnly;
|
||||||
|
|
||||||
Opened += AutoCleanForm_Init;
|
Init += AutoCleanForm_Init;
|
||||||
|
|
||||||
Closed += AutoCleanForm_Closed;
|
Closed += AutoCleanForm_Closed;
|
||||||
}
|
}
|
||||||
@ -35,7 +35,7 @@ public class AutoCleanForm : FormBase
|
|||||||
|
|
||||||
[SaveState] public EDeleteSide DeleteSide { get; set; }
|
[SaveState] public EDeleteSide DeleteSide { get; set; }
|
||||||
|
|
||||||
private Task AutoCleanForm_Init(object sender, EventArgs e)
|
private Task AutoCleanForm_Init(object sender, InitEventArgs e)
|
||||||
{
|
{
|
||||||
if (Device == null)
|
if (Device == null)
|
||||||
{
|
{
|
||||||
@ -70,8 +70,7 @@ public class AutoCleanForm : FormBase
|
|||||||
|
|
||||||
private Task Device_MessageSent(object sender, MessageSentEventArgs e)
|
private Task Device_MessageSent(object sender, MessageSentEventArgs e)
|
||||||
{
|
{
|
||||||
if (DeleteSide == EDeleteSide.UserOnly
|
if (DeleteSide == EDeleteSide.UserOnly)
|
||||||
|| Device.ActiveForm != this)
|
|
||||||
{
|
{
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
@ -138,12 +137,6 @@ public class AutoCleanForm : FormBase
|
|||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
Device.MessageSent -= Device_MessageSent;
|
|
||||||
|
|
||||||
Device.MessageReceived -= Device_MessageReceived;
|
|
||||||
|
|
||||||
Device.MessageDeleted -= Device_MessageDeleted;
|
|
||||||
|
|
||||||
MessageCleanup().Wait();
|
MessageCleanup().Wait();
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,17 +1,18 @@
|
|||||||
using System.Text;
|
using System.Text.Json;
|
||||||
using System.Text.Json;
|
using System.Text;
|
||||||
using System.Text.Json.Serialization;
|
|
||||||
using TelegramBotBase.Exceptions;
|
using TelegramBotBase.Exceptions;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
namespace TelegramBotBase.Form;
|
namespace TelegramBotBase.Form;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Base class for serializing buttons and data
|
/// Base class for serializing buttons and data
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class CallbackData
|
public class CallbackData
|
||||||
{
|
{
|
||||||
public CallbackData()
|
public CallbackData()
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public CallbackData(string method, string value)
|
public CallbackData(string method, string value)
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
using System.Threading.Tasks;
|
using System;
|
||||||
|
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;
|
||||||
@ -7,32 +8,34 @@ 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)
|
||||||
{
|
{
|
||||||
case MessageType.NewChatMembers:
|
case MessageType.ChatMembersAdded:
|
||||||
|
|
||||||
await OnMemberChanges(new MemberChangeEventArgs(MessageType.NewChatMembers, message,
|
await OnMemberChanges(new MemberChangeEventArgs(MessageType.ChatMembersAdded, message,
|
||||||
message.Message.NewChatMembers));
|
message.Message.NewChatMembers));
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case MessageType.LeftChatMember:
|
case MessageType.ChatMemberLeft:
|
||||||
|
|
||||||
await OnMemberChanges(new MemberChangeEventArgs(MessageType.LeftChatMember, message,
|
await OnMemberChanges(new MemberChangeEventArgs(MessageType.ChatMemberLeft, message,
|
||||||
message.Message.LeftChatMember));
|
message.Message.LeftChatMember));
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case MessageType.NewChatPhoto:
|
case MessageType.ChatPhotoChanged:
|
||||||
case MessageType.DeleteChatPhoto:
|
case MessageType.ChatPhotoDeleted:
|
||||||
case MessageType.NewChatTitle:
|
case MessageType.ChatTitleChanged:
|
||||||
case MessageType.MigrateFromChatId:
|
case MessageType.MigratedFromGroup:
|
||||||
case MessageType.MigrateToChatId:
|
case MessageType.MigratedToSupergroup:
|
||||||
case MessageType.PinnedMessage:
|
case MessageType.MessagePinned:
|
||||||
case MessageType.GroupChatCreated:
|
case MessageType.GroupCreated:
|
||||||
case MessageType.SupergroupChatCreated:
|
case MessageType.SupergroupCreated:
|
||||||
case MessageType.ChannelChatCreated:
|
case MessageType.ChannelCreated:
|
||||||
|
|
||||||
await OnGroupChanged(new GroupChangedEventArgs(message.MessageType, message));
|
await OnGroupChanged(new GroupChangedEventArgs(message.MessageType, message));
|
||||||
|
|
||||||
@ -46,6 +49,46 @@ 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);
|
||||||
|
|||||||
@ -1,10 +1,24 @@
|
|||||||
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;
|
||||||
|
|
||||||
internal interface IDeviceSession
|
public interface IDeviceSession : IDeviceSessionMethods
|
||||||
{
|
{
|
||||||
|
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>
|
||||||
@ -35,4 +49,6 @@ internal interface IDeviceSession
|
|||||||
/// 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; }
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
117
TelegramBotBase/Interfaces/IDeviceSessionMethods.cs
Normal file
117
TelegramBotBase/Interfaces/IDeviceSessionMethods.cs
Normal file
@ -0,0 +1,117 @@
|
|||||||
|
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);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -2,13 +2,12 @@
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using TelegramBotBase.Args;
|
using TelegramBotBase.Args;
|
||||||
using TelegramBotBase.Base;
|
using TelegramBotBase.Base;
|
||||||
using TelegramBotBase.Sessions;
|
|
||||||
|
|
||||||
namespace TelegramBotBase.Interfaces;
|
namespace TelegramBotBase.Interfaces;
|
||||||
|
|
||||||
public interface IMessageLoopFactory
|
public interface IMessageLoopFactory
|
||||||
{
|
{
|
||||||
Task MessageLoop(BotBase bot, DeviceSession session, UpdateResult ur, MessageResult e);
|
Task MessageLoop(BotBase bot, IDeviceSession session, UpdateResult ur, MessageResult e);
|
||||||
|
|
||||||
event EventHandler<UnhandledCallEventArgs> UnhandledCall;
|
event EventHandler<UnhandledCallEventArgs> UnhandledCall;
|
||||||
}
|
}
|
||||||
@ -5,7 +5,6 @@ using Telegram.Bot.Types.Enums;
|
|||||||
using TelegramBotBase.Args;
|
using TelegramBotBase.Args;
|
||||||
using TelegramBotBase.Base;
|
using TelegramBotBase.Base;
|
||||||
using TelegramBotBase.Interfaces;
|
using TelegramBotBase.Interfaces;
|
||||||
using TelegramBotBase.Sessions;
|
|
||||||
|
|
||||||
namespace TelegramBotBase.MessageLoops;
|
namespace TelegramBotBase.MessageLoops;
|
||||||
|
|
||||||
@ -18,7 +17,7 @@ public class FormBaseMessageLoop : IMessageLoopFactory
|
|||||||
|
|
||||||
private readonly EventHandlerList _events = new();
|
private readonly EventHandlerList _events = new();
|
||||||
|
|
||||||
public async Task MessageLoop(BotBase bot, DeviceSession session, UpdateResult ur, MessageResult mr)
|
public async Task MessageLoop(BotBase bot, IDeviceSession session, UpdateResult ur, MessageResult mr)
|
||||||
{
|
{
|
||||||
var update = ur.RawData;
|
var update = ur.RawData;
|
||||||
|
|
||||||
@ -46,8 +45,6 @@ public class FormBaseMessageLoop : IMessageLoopFactory
|
|||||||
mr.Device = session;
|
mr.Device = session;
|
||||||
ur.Device = session;
|
ur.Device = session;
|
||||||
|
|
||||||
session.OnMessageReceived(new(mr.Message));
|
|
||||||
|
|
||||||
var activeForm = session.ActiveForm;
|
var activeForm = session.ActiveForm;
|
||||||
|
|
||||||
//Pre Loading Event
|
//Pre Loading Event
|
||||||
|
|||||||
@ -5,7 +5,6 @@ using Telegram.Bot.Types.Enums;
|
|||||||
using TelegramBotBase.Args;
|
using TelegramBotBase.Args;
|
||||||
using TelegramBotBase.Base;
|
using TelegramBotBase.Base;
|
||||||
using TelegramBotBase.Interfaces;
|
using TelegramBotBase.Interfaces;
|
||||||
using TelegramBotBase.Sessions;
|
|
||||||
|
|
||||||
namespace TelegramBotBase.MessageLoops;
|
namespace TelegramBotBase.MessageLoops;
|
||||||
|
|
||||||
@ -18,7 +17,7 @@ public class FullMessageLoop : IMessageLoopFactory
|
|||||||
|
|
||||||
private readonly EventHandlerList _events = new();
|
private readonly EventHandlerList _events = new();
|
||||||
|
|
||||||
public async Task MessageLoop(BotBase bot, DeviceSession session, UpdateResult ur, MessageResult mr)
|
public async Task MessageLoop(BotBase bot, IDeviceSession session, UpdateResult ur, MessageResult mr)
|
||||||
{
|
{
|
||||||
var update = ur.RawData;
|
var update = ur.RawData;
|
||||||
|
|
||||||
|
|||||||
@ -4,7 +4,6 @@ using System.Threading.Tasks;
|
|||||||
using TelegramBotBase.Args;
|
using TelegramBotBase.Args;
|
||||||
using TelegramBotBase.Base;
|
using TelegramBotBase.Base;
|
||||||
using TelegramBotBase.Interfaces;
|
using TelegramBotBase.Interfaces;
|
||||||
using TelegramBotBase.Sessions;
|
|
||||||
|
|
||||||
namespace TelegramBotBase.MessageLoops;
|
namespace TelegramBotBase.MessageLoops;
|
||||||
|
|
||||||
@ -17,7 +16,7 @@ public class MinimalMessageLoop : IMessageLoopFactory
|
|||||||
|
|
||||||
private readonly EventHandlerList _events = new();
|
private readonly EventHandlerList _events = new();
|
||||||
|
|
||||||
public async Task MessageLoop(BotBase bot, DeviceSession session, UpdateResult ur, MessageResult mr)
|
public async Task MessageLoop(BotBase bot, IDeviceSession session, UpdateResult ur, MessageResult mr)
|
||||||
{
|
{
|
||||||
var update = ur.RawData;
|
var update = ur.RawData;
|
||||||
|
|
||||||
|
|||||||
@ -6,7 +6,6 @@ using System.Threading.Tasks;
|
|||||||
using TelegramBotBase.Args;
|
using TelegramBotBase.Args;
|
||||||
using TelegramBotBase.Attributes;
|
using TelegramBotBase.Attributes;
|
||||||
using TelegramBotBase.Base;
|
using TelegramBotBase.Base;
|
||||||
using TelegramBotBase.Factories;
|
|
||||||
using TelegramBotBase.Form;
|
using TelegramBotBase.Form;
|
||||||
using TelegramBotBase.Interfaces;
|
using TelegramBotBase.Interfaces;
|
||||||
using TelegramBotBase.Sessions;
|
using TelegramBotBase.Sessions;
|
||||||
@ -22,7 +21,7 @@ public class SessionManager
|
|||||||
public SessionManager(BotBase botBase)
|
public SessionManager(BotBase botBase)
|
||||||
{
|
{
|
||||||
BotBase = botBase;
|
BotBase = botBase;
|
||||||
SessionList = new Dictionary<long, DeviceSession>();
|
SessionList = new Dictionary<long, IDeviceSession>();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -33,7 +32,7 @@ public class SessionManager
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// A list of all active sessions.
|
/// A list of all active sessions.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Dictionary<long, DeviceSession> SessionList { get; set; }
|
public Dictionary<long, IDeviceSession> SessionList { get; set; }
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -46,7 +45,7 @@ public class SessionManager
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="deviceId"></param>
|
/// <param name="deviceId"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public DeviceSession GetSession(long deviceId)
|
public IDeviceSession GetSession(long deviceId)
|
||||||
{
|
{
|
||||||
var ds = SessionList.FirstOrDefault(a => a.Key == deviceId).Value ?? null;
|
var ds = SessionList.FirstOrDefault(a => a.Key == deviceId).Value ?? null;
|
||||||
return ds;
|
return ds;
|
||||||
@ -58,7 +57,7 @@ public class SessionManager
|
|||||||
/// <typeparam name="T"></typeparam>
|
/// <typeparam name="T"></typeparam>
|
||||||
/// <param name="deviceId"></param>
|
/// <param name="deviceId"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task<DeviceSession> StartSession(long deviceId)
|
public async Task<IDeviceSession> StartSession(long deviceId)
|
||||||
{
|
{
|
||||||
var start = BotBase.StartFormFactory.CreateForm();
|
var start = BotBase.StartFormFactory.CreateForm();
|
||||||
|
|
||||||
@ -92,7 +91,7 @@ public class SessionManager
|
|||||||
/// Returns all active User Sessions.
|
/// Returns all active User Sessions.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public List<DeviceSession> GetUserSessions()
|
public List<IDeviceSession> GetUserSessions()
|
||||||
{
|
{
|
||||||
return SessionList.Where(a => a.Key > 0).Select(a => a.Value).ToList();
|
return SessionList.Where(a => a.Key > 0).Select(a => a.Value).ToList();
|
||||||
}
|
}
|
||||||
@ -101,7 +100,7 @@ public class SessionManager
|
|||||||
/// Returns all active Group Sessions.
|
/// Returns all active Group Sessions.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public List<DeviceSession> GetGroupSessions()
|
public List<IDeviceSession> GetGroupSessions()
|
||||||
{
|
{
|
||||||
return SessionList.Where(a => a.Key < 0).Select(a => a.Value).ToList();
|
return SessionList.Where(a => a.Key < 0).Select(a => a.Value).ToList();
|
||||||
}
|
}
|
||||||
@ -146,17 +145,9 @@ public class SessionManager
|
|||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
FormBase form;
|
|
||||||
if (BotBase.StartFormFactory is ServiceProviderStartFormFactory diFactory)
|
|
||||||
{
|
|
||||||
form = diFactory.CreateForm(t);
|
|
||||||
}
|
|
||||||
//No default constructor, fallback
|
//No default constructor, fallback
|
||||||
else if (t.GetConstructor(new Type[] { })?.Invoke(new object[] { }) is FormBase f)
|
if (!(t.GetConstructor(new Type[] { })?.Invoke(new object[] { }) is FormBase form))
|
||||||
{
|
|
||||||
form = f;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
if (!statemachine.FallbackStateForm.IsSubclassOf(typeof(FormBase)))
|
if (!statemachine.FallbackStateForm.IsSubclassOf(typeof(FormBase)))
|
||||||
{
|
{
|
||||||
@ -302,19 +293,17 @@ public class SessionManager
|
|||||||
|
|
||||||
se.Values = ssea.Values;
|
se.Values = ssea.Values;
|
||||||
}
|
}
|
||||||
else
|
|
||||||
|
//Search for public properties with SaveState attribute
|
||||||
|
var fields = form.GetType()
|
||||||
|
.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
|
||||||
|
.Where(a => a.GetCustomAttributes(typeof(SaveState), true).Length != 0).ToList();
|
||||||
|
|
||||||
|
foreach (var f in fields)
|
||||||
{
|
{
|
||||||
//Search for public properties with SaveState attribute
|
var val = f.GetValue(form);
|
||||||
var fields = form.GetType()
|
|
||||||
.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
|
|
||||||
.Where(a => a.GetCustomAttributes(typeof(SaveState), true).Length != 0).ToList();
|
|
||||||
|
|
||||||
foreach (var f in fields)
|
se.Values.Add("$" + f.Name, val);
|
||||||
{
|
|
||||||
var val = f.GetValue(form);
|
|
||||||
|
|
||||||
se.Values.Add("$" + f.Name, val);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
states.Add(se);
|
states.Add(se);
|
||||||
|
|||||||
@ -239,7 +239,8 @@ public class DeviceSession : IDeviceSession
|
|||||||
text = text.MarkdownV2Escape();
|
text = text.MarkdownV2Escape();
|
||||||
}
|
}
|
||||||
|
|
||||||
var t = Api(a => a.SendMessage(deviceId, text, messageThreadId: null, parseMode: parseMode, replyParameters: replyTo,
|
|
||||||
|
var t = Api(a => a.SendTextMessageAsync(deviceId, text, null, parseMode, replyToMessageId: replyTo,
|
||||||
replyMarkup: markup, disableNotification: disableNotification));
|
replyMarkup: markup, disableNotification: disableNotification));
|
||||||
|
|
||||||
var o = GetOrigin(new StackTrace());
|
var o = GetOrigin(new StackTrace());
|
||||||
@ -293,7 +294,7 @@ public class DeviceSession : IDeviceSession
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
var t = Api(a => a.SendMessage(DeviceId, text, messageThreadId: null, parseMode: parseMode, replyParameters: replyTo,
|
var t = Api(a => a.SendTextMessageAsync(DeviceId, text, null, parseMode, replyToMessageId: replyTo,
|
||||||
replyMarkup: markup, disableNotification: disableNotification));
|
replyMarkup: markup, disableNotification: disableNotification));
|
||||||
|
|
||||||
var o = GetOrigin(new StackTrace());
|
var o = GetOrigin(new StackTrace());
|
||||||
@ -331,7 +332,7 @@ public class DeviceSession : IDeviceSession
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
var t = Api(a => a.SendMessage(DeviceId, text, messageThreadId: null, parseMode: parseMode, replyParameters: replyTo,
|
var t = Api(a => a.SendTextMessageAsync(DeviceId, text, null, parseMode, replyToMessageId: replyTo,
|
||||||
replyMarkup: markup, disableNotification: disableNotification));
|
replyMarkup: markup, disableNotification: disableNotification));
|
||||||
|
|
||||||
var o = GetOrigin(new StackTrace());
|
var o = GetOrigin(new StackTrace());
|
||||||
@ -361,7 +362,7 @@ public class DeviceSession : IDeviceSession
|
|||||||
InlineKeyboardMarkup markup = buttons;
|
InlineKeyboardMarkup markup = buttons;
|
||||||
|
|
||||||
|
|
||||||
var t = Api(a => a.SendPhoto(DeviceId, file, messageThreadId: null, caption: caption, parseMode: parseMode, replyParameters: replyTo,
|
var t = Api(a => a.SendPhotoAsync(DeviceId, file, null, caption, parseMode, replyToMessageId: replyTo,
|
||||||
replyMarkup: markup, disableNotification: disableNotification));
|
replyMarkup: markup, disableNotification: disableNotification));
|
||||||
|
|
||||||
var o = GetOrigin(new StackTrace());
|
var o = GetOrigin(new StackTrace());
|
||||||
@ -391,8 +392,8 @@ public class DeviceSession : IDeviceSession
|
|||||||
InlineKeyboardMarkup markup = buttons;
|
InlineKeyboardMarkup markup = buttons;
|
||||||
|
|
||||||
|
|
||||||
var t = Api(a => a.SendVideo(DeviceId, file, caption: caption, parseMode: parseMode,
|
var t = Api(a => a.SendVideoAsync(DeviceId, file, caption: caption, parseMode: parseMode,
|
||||||
replyParameters: replyTo, replyMarkup: markup,
|
replyToMessageId: replyTo, replyMarkup: markup,
|
||||||
disableNotification: disableNotification));
|
disableNotification: disableNotification));
|
||||||
|
|
||||||
var o = GetOrigin(new StackTrace());
|
var o = GetOrigin(new StackTrace());
|
||||||
@ -421,8 +422,8 @@ public class DeviceSession : IDeviceSession
|
|||||||
InlineKeyboardMarkup markup = buttons;
|
InlineKeyboardMarkup markup = buttons;
|
||||||
|
|
||||||
|
|
||||||
var t = Api(a => a.SendVideo(DeviceId, InputFile.FromUri(url), parseMode: parseMode,
|
var t = Api(a => a.SendVideoAsync(DeviceId, InputFile.FromUri(url), parseMode: parseMode,
|
||||||
replyParameters: replyTo, replyMarkup: markup,
|
replyToMessageId: replyTo, replyMarkup: markup,
|
||||||
disableNotification: disableNotification));
|
disableNotification: disableNotification));
|
||||||
|
|
||||||
var o = GetOrigin(new StackTrace());
|
var o = GetOrigin(new StackTrace());
|
||||||
@ -456,7 +457,7 @@ public class DeviceSession : IDeviceSession
|
|||||||
|
|
||||||
var fts = InputFile.FromStream(ms, filename);
|
var fts = InputFile.FromStream(ms, filename);
|
||||||
|
|
||||||
var t = Api(a => a.SendVideo(DeviceId, fts, parseMode: parseMode, replyParameters: replyTo,
|
var t = Api(a => a.SendVideoAsync(DeviceId, fts, parseMode: parseMode, replyToMessageId: replyTo,
|
||||||
replyMarkup: markup, disableNotification: disableNotification));
|
replyMarkup: markup, disableNotification: disableNotification));
|
||||||
|
|
||||||
var o = GetOrigin(new StackTrace());
|
var o = GetOrigin(new StackTrace());
|
||||||
@ -493,7 +494,7 @@ public class DeviceSession : IDeviceSession
|
|||||||
|
|
||||||
var fts = InputFile.FromStream(fs, filename);
|
var fts = InputFile.FromStream(fs, filename);
|
||||||
|
|
||||||
var t = Api(a => a.SendVideo(DeviceId, fts, parseMode: parseMode, replyParameters: replyTo,
|
var t = Api(a => a.SendVideoAsync(DeviceId, fts, parseMode: parseMode, replyToMessageId: replyTo,
|
||||||
replyMarkup: markup, disableNotification: disableNotification));
|
replyMarkup: markup, disableNotification: disableNotification));
|
||||||
|
|
||||||
var o = GetOrigin(new StackTrace());
|
var o = GetOrigin(new StackTrace());
|
||||||
@ -572,8 +573,8 @@ public class DeviceSession : IDeviceSession
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
var t = Api(a => a.SendDocument(DeviceId, document, messageThreadId: null, thumbnail: null, caption: caption, replyMarkup: markup,
|
var t = Api(a => a.SendDocumentAsync(DeviceId, document, null, null, caption, replyMarkup: markup,
|
||||||
disableNotification: disableNotification, replyParameters: replyTo));
|
disableNotification: disableNotification, replyToMessageId: replyTo));
|
||||||
|
|
||||||
var o = GetOrigin(new StackTrace());
|
var o = GetOrigin(new StackTrace());
|
||||||
await OnMessageSent(new MessageSentEventArgs(await t, o));
|
await OnMessageSent(new MessageSentEventArgs(await t, o));
|
||||||
@ -771,11 +772,11 @@ public class DeviceSession : IDeviceSession
|
|||||||
|
|
||||||
#region "Users"
|
#region "Users"
|
||||||
|
|
||||||
public virtual async Task RestrictUser(long userId, ChatPermissions permissions, bool useIndependentGroupPermission = false, DateTime until = default)
|
public virtual async Task RestrictUser(long userId, ChatPermissions permissions, bool? useIndependentGroupPermission = null, DateTime until = default)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await Api(a => a.RestrictChatMember(DeviceId, userId, permissions, useIndependentChatPermissions: useIndependentGroupPermission, untilDate: until));
|
await Api(a => a.RestrictChatMemberAsync(DeviceId, userId, permissions, useIndependentGroupPermission, until));
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
|
|||||||
155
TelegramBotBase/States/Converter/DictionaryTypeConverter.cs
Normal file
155
TelegramBotBase/States/Converter/DictionaryTypeConverter.cs
Normal file
@ -0,0 +1,155 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
using System.Text.Json;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
|
namespace TelegramBotBase.States.Converter
|
||||||
|
{
|
||||||
|
public class DictionaryObjectJsonConverter : JsonConverter<Dictionary<string, object>>
|
||||||
|
{
|
||||||
|
public override Dictionary<string, object> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||||
|
{
|
||||||
|
var dictionary = new Dictionary<string, object>();
|
||||||
|
var jsonDocument = JsonDocument.ParseValue(ref reader);
|
||||||
|
|
||||||
|
foreach (var element in jsonDocument.RootElement.EnumerateObject())
|
||||||
|
{
|
||||||
|
switch (element.Value.ValueKind)
|
||||||
|
{
|
||||||
|
case JsonValueKind.String:
|
||||||
|
dictionary[element.Name] = element.Value.GetString();
|
||||||
|
break;
|
||||||
|
case JsonValueKind.Number:
|
||||||
|
|
||||||
|
if (element.Value.TryGetInt32(out var number))
|
||||||
|
{
|
||||||
|
dictionary[element.Name] = number;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (element.Value.TryGetInt64(out long l))
|
||||||
|
dictionary[element.Name] = l;
|
||||||
|
else
|
||||||
|
dictionary[element.Name] = element.Value.GetDouble();
|
||||||
|
break;
|
||||||
|
case JsonValueKind.True:
|
||||||
|
case JsonValueKind.False:
|
||||||
|
dictionary[element.Name] = element.Value.GetBoolean();
|
||||||
|
break;
|
||||||
|
case JsonValueKind.Object:
|
||||||
|
dictionary[element.Name] = JsonSerializer.Deserialize<Dictionary<string, object>>(element.Value.GetRawText(), options);
|
||||||
|
break;
|
||||||
|
case JsonValueKind.Array:
|
||||||
|
|
||||||
|
dictionary[element.Name] = HandleArray(element.Value);
|
||||||
|
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
dictionary[element.Name] = element.Value.GetRawText();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return dictionary;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private object HandleArray(JsonElement jsonArray)
|
||||||
|
{
|
||||||
|
// Hier wird geprüft, ob alle Elemente einer bestimmten Art angehören (z. B. int, string)
|
||||||
|
if (jsonArray.GetArrayLength() > 0)
|
||||||
|
{
|
||||||
|
var firstElement = jsonArray[0];
|
||||||
|
switch (firstElement.ValueKind)
|
||||||
|
{
|
||||||
|
case JsonValueKind.Number:
|
||||||
|
// Prüfen, ob alle Elemente ganze Zahlen sind
|
||||||
|
var isIntArray = true;
|
||||||
|
var isLongArray = false;
|
||||||
|
|
||||||
|
foreach (var element in jsonArray.EnumerateArray())
|
||||||
|
{
|
||||||
|
if (!element.TryGetInt32(out _))
|
||||||
|
{
|
||||||
|
isIntArray = false;
|
||||||
|
isLongArray = true;
|
||||||
|
if (!element.TryGetInt64(out _))
|
||||||
|
{
|
||||||
|
isLongArray = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (isIntArray)
|
||||||
|
{
|
||||||
|
var list = new List<int>();
|
||||||
|
foreach (var element in jsonArray.EnumerateArray())
|
||||||
|
{
|
||||||
|
list.Add(element.GetInt32());
|
||||||
|
}
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
else if (isLongArray)
|
||||||
|
{
|
||||||
|
var list = new List<long>();
|
||||||
|
foreach (var element in jsonArray.EnumerateArray())
|
||||||
|
{
|
||||||
|
list.Add(element.GetInt64());
|
||||||
|
}
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var list = new List<double>();
|
||||||
|
foreach (var element in jsonArray.EnumerateArray())
|
||||||
|
{
|
||||||
|
list.Add(element.GetDouble());
|
||||||
|
}
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
case JsonValueKind.String:
|
||||||
|
var stringList = new List<string>();
|
||||||
|
foreach (var element in jsonArray.EnumerateArray())
|
||||||
|
{
|
||||||
|
stringList.Add(element.GetString());
|
||||||
|
}
|
||||||
|
return stringList;
|
||||||
|
case JsonValueKind.True:
|
||||||
|
case JsonValueKind.False:
|
||||||
|
var boolList = new List<bool>();
|
||||||
|
foreach (var element in jsonArray.EnumerateArray())
|
||||||
|
{
|
||||||
|
boolList.Add(element.GetBoolean());
|
||||||
|
}
|
||||||
|
return boolList;
|
||||||
|
default:
|
||||||
|
// Fallback: Liste von Objekten (z. B. wenn es sich um komplexe Objekte handelt)
|
||||||
|
var objectList = new List<object>();
|
||||||
|
foreach (var element in jsonArray.EnumerateArray())
|
||||||
|
{
|
||||||
|
objectList.Add(JsonSerializer.Deserialize<object>(element.GetRawText()));
|
||||||
|
}
|
||||||
|
return objectList;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Leeres Array als Liste von Objekten zurückgeben
|
||||||
|
return new List<object>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Write(Utf8JsonWriter writer, Dictionary<string, object> value, JsonSerializerOptions options)
|
||||||
|
{
|
||||||
|
writer.WriteStartObject();
|
||||||
|
|
||||||
|
foreach (var kvp in value)
|
||||||
|
{
|
||||||
|
writer.WritePropertyName(kvp.Key);
|
||||||
|
JsonSerializer.Serialize(writer, kvp.Value, kvp.Value?.GetType() ?? typeof(object), options);
|
||||||
|
}
|
||||||
|
|
||||||
|
writer.WriteEndObject();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
296
TelegramBotBase/States/Converter/JsonTypeConverter.cs
Normal file
296
TelegramBotBase/States/Converter/JsonTypeConverter.cs
Normal file
@ -0,0 +1,296 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics;
|
||||||
|
using System.Reflection;
|
||||||
|
using System.Text;
|
||||||
|
using System.Text.Json;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
|
namespace TelegramBotBase.States.Converter
|
||||||
|
{
|
||||||
|
public class JsonTypeConverter : JsonConverter<object>
|
||||||
|
{
|
||||||
|
|
||||||
|
public override object Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||||
|
{
|
||||||
|
using (JsonDocument doc = JsonDocument.ParseValue(ref reader))
|
||||||
|
{
|
||||||
|
return ParseElement(doc.RootElement, options, typeof(object));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private object ParseElement(JsonElement element, JsonSerializerOptions options, Type expected_type)
|
||||||
|
{
|
||||||
|
// Wenn das Element ein JSON-Objekt ist
|
||||||
|
if (element.ValueKind == JsonValueKind.Object)
|
||||||
|
{
|
||||||
|
// Wenn es einen $type enthält, dann den Typ dynamisch instanziieren
|
||||||
|
if (element.TryGetProperty("$type", out JsonElement typeElement))
|
||||||
|
{
|
||||||
|
string typeName = typeElement.GetString();
|
||||||
|
Type type = Type.GetType(typeName);
|
||||||
|
|
||||||
|
if (type == null)
|
||||||
|
throw new InvalidOperationException($"Typ '{typeName}' konnte nicht gefunden werden.");
|
||||||
|
|
||||||
|
// Liste verarbeiten, falls $values enthalten ist
|
||||||
|
if (element.TryGetProperty("$values", out JsonElement valuesElement))
|
||||||
|
{
|
||||||
|
return ParseArray(valuesElement, type);
|
||||||
|
}
|
||||||
|
else if (expected_type.IsGenericType && expected_type.GetGenericTypeDefinition() == typeof(Dictionary<,>))
|
||||||
|
{
|
||||||
|
var dictionary = new Dictionary<string, object>();
|
||||||
|
foreach (var property in element.EnumerateObject())
|
||||||
|
{
|
||||||
|
dictionary[property.Name] = ParseElement(property.Value, options, typeof(object));
|
||||||
|
}
|
||||||
|
|
||||||
|
return dictionary;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var instance = type.GetConstructor(new Type[] { }).Invoke(new object[] { });
|
||||||
|
var properties = type.GetProperties();
|
||||||
|
|
||||||
|
foreach (var p in properties)
|
||||||
|
{
|
||||||
|
if (!element.TryGetProperty(p.Name, out JsonElement value))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
switch (value.ValueKind)
|
||||||
|
{
|
||||||
|
case JsonValueKind.Number:
|
||||||
|
|
||||||
|
if (p.PropertyType == typeof(int))
|
||||||
|
{
|
||||||
|
if (value.TryGetInt32(out int i))
|
||||||
|
{
|
||||||
|
p.SetValue(instance, i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (p.PropertyType == typeof(long))
|
||||||
|
{
|
||||||
|
if (value.TryGetInt64(out long l))
|
||||||
|
{
|
||||||
|
p.SetValue(instance, l);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
case JsonValueKind.String:
|
||||||
|
|
||||||
|
p.SetValue(instance, value.ToString());
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
|
||||||
|
//if (p.PropertyType.IsGenericType && p.PropertyType.GetGenericTypeDefinition() == typeof(Dictionary<,>))
|
||||||
|
//{
|
||||||
|
// var dictionary = new Dictionary<string, object>();
|
||||||
|
// foreach (var property in element.EnumerateObject())
|
||||||
|
// {
|
||||||
|
// dictionary[property.Name] = ParseElement(property.Value, options, p.PropertyType);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// p.SetValue(instance, dictionary);
|
||||||
|
//}
|
||||||
|
//else
|
||||||
|
//{
|
||||||
|
|
||||||
|
var obj = ParseElement(value, options, p.PropertyType);
|
||||||
|
|
||||||
|
if (obj == null)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
p.SetValue(instance, obj);
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
//}
|
||||||
|
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return instance;
|
||||||
|
|
||||||
|
// Objekt dynamisch deserialisieren
|
||||||
|
//return JsonSerializer.Deserialize(element.GetRawText(), type, options);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Falls kein $type vorhanden ist, als Dictionary verarbeiten
|
||||||
|
var dictionary = new Dictionary<string, object>();
|
||||||
|
foreach (var property in element.EnumerateObject())
|
||||||
|
{
|
||||||
|
dictionary[property.Name] = ParseElement(property.Value, options, typeof(object));
|
||||||
|
}
|
||||||
|
return dictionary;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Wenn das Element ein Array ist
|
||||||
|
else if (element.ValueKind == JsonValueKind.Array)
|
||||||
|
{
|
||||||
|
return ParseArray(element, typeof(object));
|
||||||
|
}
|
||||||
|
// Primitive Typen (String, Number, Bool)
|
||||||
|
else if (element.ValueKind == JsonValueKind.Number)
|
||||||
|
{
|
||||||
|
|
||||||
|
if (expected_type == typeof(Int64))
|
||||||
|
{
|
||||||
|
if (element.TryGetInt64(out long l))
|
||||||
|
return l;
|
||||||
|
}
|
||||||
|
else if (expected_type == typeof(Int32))
|
||||||
|
{
|
||||||
|
if (element.TryGetInt32(out int i))
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (element.TryGetInt32(out int i2))
|
||||||
|
return i2;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
else if (element.ValueKind == JsonValueKind.String)
|
||||||
|
{
|
||||||
|
return element.ToString();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
|
||||||
|
#if NETCOREAPP3_1
|
||||||
|
return JsonSerializer.Deserialize(element.ToString(), typeof(object), options);
|
||||||
|
#else
|
||||||
|
return element.Deserialize<object>(options);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private object ParseElement(JsonElement element)
|
||||||
|
{
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private object ParseArray(JsonElement arrayElement, Type listType)
|
||||||
|
{
|
||||||
|
// Wenn der Typ eine generische Liste ist
|
||||||
|
if (listType.IsGenericType && listType.GetGenericTypeDefinition() == typeof(List<>))
|
||||||
|
{
|
||||||
|
Type elementType = listType.GetGenericArguments()[0];
|
||||||
|
var list = (IList)Activator.CreateInstance(typeof(List<>).MakeGenericType(elementType));
|
||||||
|
foreach (var item in arrayElement.EnumerateArray())
|
||||||
|
{
|
||||||
|
list.Add(ParseElement(item, new JsonSerializerOptions(), elementType));
|
||||||
|
}
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Standardmäßig als List<object>
|
||||||
|
var list = new List<object>();
|
||||||
|
foreach (var item in arrayElement.EnumerateArray())
|
||||||
|
{
|
||||||
|
list.Add(ParseElement(item, new JsonSerializerOptions(), typeof(object)));
|
||||||
|
}
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Write(Utf8JsonWriter writer, object value, JsonSerializerOptions options)
|
||||||
|
{
|
||||||
|
WriteWithTypeInfo(writer, value, options);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void WriteWithTypeInfo(Utf8JsonWriter writer, object value, JsonSerializerOptions options)
|
||||||
|
{
|
||||||
|
if (value == null)
|
||||||
|
{
|
||||||
|
writer.WriteNullValue();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Type type = value.GetType();
|
||||||
|
|
||||||
|
// Primitives und grundlegende Typen (int, string, bool, etc.)
|
||||||
|
if (type.IsPrimitive || type == typeof(string) || type == typeof(decimal))
|
||||||
|
{
|
||||||
|
JsonSerializer.Serialize(writer, value, type, options);
|
||||||
|
}
|
||||||
|
// Behandlung für Sammlungen
|
||||||
|
else if (value is IList list)
|
||||||
|
{
|
||||||
|
writer.WriteStartObject();
|
||||||
|
writer.WriteString("$type", type.AssemblyQualifiedName);
|
||||||
|
writer.WritePropertyName("$values");
|
||||||
|
writer.WriteStartArray();
|
||||||
|
|
||||||
|
foreach (var item in list)
|
||||||
|
{
|
||||||
|
WriteWithTypeInfo(writer, item, options);
|
||||||
|
}
|
||||||
|
|
||||||
|
writer.WriteEndArray();
|
||||||
|
writer.WriteEndObject();
|
||||||
|
}
|
||||||
|
// Behandlung für Dictionaries
|
||||||
|
else if (value is IDictionary dictionary)
|
||||||
|
{
|
||||||
|
writer.WriteStartObject();
|
||||||
|
writer.WriteString("$type", type.AssemblyQualifiedName);
|
||||||
|
|
||||||
|
foreach (DictionaryEntry entry in dictionary)
|
||||||
|
{
|
||||||
|
writer.WritePropertyName(entry.Key.ToString());
|
||||||
|
WriteWithTypeInfo(writer, entry.Value, options);
|
||||||
|
}
|
||||||
|
|
||||||
|
writer.WriteEndObject();
|
||||||
|
}
|
||||||
|
// Komplexe Objekte
|
||||||
|
else if (value is Enum e)
|
||||||
|
{
|
||||||
|
|
||||||
|
int eValue = (int)value;
|
||||||
|
|
||||||
|
writer.WriteNumberValue(eValue);
|
||||||
|
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
writer.WriteStartObject();
|
||||||
|
writer.WriteString("$type", type.AssemblyQualifiedName);
|
||||||
|
|
||||||
|
foreach (var property in type.GetProperties())
|
||||||
|
{
|
||||||
|
if (property.CanRead)
|
||||||
|
{
|
||||||
|
var propValue = property.GetValue(value);
|
||||||
|
writer.WritePropertyName(property.Name);
|
||||||
|
WriteWithTypeInfo(writer, propValue, options);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
writer.WriteEndObject();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -48,12 +48,24 @@ public class JsonStateMachine : IStateMachine
|
|||||||
{
|
{
|
||||||
var content = File.ReadAllText(FilePath);
|
var content = File.ReadAllText(FilePath);
|
||||||
|
|
||||||
var sc = JsonSerializer.Deserialize<StateContainer>(content);
|
var options = new JsonSerializerOptions
|
||||||
|
{
|
||||||
|
Converters = {
|
||||||
|
new Converter.JsonTypeConverter(),
|
||||||
|
//new Converter.DictionaryObjectJsonConverter(),
|
||||||
|
|
||||||
|
},
|
||||||
|
PropertyNameCaseInsensitive = true
|
||||||
|
};
|
||||||
|
|
||||||
|
var obj = JsonSerializer.Deserialize<object>(content, options) ;
|
||||||
|
var sc = obj as StateContainer;
|
||||||
|
|
||||||
return sc;
|
return sc;
|
||||||
}
|
}
|
||||||
catch
|
catch(Exception ex)
|
||||||
{
|
{
|
||||||
|
throw ex;
|
||||||
}
|
}
|
||||||
|
|
||||||
return new StateContainer();
|
return new StateContainer();
|
||||||
@ -73,15 +85,23 @@ public class JsonStateMachine : IStateMachine
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var content = JsonSerializer.Serialize(e.States, new JsonSerializerOptions
|
var options = new JsonSerializerOptions
|
||||||
{
|
{
|
||||||
WriteIndented = true,
|
WriteIndented = true,
|
||||||
});
|
Converters = {
|
||||||
|
new Converter.JsonTypeConverter(),
|
||||||
|
},
|
||||||
|
PropertyNameCaseInsensitive = true
|
||||||
|
};
|
||||||
|
|
||||||
|
var content = JsonSerializer.Serialize<object>(e.States, options);
|
||||||
|
|
||||||
File.WriteAllText(FilePath, content);
|
File.WriteAllText(FilePath, content);
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -49,7 +49,15 @@ public class SimpleJsonStateMachine : IStateMachine
|
|||||||
{
|
{
|
||||||
var content = File.ReadAllText(FilePath);
|
var content = File.ReadAllText(FilePath);
|
||||||
|
|
||||||
var sc = JsonSerializer.Deserialize<StateContainer>(content);
|
var options = new JsonSerializerOptions
|
||||||
|
{
|
||||||
|
Converters = {
|
||||||
|
new Converter.DictionaryObjectJsonConverter(),
|
||||||
|
new Converter.JsonTypeConverter()
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
var sc = JsonSerializer.Deserialize<StateContainer>(content, options);
|
||||||
|
|
||||||
return sc;
|
return sc;
|
||||||
}
|
}
|
||||||
@ -74,9 +82,16 @@ public class SimpleJsonStateMachine : IStateMachine
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var content = JsonSerializer.Serialize(e.States, new JsonSerializerOptions() {
|
var options = new JsonSerializerOptions
|
||||||
WriteIndented = true
|
{
|
||||||
});
|
WriteIndented = true,
|
||||||
|
Converters = {
|
||||||
|
new Converter.DictionaryObjectJsonConverter(),
|
||||||
|
new Converter.JsonTypeConverter()
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
var content = JsonSerializer.Serialize(e.States, options);
|
||||||
|
|
||||||
File.WriteAllText(FilePath, content);
|
File.WriteAllText(FilePath, content);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFrameworks>netstandard2.0;netcoreapp3.1;net6;net7</TargetFrameworks>
|
<TargetFrameworks>netcoreapp3.1;net6;net7;net8</TargetFrameworks>
|
||||||
<LangVersion>10</LangVersion>
|
<LangVersion>10</LangVersion>
|
||||||
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
|
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
|
||||||
<GeneratePackageOnBuild>False</GeneratePackageOnBuild>
|
<GeneratePackageOnBuild>False</GeneratePackageOnBuild>
|
||||||
@ -23,7 +23,7 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" />
|
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" />
|
||||||
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
|
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|
||||||
@ -57,7 +57,12 @@
|
|||||||
|
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Telegram.Bot" Version="22.2.0" />
|
<PackageReference Include="Telegram.Bot" Version="19.0.0" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\TelegramBotBase.SourceGenerators\TelegramBotBase.SourceGenerators.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" TargetFramework="netstandard2.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@ -14,12 +14,12 @@
|
|||||||
<dependencies>
|
<dependencies>
|
||||||
<group targetFramework=".NETFramework4.6.1">
|
<group targetFramework=".NETFramework4.6.1">
|
||||||
<dependency id="Newtonsoft.Json" version="13.0.1" exclude="Build,Analyzers" />
|
<dependency id="Newtonsoft.Json" version="13.0.1" exclude="Build,Analyzers" />
|
||||||
<dependency id="Telegram.Bot" version="22.2.0" exclude="Build,Analyzers" />
|
<dependency id="Telegram.Bot" version="19.0.0" exclude="Build,Analyzers" />
|
||||||
</group>
|
</group>
|
||||||
<group targetFramework=".NETStandard2.0">
|
<group targetFramework=".NETStandard2.0">
|
||||||
<dependency id="Newtonsoft.Json" version="13.0.1" exclude="Build,Analyzers" />
|
<dependency id="Newtonsoft.Json" version="13.0.1" exclude="Build,Analyzers" />
|
||||||
<dependency id="Telegram.Bot" version="22.2.0" exclude="Build,Analyzers" />
|
<dependency id="Telegram.Bot" version="19.0.0" exclude="Build,Analyzers" />
|
||||||
</group>
|
</group>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</metadata>
|
</metadata>
|
||||||
</package>
|
</package>
|
||||||
|
|||||||
@ -38,10 +38,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TelegramBotBase.Extensions.
|
|||||||
EndProject
|
EndProject
|
||||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TelegramBotBase.Extensions.Images.IronSoftware", "TelegramBotBase.Extensions.Images.IronSoftware\TelegramBotBase.Extensions.Images.IronSoftware.csproj", "{DC521A4C-7446-46F7-845B-AAF10EDCF8C6}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TelegramBotBase.Extensions.Images.IronSoftware", "TelegramBotBase.Extensions.Images.IronSoftware\TelegramBotBase.Extensions.Images.IronSoftware.csproj", "{DC521A4C-7446-46F7-845B-AAF10EDCF8C6}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Элементы решения", "Элементы решения", "{040F54FA-B51F-475F-89F8-2DD23CDC2989}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TelegramBotBase.SourceGenerators", "TelegramBotBase.SourceGenerators\TelegramBotBase.SourceGenerators.csproj", "{B78455D6-8AF2-459C-B56A-210DC89D7793}"
|
||||||
ProjectSection(SolutionItems) = preProject
|
EndProject
|
||||||
.gitea\workflows\TelegramBotFramework.nuget.yaml = .gitea\workflows\TelegramBotFramework.nuget.yaml
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TelegramBotBase.Extensions.Serializer.Legacy.NewtonsoftJson", "TelegramBotBase.Extensions.Serializer.Legacy.NewtonsoftJson\TelegramBotBase.Extensions.Serializer.Legacy.NewtonsoftJson.csproj", "{21C44B20-1ED8-4BE6-A629-C2EC737C06E0}"
|
||||||
EndProjectSection
|
|
||||||
EndProject
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
@ -101,6 +100,14 @@ Global
|
|||||||
{DC521A4C-7446-46F7-845B-AAF10EDCF8C6}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{DC521A4C-7446-46F7-845B-AAF10EDCF8C6}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{DC521A4C-7446-46F7-845B-AAF10EDCF8C6}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{DC521A4C-7446-46F7-845B-AAF10EDCF8C6}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{DC521A4C-7446-46F7-845B-AAF10EDCF8C6}.Release|Any CPU.Build.0 = Release|Any CPU
|
{DC521A4C-7446-46F7-845B-AAF10EDCF8C6}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{B78455D6-8AF2-459C-B56A-210DC89D7793}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{B78455D6-8AF2-459C-B56A-210DC89D7793}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{B78455D6-8AF2-459C-B56A-210DC89D7793}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{B78455D6-8AF2-459C-B56A-210DC89D7793}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{21C44B20-1ED8-4BE6-A629-C2EC737C06E0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{21C44B20-1ED8-4BE6-A629-C2EC737C06E0}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{21C44B20-1ED8-4BE6-A629-C2EC737C06E0}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{21C44B20-1ED8-4BE6-A629-C2EC737C06E0}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
@ -117,6 +124,7 @@ Global
|
|||||||
{689B16BC-200E-4C68-BB2E-8B209070849B} = {BFA71E3F-31C0-4FC1-A320-4DCF704768C5}
|
{689B16BC-200E-4C68-BB2E-8B209070849B} = {BFA71E3F-31C0-4FC1-A320-4DCF704768C5}
|
||||||
{7C55D9FF-7DC1-41D0-809C-469EBFA20992} = {E3193182-6FDA-4FA3-AD26-A487291E7681}
|
{7C55D9FF-7DC1-41D0-809C-469EBFA20992} = {E3193182-6FDA-4FA3-AD26-A487291E7681}
|
||||||
{DC521A4C-7446-46F7-845B-AAF10EDCF8C6} = {E3193182-6FDA-4FA3-AD26-A487291E7681}
|
{DC521A4C-7446-46F7-845B-AAF10EDCF8C6} = {E3193182-6FDA-4FA3-AD26-A487291E7681}
|
||||||
|
{21C44B20-1ED8-4BE6-A629-C2EC737C06E0} = {E3193182-6FDA-4FA3-AD26-A487291E7681}
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||||
SolutionGuid = {59CB40E1-9FA7-4867-A56F-4F418286F057}
|
SolutionGuid = {59CB40E1-9FA7-4867-A56F-4F418286F057}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user