- added ToKeyboardButton to ButtonBase

- added eKeyboardType to switch between Inline and KeykboardButtons
- addes some small functions to ButtonForm
- added new control "ButtonGrid" which manages Button handling with Event Handler for each Button and can switch between InlineMode and KeyboardMode during runtime
- added also Examples to it
This commit is contained in:
FlorianDahn
2019-09-26 20:54:35 +02:00
parent 94195ec5bf
commit 1b811e1786
6 changed files with 368 additions and 2 deletions
+212
View File
@@ -0,0 +1,212 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Telegram.Bot.Types;
using Telegram.Bot.Types.ReplyMarkups;
using TelegramBotBase.Base;
using TelegramBotBase.Enums;
using TelegramBotBase.Form;
namespace TelegramBotBase.Controls
{
public class ButtonGrid : Base.ControlBase
{
public String Title { get; set; } = "Toggle";
private bool RenderNecessary = true;
private static readonly object __evButtonClicked = new object();
private readonly EventHandlerList Events = new EventHandlerList();
public ButtonForm ButtonsForm { get; set; }
public int? MessageId
{
get
{
return m_iMessageId;
}
set
{
m_iMessageId = value;
}
}
private int? m_iMessageId = null;
/// <summary>
/// Defines which type of Button Keyboard should be rendered.
/// </summary>
public eKeyboardType KeyboardType
{
get; private set;
}
public async Task SetKeyboardType(eKeyboardType type)
{
if (KeyboardType == type)
return;
this.RenderNecessary = true;
Cleanup().Wait();
KeyboardType = type;
}
private bool m_bVisible = true;
public ButtonGrid()
{
this.ButtonsForm = new ButtonForm();
}
public ButtonGrid(ButtonForm form)
{
this.ButtonsForm = form;
}
public event EventHandler<ButtonClickedEventArgs> ButtonClicked
{
add
{
this.Events.AddHandler(__evButtonClicked, value);
}
remove
{
this.Events.RemoveHandler(__evButtonClicked, value);
}
}
public void OnButtonClicked(ButtonClickedEventArgs e)
{
(this.Events[__evButtonClicked] as EventHandler<ButtonClickedEventArgs>)?.Invoke(this, e);
}
public async override Task Load(MessageResult result)
{
if (this.KeyboardType != eKeyboardType.ReplyKeyboard)
return;
var button = ButtonsForm.ToList().FirstOrDefault(a => a.Text == result.MessageText);
if (button == null)
return;
OnButtonClicked(new ButtonClickedEventArgs(button));
result.Handled = true;
}
public async override Task Action(MessageResult result, string value = null)
{
if (result.Handled)
return;
await result.ConfirmAction();
//Find clicked button depending on Text or Value (depending on markup type)
switch (this.KeyboardType)
{
case eKeyboardType.InlineKeyBoard:
var button = ButtonsForm.ToList().FirstOrDefault(a => a.Value == result.RawData);
if (button == null)
return;
OnButtonClicked(new ButtonClickedEventArgs(button));
result.Handled = true;
break;
}
}
public async override Task Render(MessageResult result)
{
if (!this.RenderNecessary)
return;
Message m = null;
if (this.MessageId != null)
{
switch (this.KeyboardType)
{
//Reply Keyboard could only be updated with a new keyboard.
case eKeyboardType.ReplyKeyboard:
if (this.ButtonsForm.Count == 0)
{
await this.Device.Send("", new ReplyKeyboardRemove());
this.MessageId = null;
}
else
{
m = await this.Device.Send(this.Title, (ReplyKeyboardMarkup)this.ButtonsForm, disableNotification: true);
}
break;
case eKeyboardType.InlineKeyBoard:
m = await this.Device.Edit(this.MessageId.Value, this.Title, (InlineKeyboardMarkup)this.ButtonsForm);
break;
}
}
else
{
switch (this.KeyboardType)
{
case eKeyboardType.ReplyKeyboard:
m = await this.Device.Send(this.Title, (ReplyKeyboardMarkup)this.ButtonsForm, disableNotification: true);
break;
case eKeyboardType.InlineKeyBoard:
m = await this.Device.Send(this.Title, (InlineKeyboardMarkup)this.ButtonsForm, disableNotification: true);
break;
}
if (m != null)
{
this.MessageId = m.MessageId;
}
}
this.RenderNecessary = false;
}
public async override Task Cleanup()
{
if (this.MessageId == null)
return;
await this.Device.DeleteMessage(this.MessageId.Value);
this.MessageId = null;
if (this.KeyboardType == eKeyboardType.ReplyKeyboard)
{
await this.Device.Send("", new ReplyKeyboardRemove());
}
}
}
}
+22
View File
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TelegramBotBase.Enums
{
public enum eKeyboardType
{
/// <summary>
/// Uses a ReplyKeyboardMarkup
/// </summary>
ReplyKeyboard = 0,
/// <summary>
/// Uses a InlineKeyboardMakup
/// </summary>
InlineKeyBoard = 1
}
}
+12
View File
@@ -48,5 +48,17 @@ namespace TelegramBotBase.Form
}
public KeyboardButton ToKeyboardButton(ButtonForm form)
{
//String id = (form.DependencyControl != null ? form.DependencyControl.ControlID + "_" : "");
if (this.Url == null)
{
return new KeyboardButton(this.Text);
}
return new KeyboardButton(this.Text);
}
}
}
+32 -2
View File
@@ -60,6 +60,14 @@ namespace TelegramBotBase.Form
return splitted;
}
public int Count
{
get
{
return this.Buttons.Select(a => a.ToArray()).Aggregate((a, b) => a.Union(b).ToArray()).Length;
}
}
/// <summary>
/// Add buttons splitted in the amount of columns (i.e. 2 per row...)
/// </summary>
@@ -75,19 +83,41 @@ namespace TelegramBotBase.Form
}
}
public InlineKeyboardButton[][] ToArray()
public List<ButtonBase> ToList()
{
return this.Buttons.Aggregate((a, b) => a.Union(b).ToList());
}
public InlineKeyboardButton[][] ToInlineButtonArray()
{
var ikb = this.Buttons.Select(a => a.Select(b => b.ToInlineButton(this)).ToArray()).ToArray();
return ikb;
}
public KeyboardButton[][] ToReplyButtonArray()
{
var ikb = this.Buttons.Select(a => a.Select(b => b.ToKeyboardButton(this)).ToArray()).ToArray();
return ikb;
}
public static implicit operator InlineKeyboardMarkup(ButtonForm form)
{
if (form == null)
return null;
InlineKeyboardMarkup ikm = new InlineKeyboardMarkup(form.ToArray());
InlineKeyboardMarkup ikm = new InlineKeyboardMarkup(form.ToInlineButtonArray());
return ikm;
}
public static implicit operator ReplyKeyboardMarkup(ButtonForm form)
{
if (form == null)
return null;
ReplyKeyboardMarkup ikm = new ReplyKeyboardMarkup(form.ToReplyButtonArray());
return ikm;
}