diff --git a/TelegramBotBase/Constants/Telegram.cs b/TelegramBotBase/Constants/Telegram.cs
new file mode 100644
index 0000000..c2287a2
--- /dev/null
+++ b/TelegramBotBase/Constants/Telegram.cs
@@ -0,0 +1,17 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace TelegramBotBase.Constants
+{
+ public static class Telegram
+ {
+ ///
+ /// The maximum length of message text before the API throws an exception. (We will catch it before)
+ ///
+ public const int MaxMessageLength = 4096;
+
+ }
+}
diff --git a/TelegramBotBase/Exceptions/MaxLengthException.cs b/TelegramBotBase/Exceptions/MaxLengthException.cs
new file mode 100644
index 0000000..4263a65
--- /dev/null
+++ b/TelegramBotBase/Exceptions/MaxLengthException.cs
@@ -0,0 +1,17 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace TelegramBotBase.Exceptions
+{
+ public class MaxLengthException : Exception
+ {
+ public MaxLengthException(int length) : base($"Your messages with a length of {length} is too long for telegram. Actually is {Constants.Telegram.MaxMessageLength} characters allowed. Please split it.")
+ {
+
+ }
+
+ }
+}
diff --git a/TelegramBotBase/Sessions/DeviceSession.cs b/TelegramBotBase/Sessions/DeviceSession.cs
index c8100cc..2bec7ef 100644
--- a/TelegramBotBase/Sessions/DeviceSession.cs
+++ b/TelegramBotBase/Sessions/DeviceSession.cs
@@ -13,6 +13,7 @@ using Telegram.Bot.Types.Enums;
using Telegram.Bot.Types.InputFiles;
using Telegram.Bot.Types.ReplyMarkups;
using TelegramBotBase.Base;
+using TelegramBotBase.Exceptions;
using TelegramBotBase.Form;
namespace TelegramBotBase.Sessions
@@ -129,6 +130,11 @@ namespace TelegramBotBase.Sessions
InlineKeyboardMarkup markup = buttons;
+ if (text.Length > Constants.Telegram.MaxMessageLength)
+ {
+ throw new MaxLengthException(text.Length);
+ }
+
try
{
var m = await this.Client.TelegramClient.EditMessageTextAsync(this.DeviceId, messageId, text, replyMarkup: markup);
@@ -158,6 +164,11 @@ namespace TelegramBotBase.Sessions
InlineKeyboardMarkup markup = buttons;
+ if (message.Text.Length > Constants.Telegram.MaxMessageLength)
+ {
+ throw new MaxLengthException(message.Text.Length);
+ }
+
try
{
var m = await this.Client.TelegramClient.EditMessageTextAsync(this.DeviceId, message.MessageId, message.Text, replyMarkup: markup);
@@ -181,7 +192,7 @@ namespace TelegramBotBase.Sessions
///
///
///
- public async Task Send(String text, ButtonForm buttons = null, int replyTo = 0, bool disableNotification = false)
+ public async Task Send(String text, ButtonForm buttons = null, int replyTo = 0, bool disableNotification = false, ParseMode parseMode = ParseMode.Default)
{
if (this.ActiveForm == null)
return null;
@@ -190,9 +201,14 @@ namespace TelegramBotBase.Sessions
Message m = null;
+ if (text.Length > Constants.Telegram.MaxMessageLength)
+ {
+ throw new MaxLengthException(text.Length);
+ }
+
try
{
- m = await (this.Client.TelegramClient.SendTextMessageAsync(this.DeviceId, text, replyToMessageId: replyTo, replyMarkup: markup, disableNotification: disableNotification));
+ m = await (this.Client.TelegramClient.SendTextMessageAsync(this.DeviceId, text, parseMode, replyToMessageId: replyTo, replyMarkup: markup, disableNotification: disableNotification));
OnMessageSent(new MessageSentEventArgs(m));
}
@@ -216,16 +232,21 @@ namespace TelegramBotBase.Sessions
///
///
///
- public async Task Send(String text, InlineKeyboardMarkup markup, int replyTo = 0, bool disableNotification = false)
+ public async Task Send(String text, InlineKeyboardMarkup markup, int replyTo = 0, bool disableNotification = false, ParseMode parseMode = ParseMode.Default)
{
if (this.ActiveForm == null)
return null;
Message m = null;
+ if (text.Length > Constants.Telegram.MaxMessageLength)
+ {
+ throw new MaxLengthException(text.Length);
+ }
+
try
{
- m = await (this.Client.TelegramClient.SendTextMessageAsync(this.DeviceId, text, replyToMessageId: replyTo, replyMarkup: markup, disableNotification: disableNotification));
+ m = await (this.Client.TelegramClient.SendTextMessageAsync(this.DeviceId, text, parseMode, replyToMessageId: replyTo, replyMarkup: markup, disableNotification: disableNotification));
OnMessageSent(new MessageSentEventArgs(m));
}
@@ -249,16 +270,21 @@ namespace TelegramBotBase.Sessions
///
///
///
- public async Task Send(String text, ReplyKeyboardMarkup markup, int replyTo = 0, bool disableNotification = false)
+ public async Task Send(String text, ReplyKeyboardMarkup markup, int replyTo = 0, bool disableNotification = false, ParseMode parseMode = ParseMode.Default)
{
if (this.ActiveForm == null)
return null;
Message m = null;
+ if (text.Length > Constants.Telegram.MaxMessageLength)
+ {
+ throw new MaxLengthException(text.Length);
+ }
+
try
{
- m = await (this.Client.TelegramClient.SendTextMessageAsync(this.DeviceId, text, replyToMessageId: replyTo, replyMarkup: markup, disableNotification: disableNotification));
+ m = await (this.Client.TelegramClient.SendTextMessageAsync(this.DeviceId, text, parseMode, replyToMessageId: replyTo, replyMarkup: markup, disableNotification: disableNotification));
OnMessageSent(new MessageSentEventArgs(m));
}
@@ -282,7 +308,7 @@ namespace TelegramBotBase.Sessions
///
///
///
- public async Task SendPhoto(InputOnlineFile file, ButtonForm buttons = null, int replyTo = 0, bool disableNotification = false)
+ public async Task SendPhoto(InputOnlineFile file, ButtonForm buttons = null, int replyTo = 0, bool disableNotification = false, ParseMode parseMode = ParseMode.Default)
{
if (this.ActiveForm == null)
return null;
@@ -293,7 +319,7 @@ namespace TelegramBotBase.Sessions
try
{
- m = await this.Client.TelegramClient.SendPhotoAsync(this.DeviceId, file, replyToMessageId: replyTo, replyMarkup: markup, disableNotification: disableNotification);
+ m = await this.Client.TelegramClient.SendPhotoAsync(this.DeviceId, file, parseMode: parseMode, replyToMessageId: replyTo, replyMarkup: markup, disableNotification: disableNotification);
OnMessageSent(new MessageSentEventArgs(m));
}