Skip to content

Commit fe65083

Browse files
authored
Merge pull request #75 from MajMcCloud/development
Migrating latest developments to the master branch
2 parents ac5881c + 2ca96c7 commit fe65083

File tree

83 files changed

+29277
-12372
lines changed

Some content is hidden

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

83 files changed

+29277
-12372
lines changed

Examples/FileWatcher/Model/Config.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Linq;
44
using System.Text;
55
using System.Text.Json;
6+
using System.Text.Json.Serialization;
67
using System.Threading.Tasks;
78

89
namespace FileWatcher.Model
@@ -14,8 +15,19 @@ public class Config
1415

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

18+
public bool ListenForCommands { get; set; } = true;
19+
1720
public List<long> DeviceIds { get; set; } = new List<long>();
1821

22+
public string Filter { get; set; } = "*.*";
23+
24+
public List<string> FilesToExclude { get; set; } = new List<string>() { "anything.txt" , "others.txt" };
25+
26+
public string MessageTemplate { get; set; } = "File '%filename%' %action%";
27+
28+
public const string FilenamePlaceholder = "%filename%";
29+
public const string ActionPlaceholder = "%action%";
30+
1931
public static Config Load()
2032
{
2133
Config config = new Config();
@@ -29,7 +41,7 @@ public static Config Load()
2941
config.Save();
3042
}
3143

32-
var content = File.ReadAllText(path);
44+
var content = File.ReadAllText(path, Encoding.UTF8);
3345

3446
config = JsonSerializer.Deserialize<Config>(content);
3547
}
@@ -60,7 +72,7 @@ public void Save(String path)
6072

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

63-
File.WriteAllText(path, content);
75+
File.WriteAllText(path, content, Encoding.UTF8);
6476
}
6577
catch
6678
{

Examples/FileWatcher/Program.cs

Lines changed: 119 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ static void Main(string[] args)
1818
if (string.IsNullOrEmpty(Config.APIKey))
1919
{
2020
Console.WriteLine("No API Key set");
21-
return;
2221
}
2322

2423
if (string.IsNullOrEmpty(Config.DirectoryToWatch))
@@ -27,34 +26,56 @@ static void Main(string[] args)
2726
return;
2827
}
2928

30-
FileSystemWatcher watcher = new FileSystemWatcher(Config.DirectoryToWatch);
31-
watcher.IncludeSubdirectories = false;
32-
33-
Console.WriteLine($"Directory: {Config.DirectoryToWatch}");
34-
35-
29+
if (Config.FilesToExclude.Count > 0)
30+
{
31+
Console.WriteLine("Files to exclude: " + string.Join(',', Config.FilesToExclude));
32+
}
3633

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?");
34+
FileSystemWatcher watcher = null;
4635

47-
})
48-
.NoSerialization()
49-
.UseGerman()
50-
.UseSingleThread()
51-
.Build();
36+
if (string.IsNullOrEmpty(Config.Filter))
37+
{
38+
watcher = new FileSystemWatcher(Config.DirectoryToWatch);
39+
Console.WriteLine("No filter set, using default *.*");
40+
}
41+
else
42+
{
43+
watcher = new FileSystemWatcher(Config.DirectoryToWatch, Config.Filter);
44+
Console.WriteLine($"Filter: {Config.Filter}");
45+
}
5246

53-
Bot.BotCommand += Bot_BotCommand;
47+
watcher.IncludeSubdirectories = false;
5448

55-
Bot.UploadBotCommands();
49+
Console.WriteLine($"Directory: {Config.DirectoryToWatch}");
5650

57-
Bot.Start();
51+
//start file watcher even without api key for testing
52+
if (!string.IsNullOrEmpty(Config.APIKey))
53+
{
54+
Bot = BotBaseBuilder.Create()
55+
.WithAPIKey(Config.APIKey)
56+
.DefaultMessageLoop()
57+
.WithStartForm<Forms.Start>()
58+
.NoProxy()
59+
.CustomCommands(a =>
60+
{
61+
a.Start("Starts the bot");
62+
a.Add("myid", "Whats my id?");
63+
64+
})
65+
.NoSerialization()
66+
.UseGerman()
67+
.UseSingleThread()
68+
.Build();
69+
70+
Bot.BotCommand += Bot_BotCommand;
71+
72+
Bot.UploadBotCommands();
73+
74+
if (Config.ListenForCommands)
75+
{
76+
Bot.Start();
77+
}
78+
}
5879

5980
watcher.EnableRaisingEvents = true;
6081

@@ -69,8 +90,10 @@ static void Main(string[] args)
6990

7091
watcher.EnableRaisingEvents = false;
7192

72-
Bot.Stop();
73-
93+
if (Config.ListenForCommands)
94+
{
95+
Bot?.Stop();
96+
}
7497
}
7598

