-update on FormBase which should prevent issues on control management

This commit is contained in:
FlorianDahn 2019-12-02 03:46:40 +01:00
parent 7be0f15ca4
commit a08ba8f512
13 changed files with 182 additions and 31 deletions

12
.gitignore vendored
View File

@ -240,3 +240,15 @@ ModelManifest.xml
# FAKE - F# Make
.fake/
MigrationBackup
TelegramBotBase/nuget.exe
Material
TelegramBotBase/cp.ps1
TelegramBotBase/TelegramBotBase.nuspec
Program.cs
TelegramBotBase/Archive
/TelegramBotBase/TelegramBotBase.1.5.0.zip
/TelegramBotBase/TelegramBotBase.1.5.1.zip
/TelegramBotBase/TelegramBotBase.1.5.2.zip
/TelegramBotBase/cpush.ps1
/TelegramBotBase/cpack.ps1

View File

@ -291,7 +291,7 @@ namespace TelegramBotBase.Form
await newForm.OnInit(new InitEventArgs(args));
await this.CloseControls();
this.CloseControls().Wait();
await this.OnClosed(new EventArgs());

View File

@ -8,6 +8,7 @@ using Telegram.Bot.Types;
using Telegram.Bot.Types.ReplyMarkups;
using TelegramBotBase.Base;
using TelegramBotBase.Enums;
using TelegramBotBase.Exceptions;
using TelegramBotBase.Form;
namespace TelegramBotBase.Controls
@ -27,6 +28,15 @@ namespace TelegramBotBase.Controls
public int? MessageId { get; set; }
/// <summary>
/// Optional. Requests clients to resize the keyboard vertically for optimal fit (e.g., make the keyboard smaller if there are just two rows of buttons). Defaults to false, in which case the custom keyboard is always of the same height as the app's standard keyboard.
/// Source: https://core.telegram.org/bots/api#replykeyboardmarkup
/// </summary>
public bool ResizeKeyboard { get; set; } = false;
public bool OneTimeKeyboard { get; set; } = false;
/// <summary>
/// Defines which type of Button Keyboard should be rendered.
/// </summary>
@ -39,7 +49,7 @@ namespace TelegramBotBase.Controls
}
set
{
if(m_eKeyboardType != value)
if (m_eKeyboardType != value)
{
this.RenderNecessary = true;
@ -47,7 +57,7 @@ namespace TelegramBotBase.Controls
m_eKeyboardType = value;
}
}
}
@ -133,6 +143,36 @@ namespace TelegramBotBase.Controls
if (!this.RenderNecessary)
return;
switch (m_eKeyboardType)
{
case eKeyboardType.InlineKeyBoard:
if (ButtonsForm.Rows > 13)
{
throw new MaximumRowsReachedException() { Value = ButtonsForm.Rows, Maximum = 13 };
}
if (ButtonsForm.Rows > 8)
{
throw new MaximumColsException() { Value = ButtonsForm.Rows, Maximum = 8 };
}
break;
case eKeyboardType.ReplyKeyboard:
if (ButtonsForm.Rows > 25)
{
throw new MaximumRowsReachedException() { Value = ButtonsForm.Rows, Maximum = 25 };
}
if (ButtonsForm.Rows > 12)
{
throw new MaximumColsException() { Value = ButtonsForm.Rows, Maximum = 12 };
}
break;
}
Message m = null;
if (this.MessageId != null)
{
@ -142,12 +182,15 @@ namespace TelegramBotBase.Controls
case eKeyboardType.ReplyKeyboard:
if (this.ButtonsForm.Count == 0)
{
await this.Device.Send("", new ReplyKeyboardRemove());
await this.Device.HideReplyKeyboard();
this.MessageId = null;
}
else
{
m = await this.Device.Send(this.Title, (ReplyKeyboardMarkup)this.ButtonsForm, disableNotification: true);
var rkm = (ReplyKeyboardMarkup)this.ButtonsForm;
rkm.ResizeKeyboard = this.ResizeKeyboard;
rkm.OneTimeKeyboard = this.OneTimeKeyboard;
m = await this.Device.Send(this.Title, rkm, disableNotification: true);
}
break;
@ -164,7 +207,10 @@ namespace TelegramBotBase.Controls
switch (this.KeyboardType)
{
case eKeyboardType.ReplyKeyboard:
m = await this.Device.Send(this.Title, (ReplyKeyboardMarkup)this.ButtonsForm, disableNotification: true);
var rkm = (ReplyKeyboardMarkup)this.ButtonsForm;
rkm.ResizeKeyboard = this.ResizeKeyboard;
rkm.OneTimeKeyboard = this.OneTimeKeyboard;
m = await this.Device.Send(this.Title, rkm, disableNotification: true);
break;
case eKeyboardType.InlineKeyBoard:
@ -194,7 +240,7 @@ namespace TelegramBotBase.Controls
if (this.KeyboardType == eKeyboardType.ReplyKeyboard)
{
await this.Device.Send("", new ReplyKeyboardRemove());
await this.Device.HideReplyKeyboard(autoDeleteResponse: false);
}
}

View File

@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace TelegramBotBase.Exceptions
{
public class MaximumColsException : Exception
{
public int Value { get; set; }
public int Maximum { get; set; }
public override string Message
{
get
{
return $"You have exceeded the maximum of columns by {Value.ToString()} / {Maximum.ToString()}";
}
}
}
}

View File

@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace TelegramBotBase.Exceptions
{
public class MaximumRowsReachedException : Exception
{
public int Value { get; set; }
public int Maximum { get; set; }
public override string Message
{
get
{
return $"You have exceeded the maximum of rows by {Value.ToString()} / {Maximum.ToString()}";
}
}
}
}

View File

@ -56,7 +56,7 @@ namespace TelegramBotBase.Form
var buttons = this.Buttons.Aggregate((a, b) => a.Union(b).ToArray()).ToList();
if(call==null)
if (call == null)
{
return;
}
@ -83,7 +83,7 @@ namespace TelegramBotBase.Form
{
ButtonForm btn = new ButtonForm();
foreach(var bl in this.Buttons)
foreach (var bl in this.Buttons)
{
btn.AddButtonRow(bl.Select(a => new ButtonBase(a.Text, CallbackData.Create("action", a.Value))).ToList());
}

View File

@ -20,6 +20,28 @@ namespace TelegramBotBase.Form
public ControlBase DependencyControl { get; set; }
/// <summary>
/// Contains the number of rows.
/// </summary>
public int Rows
{
get
{
return Buttons.Count;
}
}
/// <summary>
/// Contains the highest number of columns in an row.
/// </summary>
public int Cols
{
get
{
return Buttons.Select(a => a.Count).OrderByDescending(a => a).FirstOrDefault();
}
}
public ButtonForm()
{

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("2.0.0.0")]
[assembly: AssemblyFileVersion("2.0.0.0")]
[assembly: AssemblyVersion("2.1.0.0")]
[assembly: AssemblyFileVersion("2.1.0.0")]

View File

@ -492,6 +492,26 @@ namespace TelegramBotBase.Sessions
return await this.Client.TelegramClient.SendTextMessageAsync(this.DeviceId, requestMessage, replyMarkup: rcl);
}
public async Task<Message> HideReplyKeyboard(String closedMsg = "Closed", bool autoDeleteResponse = true)
{
try
{
var m = await this.Send(closedMsg, new ReplyKeyboardRemove());
if (autoDeleteResponse && m != null)
{
await this.DeleteMessage(m);
}
return m;
}
catch
{
}
return null;
}
/// <summary>
/// Deletes a message
/// </summary>

Binary file not shown.

Binary file not shown.

View File

@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net461</TargetFrameworks>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<PackageProjectUrl>https://github.com/MajMcCloud/TelegramBotFramework</PackageProjectUrl>
<RepositoryUrl>https://github.com/MajMcCloud/TelegramBotFramework</RepositoryUrl>
@ -63,12 +63,16 @@
<None Remove="TelegramBotBase.1.5.1.zip" />
<None Remove="TelegramBotBase.1.5.2.nupkg" />
<None Remove="TelegramBotBase.1.5.2.zip" />
<None Remove="TelegramBotBase.2.0.0.nupkg" />
<None Remove="TelegramBotBase.2.0.0.zip" />
<None Remove="TelegramBotBase.2.1.0.nupkg" />
<None Remove="TelegramBotBase.2.1.0.zip" />
<None Remove="TelegramBotBase.nuspec" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
<PackageReference Include="System.Drawing.Common" Version="4.6.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="System.Drawing.Common" Version="4.6.1" />
<PackageReference Include="Telegram.Bot" Version="15.0.0" />
</ItemGroup>

View File

@ -1,24 +1,27 @@
<?xml version="1.0"?>
<package >
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
<metadata>
<id>$id$</id>
<version>$version$</version>
<title>$title$</title>
<authors>$author$</authors>
<owners>$author$</owners>
<id>TelegramBotBase</id>
<version>2.1.0</version>
<authors>TelegramBotBase</authors>
<owners>TelegramBotBase</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<license type="expression">MIT</license>
<projectUrl>https://github.com/MajMcCloud/TelegramBotFramework</projectUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>$description$</description>
<releaseNotes>This is a context based application framework for the C# TelegramBot library.</releaseNotes>
<copyright>Copyright 2019</copyright>
<tags>Telegram Bot Framework C# Addon Context Modules</tags>
<description>Package Description</description>
<releaseNotes>- moving from .Net Framework 4.7.2 to .Net Standard 2.1 for the Library and .Net Core 3.0 for the test project!</releaseNotes>
<repository url="https://github.com/MajMcCloud/TelegramBotFramework" />
<dependencies>
<dependency id="Telegram.Bot" version="14.3.0" />
<dependency id="Newtonsoft.Json" version="11.0.2" />
<dependency id="System.Net.Requests" version="4.3.0" />
<group targetFramework=".NETFramework4.6.1">
<dependency id="Newtonsoft.Json" version="12.0.2" exclude="Build,Analyzers" />
<dependency id="System.Drawing.Common" version="4.6.0" exclude="Build,Analyzers" />
<dependency id="Telegram.Bot" version="15.0.0" exclude="Build,Analyzers" />
</group>
<group targetFramework=".NETStandard2.0">
<dependency id="Newtonsoft.Json" version="12.0.2" exclude="Build,Analyzers" />
<dependency id="System.Drawing.Common" version="4.6.0" exclude="Build,Analyzers" />
<dependency id="Telegram.Bot" version="15.0.0" exclude="Build,Analyzers" />
</group>
</dependencies>
<repository type="git"
url="https://github.com/MajMcCloud/TelegramBotFramework" />
</metadata>
</package>