diff --git a/TelegramBotBase.Extensions.Serializer.Legacy.NewtonsoftJson/BotBaseBuilderExtensions.cs b/TelegramBotBase.Extensions.Serializer.Legacy.NewtonsoftJson/BotBaseBuilderExtensions.cs new file mode 100644 index 0000000..77db456 --- /dev/null +++ b/TelegramBotBase.Extensions.Serializer.Legacy.NewtonsoftJson/BotBaseBuilderExtensions.cs @@ -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 + { + + /// + /// Using the complex version of .Net JSON, which can serialize all objects. + /// Saves in application directory. + /// + /// + /// + public static ILanguageSelectionStage UseNewtonsoftJson(this ISessionSerializationStage builder) + { + var path = Path.Combine(Directory.GetCurrentDirectory(), "states.json"); + + builder.UseNewtonsoftJson(path); + + return builder as BotBaseBuilder; + } + + /// + /// Using the complex version of .Net JSON, which can serialize all objects. + /// Saves in application directory. + /// + /// + /// + public static ILanguageSelectionStage UseNewtonsoftJson(this ISessionSerializationStage builder, String path) + { + var _stateMachine = new NewtonsoftJsonStateMachine(path); + + builder.UseSerialization(_stateMachine); + + return builder as BotBaseBuilder; + } + } +} \ No newline at end of file diff --git a/TelegramBotBase.Extensions.Serializer.Legacy.NewtonsoftJson/NewtonsoftJsonStateMachine.cs b/TelegramBotBase.Extensions.Serializer.Legacy.NewtonsoftJson/NewtonsoftJsonStateMachine.cs new file mode 100644 index 0000000..39a3a60 --- /dev/null +++ b/TelegramBotBase.Extensions.Serializer.Legacy.NewtonsoftJson/NewtonsoftJsonStateMachine.cs @@ -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 +{ + /// + /// Is used for all complex data types. Use if other default machines are not working. + /// + public class NewtonsoftJsonStateMachine : IStateMachine + { + /// + /// Will initialize the state machine. + /// + /// Path of the file and name where to save the session details. + /// + /// Type of Form which will be saved instead of Form which has + /// attribute declared. Needs to be subclass of + /// . + /// + /// Declares of the file could be overwritten. + 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(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 + { + } + } + } +} \ No newline at end of file diff --git a/TelegramBotBase.Extensions.Serializer.Legacy.NewtonsoftJson/README.md b/TelegramBotBase.Extensions.Serializer.Legacy.NewtonsoftJson/README.md new file mode 100644 index 0000000..23169e9 --- /dev/null +++ b/TelegramBotBase.Extensions.Serializer.Legacy.NewtonsoftJson/README.md @@ -0,0 +1,31 @@ +# TelegramBotBase.Extensions.Serializer.Legacy.NewtonsoftJson + +[![NuGet version (TelegramBotBase)](https://img.shields.io/nuget/v/TelegramBotBase.Extensions.Serializer.Legacy.NewtonsoftJson.svg?style=flat-square)](https://www.nuget.org/packages/TelegramBotBase.Extensions.Serializer.Legacy.NewtonsoftJson/) +[![Telegram chat](https://img.shields.io/badge/Support_Chat-Telegram-blue.svg?style=flat-square)](https://www.t.me/tgbotbase) + +[![License](https://img.shields.io/github/license/MajMcCloud/telegrambotframework.svg?style=flat-square&maxAge=2592000&label=License)](https://raw.githubusercontent.com/MajMcCloud/TelegramBotFramework/master/LICENCE.md) +[![Package Downloads](https://img.shields.io/nuget/dt/TelegramBotBase.Extensions.Serializer.Legacy.NewtonsoftJson.svg?style=flat-square&label=Package%20Downloads)](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() + .NoProxy() + .OnlyStart() + .UseNewtonsoftJson() + .UseEnglish() + .Build(); + +bot.Start(); +``` diff --git a/TelegramBotBase.Extensions.Serializer.Legacy.NewtonsoftJson/TelegramBotBase.Extensions.Serializer.Legacy.NewtonsoftJson.csproj b/TelegramBotBase.Extensions.Serializer.Legacy.NewtonsoftJson/TelegramBotBase.Extensions.Serializer.Legacy.NewtonsoftJson.csproj new file mode 100644 index 0000000..f647b56 --- /dev/null +++ b/TelegramBotBase.Extensions.Serializer.Legacy.NewtonsoftJson/TelegramBotBase.Extensions.Serializer.Legacy.NewtonsoftJson.csproj @@ -0,0 +1,27 @@ + + + + netstandard2.0;netcoreapp3.1;net6;net7;net8;net9 + True + https://github.com/MajMcCloud/TelegramBotFramework + https://github.com/MajMcCloud/TelegramBotFramework + MIT + true + snupkg + 1.0.1 + 1.0.1 + 1.0.1 + A session serializer for Newtonsoft Json. + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + diff --git a/TelegramBotFramework.sln b/TelegramBotFramework.sln index 2fee452..d848d6a 100644 --- a/TelegramBotFramework.sln +++ b/TelegramBotFramework.sln @@ -38,7 +38,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TelegramBotBase.Extensions. 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}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TelegramBotBase.SourceGenerators", "TelegramBotBase.SourceGenerators\TelegramBotBase.SourceGenerators.csproj", "{B78455D6-8AF2-459C-B56A-210DC89D7793}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TelegramBotBase.SourceGenerators", "TelegramBotBase.SourceGenerators\TelegramBotBase.SourceGenerators.csproj", "{B78455D6-8AF2-459C-B56A-210DC89D7793}" +EndProject +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}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -102,6 +104,10 @@ Global {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 GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -118,6 +124,7 @@ Global {689B16BC-200E-4C68-BB2E-8B209070849B} = {BFA71E3F-31C0-4FC1-A320-4DCF704768C5} {7C55D9FF-7DC1-41D0-809C-469EBFA20992} = {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 GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {59CB40E1-9FA7-4867-A56F-4F418286F057}