7699

@@ -93,31 +116,95 @@ private static async Task Bot_BotCommand(object sender, TelegramBotBase.Args.Bot
93116

94117
private static async void Watcher_Changed(object sender, FileSystemEventArgs e)
95118
{
96-
Console.WriteLine($"File '{e.Name}' changed");
119+
if (Config.FilesToExclude.Count > 0)
120+
{
121+
var fn = Path.GetFileName(e.Name);
122+
123+
if (Config.FilesToExclude.Contains(fn))
124+
return;
125+
}
126+
127+
String s = Config.MessageTemplate.Replace(Model.Config.FilenamePlaceholder, e.Name)
128+
.Replace(Model.Config.ActionPlaceholder, e.ChangeType.ToString());
129+
130+
Console.WriteLine(s);
131+
132+
if (Bot == null)
133+
return;
97134

98135
foreach (var device in Config.DeviceIds)
99136
{
100-
await Bot.Client.TelegramClient.SendTextMessageAsync(device, $"File '{e.Name}' changed");
137+
try
138+
{
139+
await Bot.Client.TelegramClient.SendTextMessageAsync(device, s);
140+
}
141+
catch
142+
{
143+
144+
}
101145
}
102146
}
103147

104148
private static async void Watcher_Created(object sender, FileSystemEventArgs e)
105149
{
106-
Console.WriteLine($"File '{e.Name}' created");
150+
if (Config.FilesToExclude.Count > 0)
151+
{
152+
var fn = Path.GetFileName(e.Name);
153+
154+
if (Config.FilesToExclude.Contains(fn))
155+
return;
156+
}
157+
158+
String s = Config.MessageTemplate.Replace(Model.Config.FilenamePlaceholder, e.Name)
159+
.Replace(Model.Config.ActionPlaceholder, e.ChangeType.ToString());
160+
161+
Console.WriteLine(s);
162+
163+
if (Bot == null)
164+
return;
107165

108166
foreach (var device in Config.DeviceIds)
109167
{
110-
await Bot.Client.TelegramClient.SendTextMessageAsync(device, $"File '{e.Name}' created");
168+
try
169+
{
170+
await Bot.Client.TelegramClient.SendTextMessageAsync(device, s);
171+
}
172+
catch
173+
{
174+
175+
}
111176
}
112177
}
113178

114179
private static async void Watcher_Renamed(object sender, RenamedEventArgs e)
115180
{
116-
Console.WriteLine($"File '{e.Name}' renamed");
181+
if (Config.FilesToExclude.Count > 0)
182+
{
183+
var fn = Path.GetFileName(e.Name);
184+
185+
if (Config.FilesToExclude.Contains(fn))
186+
return;
187+
}
188+
189+
String s = Config.MessageTemplate.Replace(Model.Config.FilenamePlaceholder, e.Name)
190+
.Replace(Model.Config.ActionPlaceholder, e.ChangeType.ToString());
191+
192+
Console.WriteLine(s);
193+
194+
if (Bot == null)
195+
return;
117196

118197
foreach (var device in Config.DeviceIds)
119198
{
120-
await Bot.Client.TelegramClient.SendTextMessageAsync(device, $"File '{e.Name}' renamed");
199+
try
200+
{
201+
await Bot.Client.TelegramClient.SendTextMessageAsync(device, s);
202+
}
203+
catch
204+
{
205+
206+
}
207+
121208
}
122209
}
123210
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using TelegramBotBase.Base;
2+
using TelegramBotBase.Form;
3+
4+
internal sealed class StartForm : FormBase
5+
{
6+
public override async Task PreLoad(MessageResult message)
7+
{
8+
await this.Device.Send("PreLoad");
9+
10+
await Task.Delay(200);
11+
}
12+
13+
public override async Task Load(MessageResult message)
14+
{
15+
await this.Device.Send("Load");
16+
17+
await Task.Delay(200);
18+
}
19+
20+
public override async Task Edited(MessageResult message)
21+
{
22+
await this.Device.Send("Edited");
23+
24+
await Task.Delay(200);
25+
}
26+
27+
public override async Task Action(MessageResult message)
28+
{
29+
await this.Device.Send("Action");
30+
31+
await Task.Delay(200);
32+
}
33+
34+
public override async Task SentData(DataResult data)
35+
{
36+
await this.Device.Send("SentData");
37+
38+
await Task.Delay(200);
39+
}
40+
41+
public override async Task Render(MessageResult message)
42+
{
43+
await this.Device.Send("Render");
44+
45+
await Task.Delay(200);
46+
}
47+
}
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>net7.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<ProjectReference Include="..\..\TelegramBotBase\TelegramBotBase.csproj" />
12+
</ItemGroup>
13+
14+
</Project>

0 commit comments

Comments
 (0)