Skip to content

Commit

Permalink
Merge pull request #69 from MajMcCloud/development
Browse files Browse the repository at this point in the history
Integrate development branch into master (Version 6.6 RC)
  • Loading branch information
MajMcCloud authored Dec 14, 2024
2 parents 82e6d79 + df662d7 commit afe2862
Show file tree
Hide file tree
Showing 48 changed files with 16,874 additions and 80 deletions.
14 changes: 14 additions & 0 deletions Examples/FileWatcher/FileWatcher.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

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

<ItemGroup>
<PackageReference Include="TelegramBotBase" Version="6.5.1" />
</ItemGroup>

</Project>
20 changes: 20 additions & 0 deletions Examples/FileWatcher/Forms/Start.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TelegramBotBase.Base;
using TelegramBotBase.Form;

namespace FileWatcher.Forms
{
public class Start : FormBase
{

public override async Task Load(MessageResult message)
{

await Device.Send("I'm here !");
}
}
}
73 changes: 73 additions & 0 deletions Examples/FileWatcher/Model/Config.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

namespace FileWatcher.Model
{
public class Config
{

public String APIKey { get; set; } = "";

public String DirectoryToWatch { get; set; } = "";

public List<long> DeviceIds { get; set; } = new List<long>();

public static Config Load()
{
Config config = new Config();

var path = Path.Combine(Directory.GetCurrentDirectory(), "config.json");

try
{
if (!File.Exists(path))
{
config.Save();
}

var content = File.ReadAllText(path);

config = JsonSerializer.Deserialize<Config>(content);
}
catch
{

}


return config;
}

public void Save()
{
var path = Path.Combine(Directory.GetCurrentDirectory(), "config.json");

Save(path);
}

public void Save(String path)
{

try
{
if (File.Exists(path))
File.Delete(path);


var content = System.Text.Json.JsonSerializer.Serialize(this, new JsonSerializerOptions() { WriteIndented = true });

File.WriteAllText(path, content);
}
catch
{

}

}

}
}
124 changes: 124 additions & 0 deletions Examples/FileWatcher/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
using Telegram.Bot;
using TelegramBotBase.Builder;
using TelegramBotBase.Commands;

namespace FileWatcher
{
internal class Program
{
public static Model.Config Config { get; set; }

public static TelegramBotBase.BotBase Bot { get; set; }

static void Main(string[] args)
{

Config = Model.Config.Load();

if (string.IsNullOrEmpty(Config.APIKey))
{
Console.WriteLine("No API Key set");
return;
}

if (string.IsNullOrEmpty(Config.DirectoryToWatch))
{
Console.WriteLine("No directory set");
return;
}

FileSystemWatcher watcher = new FileSystemWatcher(Config.DirectoryToWatch);
watcher.IncludeSubdirectories = false;

Console.WriteLine($"Directory: {Config.DirectoryToWatch}");



Bot = BotBaseBuilder.Create()
.WithAPIKey(Config.APIKey)
.DefaultMessageLoop()
.WithStartForm<Forms.Start>()
.NoProxy()
.CustomCommands(a =>
{
a.Start("Starts the bot");
a.Add("myid", "Whats my id?");

})
.NoSerialization()
.UseGerman()
.UseSingleThread()
.Build();

Bot.BotCommand += Bot_BotCommand;

Bot.UploadBotCommands();

Bot.Start();

watcher.EnableRaisingEvents = true;

watcher.Created += Watcher_Created;
watcher.Changed += Watcher_Changed;
watcher.Renamed += Watcher_Renamed;

Console.WriteLine("Bot started.");


Console.ReadLine();

watcher.EnableRaisingEvents = false;

Bot.Stop();

}



private static async Task Bot_BotCommand(object sender, TelegramBotBase.Args.BotCommandEventArgs e)
{
switch (e.Command)
{
case "/myid":

await e.Device.Send($"Your ID is: {e.DeviceId}");

e.Handled = true;

break;


}
}

private static async void Watcher_Changed(object sender, FileSystemEventArgs e)
{
Console.WriteLine($"File '{e.Name}' changed");

foreach (var device in Config.DeviceIds)
{
await Bot.Client.TelegramClient.SendTextMessageAsync(device, $"File '{e.Name}' changed");
}
}

private static async void Watcher_Created(object sender, FileSystemEventArgs e)
{
Console.WriteLine($"File '{e.Name}' created");

foreach (var device in Config.DeviceIds)
{
await Bot.Client.TelegramClient.SendTextMessageAsync(device, $"File '{e.Name}' created");
}
}

private static async void Watcher_Renamed(object sender, RenamedEventArgs e)
{
Console.WriteLine($"File '{e.Name}' renamed");

foreach (var device in Config.DeviceIds)
{
await Bot.Client.TelegramClient.SendTextMessageAsync(device, $"File '{e.Name}' renamed");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Threading.Tasks;
using Telegram.Bot.Types;
using TelegramBotBase.Form;
using TelegramBotBase.Interfaces;
using TelegramBotBase.Sessions;
using static IronSoftware.Drawing.AnyBitmap;
using SKImage = SixLabors.ImageSharp.Image;
Expand Down Expand Up @@ -37,7 +38,7 @@ public static async Task<Stream> ToStream(this SKImage image)
/// <param name="replyTo"></param>
/// <param name="disableNotification"></param>
/// <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,
bool disableNotification = false)
{
Expand All @@ -58,7 +59,7 @@ public static async Task<Message> SendPhoto(this DeviceSession session, AnyBitma
/// <param name="replyTo"></param>
/// <param name="disableNotification"></param>
/// <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,
bool disableNotification = false)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<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>
<PackageProjectUrl>https://github.com/MajMcCloud/TelegramBotFramework/tree/development/TelegramBotBase.Extensions.Images.IronSoftware</PackageProjectUrl>
<Copyright>MIT</Copyright>
Expand All @@ -19,11 +19,14 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="TelegramBotBase" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
<Folder Include="Properties\" />
</ItemGroup>

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

</Project>
6 changes: 3 additions & 3 deletions TelegramBotBase.Extensions.Images/ImageExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Threading.Tasks;
using Telegram.Bot.Types;
using TelegramBotBase.Form;
using TelegramBotBase.Sessions;
using TelegramBotBase.Interfaces;

namespace TelegramBotBase.Extensions.Images
{
Expand All @@ -27,7 +27,7 @@ public static Stream ToStream(this Image image, ImageFormat format)
/// <param name="replyTo"></param>
/// <param name="disableNotification"></param>
/// <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,
bool disableNotification = false)
{
Expand All @@ -48,7 +48,7 @@ public static async Task<Message> SendPhoto(this DeviceSession session, Image im
/// <param name="replyTo"></param>
/// <param name="disableNotification"></param>
/// <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,
bool disableNotification = false)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<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>
<PackageProjectUrl>https://github.com/MajMcCloud/TelegramBotFramework/tree/development/TelegramBotBase.Extensions.Images</PackageProjectUrl>
<Copyright>MIT</Copyright>
Expand All @@ -21,11 +21,14 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="System.Drawing.Common" Version="6.0.0" />
<PackageReference Include="TelegramBotBase" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
<Folder Include="Properties\" />
</ItemGroup>

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

</Project>
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
Loading

0 comments on commit afe2862

Please sign in to comment.