From 382ae2de2d04933fa9ef5e979532c5d1f74bd0e9 Mon Sep 17 00:00:00 2001 From: FlorianDahn Date: Tue, 10 Mar 2020 01:24:09 +0100 Subject: [PATCH] - adding example project for Bot to start predefined system commands --- .../SystemCommandsBot.csproj | 16 ++ .../SystemCommandsBot/commands/Commando.cs | 30 +++ Examples/SystemCommandsBot/config/Config.cs | 92 ++++++++++ Examples/SystemCommandsBot/forms/CmdForm.cs | 172 ++++++++++++++++++ Examples/SystemCommandsBot/forms/StartForm.cs | 42 +++++ TelegramBotFramework.sln | 11 ++ 6 files changed, 363 insertions(+) create mode 100644 Examples/SystemCommandsBot/SystemCommandsBot.csproj create mode 100644 Examples/SystemCommandsBot/commands/Commando.cs create mode 100644 Examples/SystemCommandsBot/config/Config.cs create mode 100644 Examples/SystemCommandsBot/forms/CmdForm.cs create mode 100644 Examples/SystemCommandsBot/forms/StartForm.cs diff --git a/Examples/SystemCommandsBot/SystemCommandsBot.csproj b/Examples/SystemCommandsBot/SystemCommandsBot.csproj new file mode 100644 index 0000000..23bd127 --- /dev/null +++ b/Examples/SystemCommandsBot/SystemCommandsBot.csproj @@ -0,0 +1,16 @@ + + + + Exe + netcoreapp3.1 + + + + + + + + + + + diff --git a/Examples/SystemCommandsBot/commands/Commando.cs b/Examples/SystemCommandsBot/commands/Commando.cs new file mode 100644 index 0000000..97d6554 --- /dev/null +++ b/Examples/SystemCommandsBot/commands/Commando.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; + +namespace SystemCommandsBot.commands +{ + public class Commando + { + public int ID { get; set; } + + public String Title { get; set; } + + public String ShellCmd { get; set; } + + public bool Enabled { get; set; } = true; + + public String Action { get; set; } + + public bool UseShell { get; set; } = true; + + + public int? MaxInstances { get; set; } + + public String ProcName + { + get;set; + } + } +} diff --git a/Examples/SystemCommandsBot/config/Config.cs b/Examples/SystemCommandsBot/config/Config.cs new file mode 100644 index 0000000..8300806 --- /dev/null +++ b/Examples/SystemCommandsBot/config/Config.cs @@ -0,0 +1,92 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; + +namespace SystemCommandsBot.config +{ + public class Config + { + public String Password { get; set; } + + public String ApiKey { get; set; } + + public List Commandos { get; set; } + + + public Config() + { + this.Commandos = new List(); + } + + public void loadDefaultValues() + { + this.ApiKey = ""; + this.Commandos.Add(new commands.Commando() { ID = 0, Enabled = true, Title = "Test Befehl", ShellCmd = "explorer.exe", Action = "start", MaxInstances = 2 }); + } + + + public static Config load() + { + try + { + return load(AppContext.BaseDirectory + "config\\default.cfg"); + + + } + catch(Exception ex) + { + Console.WriteLine(ex.Message); + } + return null; + } + + + public static Config load(String path) + { + try + { + var cfg = Newtonsoft.Json.JsonConvert.DeserializeObject(File.ReadAllText(path)) as Config; + return cfg; + } + catch (DirectoryNotFoundException ex) + { + DirectoryInfo di = new DirectoryInfo(path); + + if (!Directory.Exists(di.Parent.FullName)) + { + Directory.CreateDirectory(di.Parent.FullName); + } + + var cfg = new Config(); + cfg.loadDefaultValues(); + cfg.save(path); + return cfg; + } + catch (FileNotFoundException ex) + { + var cfg = new Config(); + cfg.loadDefaultValues(); + cfg.save(path); + return cfg; + } + catch(Exception ex) + { + Console.WriteLine(ex.Message); + } + return null; + } + + public void save(String path) + { + try + { + File.WriteAllText(path, Newtonsoft.Json.JsonConvert.SerializeObject(this)); + } + catch + { + + } + } + } +} diff --git a/Examples/SystemCommandsBot/forms/CmdForm.cs b/Examples/SystemCommandsBot/forms/CmdForm.cs new file mode 100644 index 0000000..85a90ce --- /dev/null +++ b/Examples/SystemCommandsBot/forms/CmdForm.cs @@ -0,0 +1,172 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TelegramBotBase.Base; +using TelegramBotBase.Form; + +namespace SystemCommandsBot.forms +{ + public class CmdForm : TelegramBotBase.Form.AutoCleanForm + { + public DateTime ExpiresAt { get; set; } + + public int? MessageId { get; set; } + + public override async Task Load(MessageResult message) + { + + } + + public override async Task Action(MessageResult message) + { + var btn = message.RawData; + + int id = -1; + + if (!int.TryParse(btn, out id)) + { + + return; + } + + var cmd = Program.BotConfig.Commandos.Where(a => a.Enabled && a.ID == id).FirstOrDefault(); + if (cmd == null) + { + await this.Device.Send("Cmd nicht verfügbar."); + return; + } + + message.Handled = true; + + switch (cmd.Action) + { + case "start": + + FileInfo fi = new FileInfo(cmd.ShellCmd); + + if (cmd.MaxInstances != null && cmd.MaxInstances >= 0) + { + + if (Process.GetProcessesByName(cmd.ProcName).Count() >= cmd.MaxInstances) + { + await this.Device.Send("Anwendung läuft bereits."); + await message.ConfirmAction("Anwendung läuft bereits."); + + return; + } + + } + + ProcessStartInfo psi = new ProcessStartInfo(); + psi.FileName = cmd.ShellCmd; + psi.WorkingDirectory = fi.DirectoryName; + psi.UseShellExecute = cmd.UseShell; + + Process.Start(psi); + + await this.Device.Send(fi.Name + " wurde gestarted."); + + await message.ConfirmAction(fi.Name + " wurde gestarted."); + + break; + + case "kill": + + FileInfo fi2 = new FileInfo(cmd.ShellCmd); + + String pros = fi2.Name.Replace(fi2.Extension, ""); + + var proc = Process.GetProcessesByName(pros).ToList(); + + foreach (var p in proc) + { + try + { + p.Kill(); + } + catch + { + + } + } + + await this.Device.Send(fi2.Name + " wurde beendet."); + + await message.ConfirmAction(fi2.Name + " wurde beendet."); + + break; + + case "restart": + + FileInfo fi3 = new FileInfo(cmd.ShellCmd); + + String pros2 = fi3.Name.Replace(fi3.Extension, ""); + + var proc2 = Process.GetProcessesByName(pros2).ToList(); + + foreach (var p in proc2) + { + try + { + p.Kill(); + } + catch + { + + } + } + + FileInfo fi4 = new FileInfo(cmd.ShellCmd); + + ProcessStartInfo psi2 = new ProcessStartInfo(); + psi2.FileName = cmd.ShellCmd; + psi2.WorkingDirectory = fi4.DirectoryName; + psi2.FileName = cmd.ShellCmd; + psi2.UseShellExecute = cmd.UseShell; + + Process.Start(psi2); + + await this.Device.Send(fi3.Name + " wurde neugestarted."); + await message.ConfirmAction(fi3.Name + " wurde neugestarted."); + + + break; + + default: + + await message.ConfirmAction(); + + break; + + } + + + + + } + + public override async Task Render(MessageResult message) + { + if (this.MessageId == null) + { + var buttons = Program.BotConfig.Commandos.Where(a => a.Enabled).Select(a => new ButtonBase(a.Title, a.ID.ToString())); + + ButtonForm bf = new ButtonForm(); + bf.AddSplitted(buttons, 1); + await this.Device.Send("Deine Optionen", bf); + + return; + } + + + } + + + + + } +} diff --git a/Examples/SystemCommandsBot/forms/StartForm.cs b/Examples/SystemCommandsBot/forms/StartForm.cs new file mode 100644 index 0000000..c5e6597 --- /dev/null +++ b/Examples/SystemCommandsBot/forms/StartForm.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; +using TelegramBotBase.Base; + +namespace SystemCommandsBot.forms +{ + public class StartForm : TelegramBotBase.Form.FormBase + { + public String Password { get; set; } + + public override async Task Load(MessageResult message) + { + var inp = message.MessageText; + if (Program.BotConfig.Password == inp) + { + this.Password = inp; + } + + + } + + + public override async Task Render(MessageResult message) + { + if (this.Password == null || this.Password.Trim() == "") + { + await this.Device.Send("Bitte gib dein Passwort an."); + return; + } + + + var cmd = new forms.CmdForm(); + cmd.ExpiresAt = DateTime.Now.AddDays(14); + + await this.NavigateTo(cmd); + + } + + } +} diff --git a/TelegramBotFramework.sln b/TelegramBotFramework.sln index 03aeeab..c4f75b1 100644 --- a/TelegramBotFramework.sln +++ b/TelegramBotFramework.sln @@ -12,6 +12,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution README.md = README.md EndProjectSection EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Examples", "Examples", "{BFA71E3F-31C0-4FC1-A320-4DCF704768C5}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SystemCommandsBot", "Examples\SystemCommandsBot\SystemCommandsBot.csproj", "{FC484952-3060-4F87-9809-4CD66D6961C0}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -26,10 +30,17 @@ Global {88EC0E02-583D-4B9D-956C-81D63C8CFCFA}.Debug|Any CPU.Build.0 = Debug|Any CPU {88EC0E02-583D-4B9D-956C-81D63C8CFCFA}.Release|Any CPU.ActiveCfg = Release|Any CPU {88EC0E02-583D-4B9D-956C-81D63C8CFCFA}.Release|Any CPU.Build.0 = Release|Any CPU + {FC484952-3060-4F87-9809-4CD66D6961C0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FC484952-3060-4F87-9809-4CD66D6961C0}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FC484952-3060-4F87-9809-4CD66D6961C0}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FC484952-3060-4F87-9809-4CD66D6961C0}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {FC484952-3060-4F87-9809-4CD66D6961C0} = {BFA71E3F-31C0-4FC1-A320-4DCF704768C5} + EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {59CB40E1-9FA7-4867-A56F-4F418286F057} EndGlobalSection