- adding example project for Bot to start predefined system commands
This commit is contained in:
parent
77fd3393de
commit
382ae2de2d
16
Examples/SystemCommandsBot/SystemCommandsBot.csproj
Normal file
16
Examples/SystemCommandsBot/SystemCommandsBot.csproj
Normal file
@ -0,0 +1,16 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\TelegramBotBase\TelegramBotBase.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
30
Examples/SystemCommandsBot/commands/Commando.cs
Normal file
30
Examples/SystemCommandsBot/commands/Commando.cs
Normal file
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
92
Examples/SystemCommandsBot/config/Config.cs
Normal file
92
Examples/SystemCommandsBot/config/Config.cs
Normal file
@ -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<commands.Commando> Commandos { get; set; }
|
||||
|
||||
|
||||
public Config()
|
||||
{
|
||||
this.Commandos = new List<commands.Commando>();
|
||||
}
|
||||
|
||||
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<Config>(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
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
172
Examples/SystemCommandsBot/forms/CmdForm.cs
Normal file
172
Examples/SystemCommandsBot/forms/CmdForm.cs
Normal file
@ -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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
42
Examples/SystemCommandsBot/forms/StartForm.cs
Normal file
42
Examples/SystemCommandsBot/forms/StartForm.cs
Normal file
@ -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);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@ -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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user