Adding exception for too long callback data.
- right now only in DEBUG compilation and only for the static operator
This commit is contained in:
parent
d572c353f6
commit
53886fa6bf
@ -16,4 +16,9 @@ public static class Telegram
|
||||
public const int MaxReplyKeyboardCols = 12;
|
||||
|
||||
public const int MessageDeletionsPerSecond = 30;
|
||||
|
||||
/// <summary>
|
||||
/// The maximum length of callback data. Will raise an exception of it exceeds it.
|
||||
/// </summary>
|
||||
public const int MaxCallBackDataBytes = 64;
|
||||
}
|
||||
21
TelegramBotBase/Exceptions/CallbackDataTooLongException.cs
Normal file
21
TelegramBotBase/Exceptions/CallbackDataTooLongException.cs
Normal file
@ -0,0 +1,21 @@
|
||||
using System;
|
||||
using Telegram.Bot.Exceptions;
|
||||
|
||||
namespace TelegramBotBase.Exceptions;
|
||||
|
||||
public sealed class CallbackDataTooLongException : Exception
|
||||
{
|
||||
//public override string Message =>
|
||||
// $"You have exceeded the maximum {Constants.Telegram.MaxCallBackDataBytes} bytes.";
|
||||
|
||||
static ApiRequestException _innerException = new Telegram.Bot.Exceptions.ApiRequestException("Bad Request: BUTTON_DATA_INVALID", 400);
|
||||
|
||||
static String _message = $"You have exceeded the maximum {Constants.Telegram.MaxCallBackDataBytes} bytes of callback data.\r\nThis is a pre-sending message from the TelegramBotBase framework.\r\nread more: https://core.telegram.org/bots/api#inlinekeyboardbutton";
|
||||
|
||||
public CallbackDataTooLongException() : base(_message, _innerException)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@ -1,4 +1,6 @@
|
||||
using Newtonsoft.Json;
|
||||
using System.Text;
|
||||
using TelegramBotBase.Exceptions;
|
||||
|
||||
namespace TelegramBotBase.Form;
|
||||
|
||||
@ -30,7 +32,7 @@ public class CallbackData
|
||||
/// Serializes data to json string
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public string Serialize()
|
||||
public string Serialize(bool throwExceptionOnOverflow = false)
|
||||
{
|
||||
var s = "";
|
||||
try
|
||||
@ -41,6 +43,16 @@ public class CallbackData
|
||||
{
|
||||
}
|
||||
|
||||
#if DEBUG
|
||||
|
||||
//Data is over 64 bytes
|
||||
if(throwExceptionOnOverflow && Encoding.UTF8.GetByteCount(s) > Constants.Telegram.MaxCallBackDataBytes)
|
||||
{
|
||||
throw new CallbackDataTooLongException();
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
@ -65,5 +77,5 @@ public class CallbackData
|
||||
return null;
|
||||
}
|
||||
|
||||
public static implicit operator string(CallbackData callbackData) => callbackData.Serialize();
|
||||
public static implicit operator string(CallbackData callbackData) => callbackData.Serialize(true);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user