- adding new SentData Event in FormBase for User uploads (for Photo, Audio, Video, Contact, Location, Document)

- adding RequestUserLocation and RequestUserContact to Device class
- Update to Device class
- updating examples
- adding example for data upload
- small documentary Updates (Readme.md)
This commit is contained in:
FlorianDahn 2019-03-27 14:01:48 +07:00
parent 5cd54fc746
commit b14fa2dc87
12 changed files with 348 additions and 19 deletions

View File

@ -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();

View File

@ -53,6 +53,7 @@
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Tests\ButtonTestForm.cs" />
<Compile Include="Tests\DataForm.cs" />
<Compile Include="Tests\ProgressTest.cs" />
<Compile Include="Tests\Register\PerForm.cs" />
<Compile Include="Tests\Register\PerStep.cs" />

View File

@ -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);
}
}
}

View File

@ -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);
}

View File

@ -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();

View File

@ -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
{
/// <summary>
/// Returns a class to manage attachments within messages.
/// </summary>
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;
}
}
/// <summary>
/// Returns the FileId of the first reachable element.
/// </summary>
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<InputOnlineFile> 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<InputOnlineFile> 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<InputOnlineFile> 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, "");
}
}
}

View File

@ -86,6 +86,16 @@ namespace TelegramBotBase.Form
}
/// <summary>
/// Gets invoked if the user has sent some media (Photo, Audio, Video, Contact, Location, Document)
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
public virtual async Task SentData(DataResult message)
{
}
/// <summary>
/// Gets invoked at the end of the cycle to "Render" text, images, buttons, etc...
/// </summary>

View File

@ -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);

View File

@ -38,6 +38,15 @@ namespace TelegramBotBase.Form
this.Buttons = Buttons.ToList();
}
/// <summary>
/// Adds one Button
/// </summary>
/// <param name="button"></param>
public void AddButton(ButtonBase button)
{
this.Buttons.Add(button);
}
public override async Task Action(MessageResult message)
{
var call = message.GetData<CallbackData>();

View File

@ -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")]

View File

@ -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
/// </summary>
/// <param name="buttonText"></param>
/// <param name="requestMessage"></param>
/// <param name="OneTimeOnly"></param>
/// <returns></returns>
public async Task<Message> RequestContact(String buttonText = "Send your contact", String requestMessage = "Give me your phone number!")
public async Task<Message> 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
/// </summary>
/// <param name="buttonText"></param>
/// <param name="requestMessage"></param>
/// <param name="OneTimeOnly"></param>
/// <returns></returns>
public async Task<Message> RequestLocation(String buttonText = "Send your location", String requestMessage = "Give me your location!")
public async Task<Message> 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);
}

View File

@ -51,6 +51,7 @@
<ItemGroup>
<Compile Include="Base\ButtonClickedEventArgs.cs" />
<Compile Include="Base\ControlBase.cs" />
<Compile Include="Base\DataResult.cs" />
<Compile Include="Base\MessageClient.cs" />
<Compile Include="Base\MessageIncomeResult.cs" />
<Compile Include="Base\MessageResult.cs" />