From 0af587d27f44c0db43807cadaddf368d379f1adc Mon Sep 17 00:00:00 2001 From: FlorianDahn Date: Mon, 4 May 2020 15:33:21 +0200 Subject: [PATCH] Updating readme and test project --- README.md | 23 +++++---- TelegramBotBaseTest/Program.cs | 92 ++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+), 9 deletions(-) create mode 100644 TelegramBotBaseTest/Program.cs diff --git a/README.md b/README.md index a33a726..bafafc0 100644 --- a/README.md +++ b/README.md @@ -118,7 +118,7 @@ It gives you features which will look/feel like WinForms or have a good way to c ## How to start: Within your empty App your need to put some initial lines including your APIKey to get things started. -The "BotBase" Class will manage a lot of things for you, like system calls, action events and so on. +The "BotBase" Class will manage a lot of things for you, like bot commands, action events and so on. "StartForm" is your first Formular which every user will get internally redirected to, like a start page, you could redirect from there later in code, so users won't recognize it. It needs to be a subclass of "FormBase" you will find in Namespace TelegramBotBase.Base @@ -129,7 +129,10 @@ It needs to be a subclass of "FormBase" you will find in Namespace TelegramBotBa BotBase bb = new BotBase("{YOUR API KEY}"); //Add Systemcommands if you like, you could catch them later -bb.SystemCalls.Add("/start"); +bb.BotCommands.Add(new BotCommand() { Command = "start", Description = "Starts the bot" }); + +//Update bot commands to botfather +bb.UploadBotCommands().Wait(); //Start your Bot bb.Start(); @@ -224,7 +227,7 @@ All examples are within the test project, so just try it out on your own. ### Add some system calls (Example #0 - System Calls) Inside of the BotFather you are able to add "Commands" to your TelegramBot. The user will see them, depending on the application as options he could choose. -I'm calling them Systemcalls. Before start (and later for sure) you could add them to your BotBase. Every time a message comes in they will get checked if they are one of them. +Before start (and later for sure) you could add them to your BotBase. Every time a message comes in they will get checked if they are one of them. If so, a special event Handler will get raised where you are easier able to manage the action behind. Below we have 4 options. @@ -242,13 +245,12 @@ Below we have 4 options. ``` BotBase bb = new BotBase("{YOUR API KEY}"); -bb.SystemCalls.Add("/start"); -bb.SystemCalls.Add("/form1"); -bb.SystemCalls.Add("/form2"); +bb.BotCommands.Add(new BotCommand() { Command = "start", Description = "Starts the bot" }); +bb.BotCommands.Add(new BotCommand() { Command = "form1", Description = "Opens test form 1" }); +bb.BotCommands.Add(new BotCommand() { Command = "form2", Description = "Opens test form 2" }); +bb.BotCommands.Add(new BotCommand() { Command = "params", Description = "Returns all send parameters as a message." }); -bb.SystemCalls.Add("/params"); - -bb.SystemCall += async (s, en) => +bb.BotCommand += async (s, en) => { switch (en.Command) { @@ -279,6 +281,9 @@ bb.SystemCall += async (s, en) => }; +//Update Bot commands to botfather +bb.UploadBotCommands().Wait(); + bb.Start(); ``` diff --git a/TelegramBotBaseTest/Program.cs b/TelegramBotBaseTest/Program.cs new file mode 100644 index 0000000..d052ae5 --- /dev/null +++ b/TelegramBotBaseTest/Program.cs @@ -0,0 +1,92 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Telegram.Bot.Types; +using TelegramBotBase; +using TelegramBotBase.Form; +using TelegramBotBaseTest.Tests; + +namespace TelegramBotBaseTest +{ + class Program + { + static void Main(string[] args) + { + + BotBase bb = new BotBase(APIKey); + + bb.BotCommands.Add(new BotCommand() { Command = "start", Description = "Starts the bot" }); + bb.BotCommands.Add(new BotCommand() { Command = "form1", Description = "Opens test form 1" }); + bb.BotCommands.Add(new BotCommand() { Command = "form2", Description = "Opens test form 2" }); + bb.BotCommands.Add(new BotCommand() { Command = "params", Description = "Returns all send parameters as a message." }); + + bb.BotCommand += async (s, en) => + { + switch (en.Command) + { + case "/start": + + var start = new Menu(); + + await en.Device.ActiveForm.NavigateTo(start); + + break; + case "/form1": + + var form1 = new TestForm(); + + await en.Device.ActiveForm.NavigateTo(form1); + + break; + + case "/form2": + + var form2 = new TestForm2(); + + await en.Device.ActiveForm.NavigateTo(form2); + + break; + + case "/params": + + String m = en.Parameters.DefaultIfEmpty("").Aggregate((a, b) => a + " and " + b); + + await en.Device.Send("Your parameters are: " + m, replyTo: en.Device.LastMessageId); + + en.Handled = true; + + break; + } + + }; + + //Update Bot commands to botfather + bb.UploadBotCommands().Wait(); + + bb.SetSetting(TelegramBotBase.Enums.eSettings.LogAllMessages, true); + + bb.Message += (s,en) => + { + Console.WriteLine(en.DeviceId + " " + en.Message.MessageText + " " + (en.Message.RawData ?? "")); + }; + + bb.Start(); + + + + Console.WriteLine("Telegram Bot started..."); + + Console.WriteLine("Press q to quit application."); + + + Console.ReadLine(); + + bb.Stop(); + + } + + + } +}