Adding callbackdata too long exception and checks
This commit is contained in:
parent
e345fc2948
commit
5ce2360cc7
@ -16,4 +16,9 @@ public static class Telegram
|
|||||||
public const int MaxReplyKeyboardCols = 12;
|
public const int MaxReplyKeyboardCols = 12;
|
||||||
|
|
||||||
public const int MessageDeletionsPerSecond = 30;
|
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;
|
||||||
}
|
}
|
||||||
31
TelegramBotBase/Exceptions/CallbackDataTooLongException.cs
Normal file
31
TelegramBotBase/Exceptions/CallbackDataTooLongException.cs
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
using System;
|
||||||
|
using Telegram.Bot.Exceptions;
|
||||||
|
|
||||||
|
namespace TelegramBotBase.Exceptions;
|
||||||
|
|
||||||
|
public sealed class CallbackDataTooLongException : Exception
|
||||||
|
{
|
||||||
|
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";
|
||||||
|
|
||||||
|
static String _message_with_bytes = $"You have exceeded the maximum {Constants.Telegram.MaxCallBackDataBytes} bytes of callback data with @current_callback_bytes@ bytes.\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)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public CallbackDataTooLongException(int current_callback_bytes) : base(getMessage(current_callback_bytes), _innerException)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
static String getMessage(int current_callback_bytes = -1)
|
||||||
|
{
|
||||||
|
if (current_callback_bytes == -1)
|
||||||
|
return _message;
|
||||||
|
|
||||||
|
return _message_with_bytes.Replace("@current_callback_bytes@", current_callback_bytes.ToString());
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,4 +1,6 @@
|
|||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
using System.Text;
|
||||||
|
using TelegramBotBase.Exceptions;
|
||||||
|
|
||||||
namespace TelegramBotBase.Form;
|
namespace TelegramBotBase.Form;
|
||||||
|
|
||||||
@ -23,22 +25,24 @@ public class CallbackData
|
|||||||
|
|
||||||
public static string Create(string method, string value)
|
public static string Create(string method, string value)
|
||||||
{
|
{
|
||||||
return new CallbackData(method, value).Serialize();
|
return new CallbackData(method, value).Serialize(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Serializes data to json string
|
/// Serializes data to json string
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public string Serialize()
|
public string Serialize(bool throwExceptionOnOverflow = false)
|
||||||
{
|
{
|
||||||
var s = "";
|
var s = string.Empty;
|
||||||
try
|
|
||||||
{
|
s = JsonConvert.SerializeObject(this);
|
||||||
s = JsonConvert.SerializeObject(this);
|
|
||||||
}
|
//Is data over 64 bytes ?
|
||||||
catch
|
int byte_count = Encoding.UTF8.GetByteCount(s);
|
||||||
|
if (throwExceptionOnOverflow && byte_count > Constants.Telegram.MaxCallBackDataBytes)
|
||||||
{
|
{
|
||||||
|
throw new CallbackDataTooLongException(byte_count);
|
||||||
}
|
}
|
||||||
|
|
||||||
return s;
|
return s;
|
||||||
@ -51,19 +55,8 @@ public class CallbackData
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static CallbackData Deserialize(string data)
|
public static CallbackData Deserialize(string data)
|
||||||
{
|
{
|
||||||
CallbackData cd = null;
|
return JsonConvert.DeserializeObject<CallbackData>(data);
|
||||||
try
|
|
||||||
{
|
|
||||||
cd = JsonConvert.DeserializeObject<CallbackData>(data);
|
|
||||||
|
|
||||||
return cd;
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
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