Skip to content

Commit afe2862

Browse files
authored
Merge pull request #69 from MajMcCloud/development
Integrate development branch into master (Version 6.6 RC)
2 parents 82e6d79 + df662d7 commit afe2862

File tree

48 files changed

+16874
-80
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+16874
-80
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="TelegramBotBase" Version="6.5.1" />
12+
</ItemGroup>
13+
14+
</Project>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using TelegramBotBase.Base;
7+
using TelegramBotBase.Form;
8+
9+
namespace FileWatcher.Forms
10+
{
11+
public class Start : FormBase
12+
{
13+
14+
public override async Task Load(MessageResult message)
15+
{
16+
17+
await Device.Send("I'm here !");
18+
}
19+
}
20+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Text.Json;
6+
using System.Threading.Tasks;
7+
8+
namespace FileWatcher.Model
9+
{
10+
public class Config
11+
{
12+
13+
public String APIKey { get; set; } = "";
14+
15+
public String DirectoryToWatch { get; set; } = "";
16+
17+
public List<long> DeviceIds { get; set; } = new List<long>();
18+
19+
public static Config Load()
20+
{
21+
Config config = new Config();
22+
23+
var path = Path.Combine(Directory.GetCurrentDirectory(), "config.json");
24+
25+
try
26+
{
27+
if (!File.Exists(path))
28+
{
29+
config.Save();
30+
}
31+
32+
var content = File.ReadAllText(path);
33+
34+
config = JsonSerializer.Deserialize<Config>(content);
35+
}
36+
catch
37+
{
38+
39+
}
40+
41+
42+
return config;
43+
}
44+
45+
public void Save()
46+
{
47+
var path = Path.Combine(Directory.GetCurrentDirectory(), "config.json");
48+
49+
Save(path);
50+
}
51+
52+
public void Save(String path)
53+
{
54+
55+
try
56+
{
57+
if (File.Exists(path))
58+
File.Delete(path);
59+
60+
61+
var content = System.Text.Json.JsonSerializer.Serialize(this, new JsonSerializerOptions() { WriteIndented = true });
62+
63+
File.WriteAllText(path, content);
64+
}
65+
catch
66+
{
67+
68+
}
69+
70+
}
71+
72+
}
73+
}

Examples/FileWatcher/Program.cs

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
using Telegram.Bot;
2+
using TelegramBotBase.Builder;
3+
using TelegramBotBase.Commands;
4+
5+
namespace FileWatcher
6+
{
7+
internal class Program
8+
{
9+
public static Model.Config Config { get; set; }
10+
11+
public static TelegramBotBase.BotBase Bot { get; set; }
12+
13+
static void Main(string[] args)
14+
{
15+
16+
Config = Model.Config.Load();
17+
18+
if (string.IsNullOrEmpty(Config.APIKey))
19+
{
20+
Console.WriteLine("No API Key set");
21+
return;
22+
}
23+
24+
if (string.IsNullOrEmpty(Config.DirectoryToWatch))
25+
{
26+
Console.WriteLine("No directory set");
27+
return;
28+
}
29+
30+
FileSystemWatcher watcher = new FileSystemWatcher(Config.DirectoryToWatch);
31+
watcher.IncludeSubdirectories = false;
32+
33+
Console.WriteLine($"Directory: {Config.DirectoryToWatch}");
34+
35+
36+
37+
Bot = BotBaseBuilder.Create()
38+
.WithAPIKey(Config.APIKey)
39+
.DefaultMessageLoop()
40+
.WithStartForm<Forms.Start>()
41+
.NoProxy()
42+
.CustomCommands(a =>
43+
{
44+
a.Start("Starts the bot");
45+
a.Add("myid", "Whats my id?");
46+
47+
})
48+
.NoSerialization()
49+
.UseGerman()
50+
.UseSingleThread()
51+
.Build();
52+
53+
Bot.BotCommand += Bot_BotCommand;
54+
55+
Bot.UploadBotCommands();
56+
57+
Bot.Start();
58+
59+
watcher.EnableRaisingEvents = true;
60+
61+
watcher.Created += Watcher_Created;
62+
watcher.Changed += Watcher_Changed;
63+
watcher.Renamed += Watcher_Renamed;
64+
65+
Console.WriteLine("Bot started.");
66+
67+
68+
Console.ReadLine();
69+
70+
watcher.EnableRaisingEvents = false;
71+
72+
Bot.Stop();
73+
74+
}
75+
76+
77+
78+
private static async Task Bot_BotCommand(object sender, TelegramBotBase.Args.BotCommandEventArgs e)
79+
{
80+
switch (e.Command)
81+
{
82+
case "/myid":
83+
84+
await e.Device.Send($"Your ID is: {e.DeviceId}");
85+
86+
e.Handled = true;
87+
88+
break;
89+
90+
91+
}
92+
}
93+
94+
private static async void Watcher_Changed(object sender, FileSystemEventArgs e)
95+
{
96+
Console.WriteLine($"File '{e.Name}' changed");
97+
98+
foreach (var device in Config.DeviceIds)
99+
{
100+
await Bot.Client.TelegramClient.SendTextMessageAsync(device, $"File '{e.Name}' changed");
101+
}
102+
}
103+
104+
private static async void Watcher_Created(object sender, FileSystemEventArgs e)
105+
{
106+
Console.WriteLine($"File '{e.Name}' created");
107+
108+
foreach (var device in Config.DeviceIds)
109+
{
110+
await Bot.Client.TelegramClient.SendTextMessageAsync(device, $"File '{e.Name}' created");
111+
}
112+
}
113+
114+
private static async void Watcher_Renamed(object sender, RenamedEventArgs e)
115+
{
116+
Console.WriteLine($"File '{e.Name}' renamed");
117+
118+
foreach (var device in Config.DeviceIds)
119+
{
120+
await Bot.Client.TelegramClient.SendTextMessageAsync(device, $"File '{e.Name}' renamed");
121+
}
122+
}
123+
}
124+
}

TelegramBotBase.Extensions.Images.IronSoftware/ImageExtensions.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Threading.Tasks;
55
using Telegram.Bot.Types;
66
using TelegramBotBase.Form;
7+
using TelegramBotBase.Interfaces;
78
using TelegramBotBase.Sessions;
89
using static IronSoftware.Drawing.AnyBitmap;
910
using SKImage = SixLabors.ImageSharp.Image;
@@ -37,7 +38,7 @@ public static async Task<Stream> ToStream(this SKImage image)
3738
/// <param name="replyTo"></param>
3839
/// <param name="disableNotification"></param>
3940
/// <returns></returns>
40-
public static async Task<Message> SendPhoto(this DeviceSession session, AnyBitmap image, string name,
41+
public static async Task<Message> SendPhoto(this IDeviceSession session, AnyBitmap image, string name,
4142
string caption, ButtonForm buttons = null, int replyTo = 0,
4243
bool disableNotification = false)
4344
{
@@ -58,7 +59,7 @@ public static async Task<Message> SendPhoto(this DeviceSession session, AnyBitma
5859
/// <param name="replyTo"></param>
5960
/// <param name="disableNotification"></param>
6061
/// <returns></returns>
61-
public static async Task<Message> SendPhoto(this DeviceSession session, SKImage image, string name,
62+
public static async Task<Message> SendPhoto(this IDeviceSession session, SKImage image, string name,
6263
string caption, ButtonForm buttons = null, int replyTo = 0,
6364
bool disableNotification = false)
6465
{

TelegramBotBase.Extensions.Images.IronSoftware/TelegramBotBase.Extensions.Images.IronSoftware.csproj

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>netstandard2.0;netcoreapp3.1;net6</TargetFrameworks>
4+
<TargetFrameworks>netcoreapp3.1;net6</TargetFrameworks>
55
<RepositoryUrl>https://github.com/MajMcCloud/TelegramBotFramework/tree/development/TelegramBotBase.Extensions.Images.IronSoftware</RepositoryUrl>
66
<PackageProjectUrl>https://github.com/MajMcCloud/TelegramBotFramework/tree/development/TelegramBotBase.Extensions.Images.IronSoftware</PackageProjectUrl>
77
<Copyright>MIT</Copyright>
@@ -19,11 +19,14 @@
1919
<PrivateAssets>all</PrivateAssets>
2020
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2121
</PackageReference>
22-
<PackageReference Include="TelegramBotBase" Version="6.0.0" />
2322
</ItemGroup>
2423

2524
<ItemGroup>
2625
<Folder Include="Properties\" />
2726
</ItemGroup>
2827

28+
<ItemGroup>
29+
<ProjectReference Include="..\TelegramBotBase\TelegramBotBase.csproj" />
30+
</ItemGroup>
31+
2932
</Project>

TelegramBotBase.Extensions.Images/ImageExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using System.Threading.Tasks;
55
using Telegram.Bot.Types;
66
using TelegramBotBase.Form;
7-
using TelegramBotBase.Sessions;
7+
using TelegramBotBase.Interfaces;
88

99
namespace TelegramBotBase.Extensions.Images
1010
{
@@ -27,7 +27,7 @@ public static Stream ToStream(this Image image, ImageFormat format)
2727
/// <param name="replyTo"></param>
2828
/// <param name="disableNotification"></param>
2929
/// <returns></returns>
30-
public static async Task<Message> SendPhoto(this DeviceSession session, Image image, string name,
30+
public static async Task<Message> SendPhoto(this IDeviceSession session, Image image, string name,
3131
string caption, ButtonForm buttons = null, int replyTo = 0,
3232
bool disableNotification = false)
3333
{
@@ -48,7 +48,7 @@ public static async Task<Message> SendPhoto(this DeviceSession session, Image im
4848
/// <param name="replyTo"></param>
4949
/// <param name="disableNotification"></param>
5050
/// <returns></returns>
51-
public static async Task<Message> SendPhoto(this DeviceSession session, Bitmap image, string name,
51+
public static async Task<Message> SendPhoto(this IDeviceSession session, Bitmap image, string name,
5252
string caption, ButtonForm buttons = null, int replyTo = 0,
5353
bool disableNotification = false)
5454
{

TelegramBotBase.Extensions.Images/TelegramBotBase.Extensions.Images.csproj

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>netstandard2.0;netcoreapp3.1;net6</TargetFrameworks>
4+
<TargetFrameworks>netcoreapp3.1;net6;net7;net8</TargetFrameworks>
55
<RepositoryUrl>https://github.com/MajMcCloud/TelegramBotFramework/tree/development/TelegramBotBase.Extensions.Images</RepositoryUrl>
66
<PackageProjectUrl>https://github.com/MajMcCloud/TelegramBotFramework/tree/development/TelegramBotBase.Extensions.Images</PackageProjectUrl>
77
<Copyright>MIT</Copyright>
@@ -21,11 +21,14 @@
2121
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2222
</PackageReference>
2323
<PackageReference Include="System.Drawing.Common" Version="6.0.0" />
24-
<PackageReference Include="TelegramBotBase" Version="6.0.0" />
2524
</ItemGroup>
2625

2726
<ItemGroup>
2827
<Folder Include="Properties\" />
2928
</ItemGroup>
3029

30+
<ItemGroup>
31+
<ProjectReference Include="..\TelegramBotBase\TelegramBotBase.csproj" />
32+
</ItemGroup>
33+
3134
</Project>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
using System.IO;
3+
using TelegramBotBase.Builder;
4+
using TelegramBotBase.Builder.Interfaces;
5+
6+
namespace TelegramBotBase.Extensions.Serializer.Legacy.NewtonsoftJson
7+
{
8+
public static class BotBaseBuilderExtensions
9+
{
10+
11+
/// <summary>
12+
/// Using the complex version of .Net JSON, which can serialize all objects.
13+
/// Saves in application directory.
14+
/// </summary>
15+
/// <param name="path"></param>
16+
/// <returns></returns>
17+
public static ILanguageSelectionStage UseNewtonsoftJson(this ISessionSerializationStage builder)
18+
{
19+
var path = Path.Combine(Directory.GetCurrentDirectory(), "states.json");
20+
21+
builder.UseNewtonsoftJson(path);
22+
23+
return builder as BotBaseBuilder;
24+
}
25+
26+
/// <summary>
27+
/// Using the complex version of .Net JSON, which can serialize all objects.
28+
/// Saves in application directory.
29+
/// </summary>
30+
/// <param name="path"></param>
31+
/// <returns></returns>
32+
public static ILanguageSelectionStage UseNewtonsoftJson(this ISessionSerializationStage builder, String path)
33+
{
34+
var _stateMachine = new NewtonsoftJsonStateMachine(path);
35+
36+
builder.UseSerialization(_stateMachine);
37+
38+
return builder as BotBaseBuilder;
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)