- PromptDialog has been changed to ConfirmDialog
- PromptDialog is now (like in JavaScript) for text inputs - examples corrected
This commit is contained in:
parent
48c62e57f8
commit
038a941d33
@ -11,7 +11,7 @@ namespace TelegramBotBase.Form
|
||||
/// <summary>
|
||||
/// A simple prompt dialog with one ok Button
|
||||
/// </summary>
|
||||
public class AlertDialog : PromptDialog
|
||||
public class AlertDialog : ConfirmDialog
|
||||
{
|
||||
public String ButtonText { get; set; }
|
||||
|
||||
|
||||
100
TelegramBotBase/Form/ConfirmDialog.cs
Normal file
100
TelegramBotBase/Form/ConfirmDialog.cs
Normal file
@ -0,0 +1,100 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using TelegramBotBase.Base;
|
||||
|
||||
namespace TelegramBotBase.Form
|
||||
{
|
||||
public class ConfirmDialog : FormBase
|
||||
{
|
||||
public String Message { get; set; }
|
||||
|
||||
|
||||
public List<ButtonBase> Buttons { get; set; }
|
||||
|
||||
private EventHandlerList __Events { get; set; } = new EventHandlerList();
|
||||
|
||||
private static object __evButtonClicked { get; } = new object();
|
||||
|
||||
public ConfirmDialog()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public ConfirmDialog(String Message)
|
||||
{
|
||||
this.Message = Message;
|
||||
this.Buttons = new List<Form.ButtonBase>();
|
||||
}
|
||||
|
||||
public ConfirmDialog(String Message, params ButtonBase[] Buttons)
|
||||
{
|
||||
this.Message = Message;
|
||||
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>();
|
||||
if (call == null)
|
||||
return;
|
||||
|
||||
message.Handled = true;
|
||||
|
||||
await message.ConfirmAction();
|
||||
|
||||
await message.DeleteMessage();
|
||||
|
||||
ButtonBase button = this.Buttons.FirstOrDefault(a => a.Value == call.Value);
|
||||
|
||||
if (button == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
OnButtonClicked(new ButtonClickedEventArgs(button));
|
||||
}
|
||||
|
||||
|
||||
public override async Task Render(MessageResult message)
|
||||
{
|
||||
ButtonForm btn = new ButtonForm();
|
||||
|
||||
var buttons = this.Buttons.Select(a => new ButtonBase(a.Text, CallbackData.Create("action", a.Value))).ToList();
|
||||
btn.AddButtonRow(buttons);
|
||||
|
||||
await this.Device.Send(this.Message, btn);
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@ -12,12 +12,12 @@ namespace TelegramBotBase.Form
|
||||
{
|
||||
public String Message { get; set; }
|
||||
|
||||
|
||||
public List<ButtonBase> Buttons { get; set; }
|
||||
public String Value { get; set; }
|
||||
|
||||
private EventHandlerList __Events { get; set; } = new EventHandlerList();
|
||||
|
||||
private static object __evButtonClicked { get; } = new object();
|
||||
private static object __evCompleted { get; } = new object();
|
||||
|
||||
|
||||
public PromptDialog()
|
||||
{
|
||||
@ -27,73 +27,50 @@ namespace TelegramBotBase.Form
|
||||
public PromptDialog(String Message)
|
||||
{
|
||||
this.Message = Message;
|
||||
this.Buttons = new List<Form.ButtonBase>();
|
||||
}
|
||||
|
||||
public PromptDialog(String Message, params ButtonBase[] Buttons)
|
||||
public async override Task Load(MessageResult message)
|
||||
{
|
||||
this.Message = Message;
|
||||
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>();
|
||||
if (call == null)
|
||||
if (message.Handled)
|
||||
return;
|
||||
|
||||
message.Handled = true;
|
||||
|
||||
await message.ConfirmAction();
|
||||
|
||||
await message.DeleteMessage();
|
||||
|
||||
ButtonBase button = this.Buttons.FirstOrDefault(a => a.Value == call.Value);
|
||||
|
||||
if (button == null)
|
||||
if (this.Value == null)
|
||||
{
|
||||
return;
|
||||
this.Value = message.MessageText;
|
||||
}
|
||||
|
||||
OnButtonClicked(new ButtonClickedEventArgs(button));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public override async Task Render(MessageResult message)
|
||||
{
|
||||
ButtonForm btn = new ButtonForm();
|
||||
|
||||
var buttons = this.Buttons.Select(a => new ButtonBase(a.Text, CallbackData.Create("action", a.Value))).ToList();
|
||||
btn.AddButtonRow(buttons);
|
||||
if (this.Value == null)
|
||||
{
|
||||
await this.Device.Send(this.Message);
|
||||
return;
|
||||
}
|
||||
|
||||
await this.Device.Send(this.Message, btn);
|
||||
|
||||
OnCompleted(new EventArgs());
|
||||
}
|
||||
|
||||
|
||||
public event EventHandler<ButtonClickedEventArgs> ButtonClicked
|
||||
public event EventHandler<EventArgs> Completed
|
||||
{
|
||||
add
|
||||
{
|
||||
this.__Events.AddHandler(__evButtonClicked, value);
|
||||
this.__Events.AddHandler(__evCompleted, value);
|
||||
}
|
||||
remove
|
||||
{
|
||||
this.__Events.RemoveHandler(__evButtonClicked, value);
|
||||
this.__Events.RemoveHandler(__evCompleted, value);
|
||||
}
|
||||
}
|
||||
|
||||
public void OnButtonClicked(ButtonClickedEventArgs e)
|
||||
public void OnCompleted(EventArgs e)
|
||||
{
|
||||
(this.__Events[__evButtonClicked] as EventHandler<ButtonClickedEventArgs>)?.Invoke(this, e);
|
||||
(this.__Events[__evCompleted] as EventHandler<EventArgs>)?.Invoke(this, e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -33,7 +33,7 @@ namespace TelegramBaseTest.Tests
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public override async Task Action(MessageResult message)
|
||||
{
|
||||
@ -43,6 +43,8 @@ namespace TelegramBaseTest.Tests
|
||||
|
||||
await message.DeleteMessage();
|
||||
|
||||
message.Handled = true;
|
||||
|
||||
if (call.Value == "testform1")
|
||||
{
|
||||
|
||||
@ -62,38 +64,28 @@ namespace TelegramBaseTest.Tests
|
||||
|
||||
await this.NavigateTo(ad);
|
||||
}
|
||||
else if (call.Value == "prompt")
|
||||
else if (call.Value == "confirm")
|
||||
{
|
||||
PromptDialog pd = new PromptDialog("Please confirm");
|
||||
|
||||
pd.ButtonClicked += async (s, en) =>
|
||||
{
|
||||
if(en.Button.Value == "ok")
|
||||
{
|
||||
var tf = new TestForm2();
|
||||
await pd.NavigateTo(tf);
|
||||
}
|
||||
else if(en.Button.Value == "cancel")
|
||||
{
|
||||
var tf = new TestForm2();
|
||||
await pd.NavigateTo(tf);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
pd.AddButton(new ButtonBase("Ok", "ok"));
|
||||
pd.AddButton(new ButtonBase("Cancel", "cancel"));
|
||||
|
||||
await this.NavigateTo(pd);
|
||||
}
|
||||
else if (call.Value == "promptevt")
|
||||
{
|
||||
PromptDialog pd = new PromptDialog("Please confirm", new ButtonBase("Ok", "ok"), new ButtonBase("Cancel", "cancel"));
|
||||
ConfirmDialog pd = new ConfirmDialog("Please confirm", new ButtonBase("Ok", "ok"), new ButtonBase("Cancel", "cancel"));
|
||||
|
||||
pd.ButtonClicked += async (s, en) =>
|
||||
{
|
||||
var tf = new TestForm2();
|
||||
|
||||
|
||||
await pd.NavigateTo(tf);
|
||||
};
|
||||
|
||||
await this.NavigateTo(pd);
|
||||
}
|
||||
else if (call.Value == "prompt")
|
||||
{
|
||||
PromptDialog pd = new PromptDialog("Please tell me your name ?");
|
||||
|
||||
pd.Completed += async (s, en) =>
|
||||
{
|
||||
await this.Device.Send("Hello " + pd.Value);
|
||||
|
||||
var tf = new TestForm2();
|
||||
await pd.NavigateTo(tf);
|
||||
};
|
||||
|
||||
@ -124,9 +116,9 @@ namespace TelegramBaseTest.Tests
|
||||
|
||||
btn.AddButtonRow(new ButtonBase("Information Prompt", CallbackData.Create("navigate", "alert")));
|
||||
|
||||
btn.AddButtonRow(new ButtonBase("Confirmation Prompt without event", CallbackData.Create("navigate", "prompt")));
|
||||
btn.AddButtonRow(new ButtonBase("Confirmation Prompt with event", CallbackData.Create("navigate", "confirm")));
|
||||
|
||||
btn.AddButtonRow(new ButtonBase("Confirmation Prompt with event", CallbackData.Create("navigate", "promptevt")));
|
||||
btn.AddButtonRow(new ButtonBase("Request Prompt", CallbackData.Create("navigate", "prompt")));
|
||||
|
||||
|
||||
await this.Device.SendPhoto(bmp, "Test", btn);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user