diff --git a/README.md b/README.md index dd1fc7c..1e035d2 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,7 @@ It gives you features which will look/feel like WinForms or have a good way to c 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. "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.Form. +It needs to be a subclass of "FormBase" you will find in Namespace TelegramBotBase.Base ``` @@ -83,35 +83,45 @@ public class StartForm : FormBase } + //Gets invoked during Navigation to this form public override async Task Init(params object[] param) { } + //Gets invoked if the form gets opened (with Form.NavigateTo(NewForm)) public override async Task Opened() { } + //Gets invoked if the form gets leaved public override async Task Closed() { } + //Gets invoked on every Message/Action/Data in this context public override async Task Load(MessageResult message) { await this.Device.Send("Hello world!"); } - - + //Gets invoked on Button clicks public override async Task Action(MessageResult message) { } + //Gets invoked on Data uploades by the user (of type Photo, Audio, Video, Contact, Location, Document) + public override async Task SentData(DataResult data) + { + + } + + //Gets invoked on every Message/Action/Data to render Design or Response public override async Task Render(MessageResult message) { @@ -626,7 +636,10 @@ await this.NavigateTo(ad); ``` -PromptDialog pd = new PromptDialog("Please confirm", new ButtonBase("Ok", "ok"), new ButtonBase("Cancel", "cancel")); +PromptDialog pd = new PromptDialog("Please confirm"); + +pd.AddButton(new ButtonBase("Ok", "ok")); +pd.AddButton(new ButtonBase("Cancel", "cancel")); var tf = new TestForm2(); diff --git a/TelegramBaseTest/TelegramBaseTest.csproj b/TelegramBaseTest/TelegramBaseTest.csproj index 5385414..959f738 100644 --- a/TelegramBaseTest/TelegramBaseTest.csproj +++ b/TelegramBaseTest/TelegramBaseTest.csproj @@ -53,6 +53,7 @@ + diff --git a/TelegramBaseTest/Tests/DataForm.cs b/TelegramBaseTest/Tests/DataForm.cs new file mode 100644 index 0000000..cd74f17 --- /dev/null +++ b/TelegramBaseTest/Tests/DataForm.cs @@ -0,0 +1,142 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Telegram.Bot.Types; +using Telegram.Bot.Types.InputFiles; +using Telegram.Bot.Types.ReplyMarkups; +using TelegramBotBase.Base; +using TelegramBotBase.Form; + +namespace TelegramBaseTest.Tests +{ + public class DataForm : AutoCleanForm + { + + + public override async Task SentData(DataResult data) + { + String tmp = ""; + InputOnlineFile file; + + switch (data.Type) + { + case Telegram.Bot.Types.Enums.MessageType.Contact: + + tmp += "Firstname: " + data.Contact.FirstName + "\r\n"; + tmp += "Lastname: " + data.Contact.LastName + "\r\n"; + tmp += "Phonenumber: " + data.Contact.PhoneNumber + "\r\n"; + tmp += "UserId: " + data.Contact.UserId + "\r\n"; + + await this.Device.Send("Your contact: \r\n" + tmp, replyTo: data.MessageId); + + break; + + case Telegram.Bot.Types.Enums.MessageType.Document: + + file = new InputOnlineFile(data.Document.FileId); + + await this.Device.SendDocument(file, "Your uploaded document"); + + + break; + + case Telegram.Bot.Types.Enums.MessageType.Video: + + file = new InputOnlineFile(data.Document.FileId); + + await this.Device.SendDocument(file, "Your uploaded video"); + + break; + + case Telegram.Bot.Types.Enums.MessageType.Audio: + + file = new InputOnlineFile(data.Document.FileId); + + await this.Device.SendDocument(file, "Your uploaded audio"); + + break; + + case Telegram.Bot.Types.Enums.MessageType.Location: + + tmp += "Lat: " + data.Location.Latitude + "\r\n"; + tmp += "Lng: " + data.Location.Longitude + "\r\n"; + + await this.Device.Send("Your location: \r\n" + tmp, replyTo: data.MessageId); + + break; + + case Telegram.Bot.Types.Enums.MessageType.Photo: + + InputOnlineFile photo = new InputOnlineFile(data.Photos.Last().FileId); + + await this.Device.Send("Your image: ", replyTo: data.MessageId); + await this.Client.TelegramClient.SendPhotoAsync(this.Device.DeviceId, photo); + + break; + + default: + + await this.Device.Send("Unknown response"); + + break; + } + + } + + public override async Task Action(MessageResult message) + { + await message.ConfirmAction(); + + if (message.Handled) + return; + + switch (message.RawData) + { + case "contact": + + await this.Device.RequestContact(); + + break; + + case "location": + + await this.Device.RequestLocation(); + + break; + + case "back": + + message.Handled = true; + + var start = new Start(); + + await this.NavigateTo(start); + + break; + } + + + } + + public override async Task Render(MessageResult message) + { + ButtonForm bf = new ButtonForm(); + + bf.AddButtonRow(new ButtonBase("Request User contact", "contact")); + + bf.AddButtonRow(new ButtonBase("Request User location", "location")); + + bf.AddButtonRow(new ButtonBase("Back", "back")); + + InlineKeyboardMarkup ikv = bf; + + await this.Device.Send("Please upload a contact, photo, video, audio, document or location.", bf); + + + + } + + } +} diff --git a/TelegramBaseTest/Tests/Start.cs b/TelegramBaseTest/Tests/Start.cs index cb60a33..221bdde 100644 --- a/TelegramBaseTest/Tests/Start.cs +++ b/TelegramBaseTest/Tests/Start.cs @@ -69,6 +69,16 @@ namespace TelegramBaseTest.Tests await this.NavigateTo(form2); + break; + + case "data": + + message.Handled = true; + + var data = new DataForm(); + + await this.NavigateTo(data); + break; } @@ -88,6 +98,8 @@ namespace TelegramBaseTest.Tests btn.AddButtonRow(new ButtonBase("#6 Form2 Command", new CallbackData("a", "form2").Serialize())); + btn.AddButtonRow(new ButtonBase("#7 Data Handling", new CallbackData("a", "data").Serialize())); + await this.Device.Send("Choose your test:", btn); } diff --git a/TelegramBaseTest/Tests/TestForm2.cs b/TelegramBaseTest/Tests/TestForm2.cs index 8300e05..baab75c 100644 --- a/TelegramBaseTest/Tests/TestForm2.cs +++ b/TelegramBaseTest/Tests/TestForm2.cs @@ -60,7 +60,10 @@ namespace TelegramBaseTest.Tests } else if (call.Value == "prompt") { - PromptDialog pd = new PromptDialog("Please confirm", new ButtonBase("Ok", "ok"), new ButtonBase("Cancel", "cancel")); + PromptDialog pd = new PromptDialog("Please confirm"); + + pd.AddButton(new ButtonBase("Ok", "ok")); + pd.AddButton(new ButtonBase("Cancel", "cancel")); var tf = new TestForm2(); diff --git a/TelegramBotBase/Base/DataResult.cs b/TelegramBotBase/Base/DataResult.cs new file mode 100644 index 0000000..9e210eb --- /dev/null +++ b/TelegramBotBase/Base/DataResult.cs @@ -0,0 +1,129 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Telegram.Bot.Types; +using Telegram.Bot.Types.InputFiles; + +namespace TelegramBotBase.Base +{ + /// + /// Returns a class to manage attachments within messages. + /// + public class DataResult : ResultBase + { + public Telegram.Bot.Args.MessageEventArgs RawMessageData { get; set; } + + public Contact Contact + { + get + { + return this.Message.Contact; + } + } + + public Location Location + { + get + { + return this.Message.Location; + } + } + + public Document Document + { + get + { + return this.Message.Document; + } + } + + public Audio Audio + { + get + { + return this.Message.Audio; + } + } + + public Video Video + { + get + { + return this.Message.Video; + } + } + + public PhotoSize[] Photos + { + get + { + return this.Message.Photo; + } + } + + public Telegram.Bot.Types.Enums.MessageType Type + { + get + { + return this.RawMessageData?.Message?.Type ?? Telegram.Bot.Types.Enums.MessageType.Unknown; + } + } + + /// + /// Returns the FileId of the first reachable element. + /// + public String FileId + { + get + { + return (this.Document?.FileId ?? + this.Audio?.FileId ?? + this.Video?.FileId ?? + this.Photos.FirstOrDefault()?.FileId); + } + } + + + public DataResult(Telegram.Bot.Args.MessageEventArgs rawdata) + { + this.RawMessageData = rawdata; + this.Message = rawdata.Message; + } + + public DataResult(MessageResult message) + { + this.RawMessageData = message.RawMessageData; + this.Message = message.Message; + + + } + + public async Task DownloadDocument() + { + var encryptedContent = new System.IO.MemoryStream(this.Document.FileSize); + var file = await this.Client.TelegramClient.GetInfoAndDownloadFileAsync(this.Document.FileId, encryptedContent); + + return new InputOnlineFile(encryptedContent, this.Document.FileName); + } + + public async Task DownloadVideo() + { + var encryptedContent = new System.IO.MemoryStream(this.Video.FileSize); + var file = await this.Client.TelegramClient.GetInfoAndDownloadFileAsync(this.Video.FileId, encryptedContent); + + return new InputOnlineFile(encryptedContent, ""); + } + + public async Task DownloadPhoto(int index) + { + var photo = this.Photos[index]; + var encryptedContent = new System.IO.MemoryStream(photo.FileSize); + var file = await this.Client.TelegramClient.GetInfoAndDownloadFileAsync(photo.FileId, encryptedContent); + + return new InputOnlineFile(encryptedContent, ""); + } + + } +} diff --git a/TelegramBotBase/Base/FormBase.cs b/TelegramBotBase/Base/FormBase.cs index e69f443..747bf5d 100644 --- a/TelegramBotBase/Base/FormBase.cs +++ b/TelegramBotBase/Base/FormBase.cs @@ -86,6 +86,16 @@ namespace TelegramBotBase.Form } + /// + /// Gets invoked if the user has sent some media (Photo, Audio, Video, Contact, Location, Document) + /// + /// + /// + public virtual async Task SentData(DataResult message) + { + + } + /// /// Gets invoked at the end of the cycle to "Render" text, images, buttons, etc... /// diff --git a/TelegramBotBase/BotBase.cs b/TelegramBotBase/BotBase.cs index c4d3959..5269e4a 100644 --- a/TelegramBotBase/BotBase.cs +++ b/TelegramBotBase/BotBase.cs @@ -267,6 +267,13 @@ namespace TelegramBotBase //Loading Event await activeForm.Load(e); + //Is Attachment ? (Photo, Audio, Video, Contact, Location, Document) + if (e.Message.Type == Telegram.Bot.Types.Enums.MessageType.Contact | e.Message.Type == Telegram.Bot.Types.Enums.MessageType.Document | e.Message.Type == Telegram.Bot.Types.Enums.MessageType.Location | + e.Message.Type == Telegram.Bot.Types.Enums.MessageType.Photo | e.Message.Type == Telegram.Bot.Types.Enums.MessageType.Video | e.Message.Type == Telegram.Bot.Types.Enums.MessageType.Audio) + { + await activeForm.SentData(new DataResult(e)); + } + //Render Event if (!activeForm.FormSwitched) await activeForm.Render(e); diff --git a/TelegramBotBase/Form/PromptDialog.cs b/TelegramBotBase/Form/PromptDialog.cs index 64943a5..a5e7ad4 100644 --- a/TelegramBotBase/Form/PromptDialog.cs +++ b/TelegramBotBase/Form/PromptDialog.cs @@ -38,6 +38,15 @@ namespace TelegramBotBase.Form this.Buttons = Buttons.ToList(); } + /// + /// Adds one Button + /// + /// + public void AddButton(ButtonBase button) + { + this.Buttons.Add(button); + } + public override async Task Action(MessageResult message) { var call = message.GetData(); diff --git a/TelegramBotBase/Properties/AssemblyInfo.cs b/TelegramBotBase/Properties/AssemblyInfo.cs index 810356b..653da54 100644 --- a/TelegramBotBase/Properties/AssemblyInfo.cs +++ b/TelegramBotBase/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ using System.Runtime.InteropServices; // Sie können alle Werte angeben oder die standardmäßigen Build- und Revisionsnummern // übernehmen, indem Sie "*" eingeben: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.3.1.3")] -[assembly: AssemblyFileVersion("1.3.1.3")] +[assembly: AssemblyVersion("1.4.0.0")] +[assembly: AssemblyFileVersion("1.4.0.0")] diff --git a/TelegramBotBase/Sessions/DeviceSession.cs b/TelegramBotBase/Sessions/DeviceSession.cs index c8cc5f4..4f1f0be 100644 --- a/TelegramBotBase/Sessions/DeviceSession.cs +++ b/TelegramBotBase/Sessions/DeviceSession.cs @@ -114,6 +114,7 @@ namespace TelegramBotBase.Sessions try { var m = await this.Client.TelegramClient.EditMessageTextAsync(this.DeviceId, messageId, text, replyMarkup: markup); + return m; } catch @@ -149,6 +150,8 @@ namespace TelegramBotBase.Sessions try { m = await (this.Client.TelegramClient.SendTextMessageAsync(this.DeviceId, text, replyToMessageId: replyTo, replyMarkup: markup, disableNotification: disableNotification)); + + OnMessageSent(new MessageSentEventArgs(m.MessageId, m)); } catch (ApiRequestException ex) { @@ -159,9 +162,6 @@ namespace TelegramBotBase.Sessions return null; } - - OnMessageSent(new MessageSentEventArgs(m.MessageId, m)); - return m; } @@ -183,6 +183,8 @@ namespace TelegramBotBase.Sessions try { m = await (this.Client.TelegramClient.SendTextMessageAsync(this.DeviceId, text, replyToMessageId: replyTo, replyMarkup: markup, disableNotification: disableNotification)); + + OnMessageSent(new MessageSentEventArgs(m.MessageId, m)); } catch (ApiRequestException ex) { @@ -193,8 +195,6 @@ namespace TelegramBotBase.Sessions return null; } - OnMessageSent(new MessageSentEventArgs(m.MessageId, m)); - return m; } @@ -222,6 +222,8 @@ namespace TelegramBotBase.Sessions try { m = await this.Client.TelegramClient.SendPhotoAsync(this.DeviceId, file, replyToMessageId: replyTo, replyMarkup: markup, disableNotification: disableNotification); + + OnMessageSent(new MessageSentEventArgs(m.MessageId, m)); } catch (ApiRequestException ex) { @@ -232,8 +234,6 @@ namespace TelegramBotBase.Sessions return null; } - OnMessageSent(new MessageSentEventArgs(m.MessageId, m)); - return m; } @@ -318,7 +318,7 @@ namespace TelegramBotBase.Sessions } var m = await this.Client.TelegramClient.SendDocumentAsync(this.DeviceId, document, caption, replyMarkup: markup, disableNotification: disableNotification, replyToMessageId: replyTo); - + OnMessageSent(new MessageSentEventArgs(m.MessageId, m)); return m; @@ -339,11 +339,12 @@ namespace TelegramBotBase.Sessions /// /// /// + /// /// - public async Task RequestContact(String buttonText = "Send your contact", String requestMessage = "Give me your phone number!") + public async Task RequestContact(String buttonText = "Send your contact", String requestMessage = "Give me your phone number!", bool OneTimeOnly = true) { var rck = new ReplyKeyboardMarkup(KeyboardButton.WithRequestContact(buttonText)); - + rck.OneTimeKeyboard = OneTimeOnly; return await this.Client.TelegramClient.SendTextMessageAsync(this.DeviceId, requestMessage, replyMarkup: rck); } @@ -352,11 +353,12 @@ namespace TelegramBotBase.Sessions /// /// /// + /// /// - public async Task RequestLocation(String buttonText = "Send your location", String requestMessage = "Give me your location!") + public async Task RequestLocation(String buttonText = "Send your location", String requestMessage = "Give me your location!", bool OneTimeOnly = true) { var rcl = new ReplyKeyboardMarkup(KeyboardButton.WithRequestLocation(buttonText)); - + rcl.OneTimeKeyboard = OneTimeOnly; return await this.Client.TelegramClient.SendTextMessageAsync(this.DeviceId, requestMessage, replyMarkup: rcl); } diff --git a/TelegramBotBase/TelegramBotBase.csproj b/TelegramBotBase/TelegramBotBase.csproj index fe864ef..4fd1a1b 100644 --- a/TelegramBotBase/TelegramBotBase.csproj +++ b/TelegramBotBase/TelegramBotBase.csproj @@ -51,6 +51,7 @@ +