-
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #69 from MajMcCloud/development
Integrate development branch into master (Version 6.6 RC)
- Loading branch information
Showing
48 changed files
with
16,874 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 !"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
|
||
} | ||
|
||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
TelegramBotBase.Extensions.Serializer.Legacy.NewtonsoftJson/BotBaseBuilderExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
Oops, something went wrong.