Compare commits

...
8 Commits
Author SHA1 Message Date
FlorianDahn a6d22cdd82 Update version 2019-09-17 16:44:14 +02:00
FlorianDahn ee4a3d765c Merge branch 'master' of https://github.com/MajMcCloud/TelegramBotFramework 2019-09-17 16:43:10 +02:00
FlorianDahn 703c99eb8d - added a custom exception for maximum length messages, saves to time to sent to API
- added a constants file, for have constants to work with
- catching maximum message length
2019-09-17 16:43:02 +02:00
Florian Dahn 138fee9772 Update README.md
adding paypal
2019-09-17 16:33:51 +02:00
Florian Dahn d60399514f Update README.md 2019-09-04 15:13:42 +02:00
FlorianDahn ae0fa3b0e3 Added simple Markdown/HTML Generator
Use with:

"Bold Text".Bold();

"Other text".Italic();

"www.google.de".Link("Google")
2019-08-28 17:33:18 +02:00
FlorianDahn 6a2ac7a309 Added simple Markdown/HTML Generator
Use with:

"Bold Text".Bold();

"Other text".Italic();

"www.google.de".AsLink("Google")
2019-08-28 17:32:08 +02:00
FlorianDahn 1a76b2b3f8 added some forms to obsolete, cause their use is bad for ram management 2019-08-26 01:34:50 +02:00
9 changed files with 209 additions and 10 deletions
+17
View File
@@ -17,6 +17,23 @@ Download a release: [Releases](https://github.com/MajMcCloud/TelegramBotFramewor
---
Donations
Bitcoin: 1NqyGWmZg8HLp9qQELbf6bChEoba3mxaFc
ETH: 0x795B70CFC27C69603ce115F2b450cbAC5a5460D0
Litecoin: LM5iCN6Nz22wAi8LSFnKgdGGWEtxWfJZXv
DASH: Xb2qyVefvbKTXusHoxZ4Soja2S6R4vLsSr
Paypal: https://paypal.me/majmccloud
Thanks !
---
## Index
- [Introduction](#introduction)
- [How to Start](#how-to-start)
+17
View File
@@ -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
{
/// <summary>
/// The maximum length of message text before the API throws an exception. (We will catch it before)
/// </summary>
public const int MaxMessageLength = 4096;
}
}
@@ -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.")
{
}
}
}
+8
View File
@@ -15,6 +15,14 @@ namespace TelegramBotBase.Form
{
public String ButtonText { get; set; }
public AlertDialog(String Message, String ButtonText) : base(Message)
{
this.Buttons.Add(new ButtonBase(ButtonText, "ok"));
this.ButtonText = ButtonText;
}
[Obsolete]
public AlertDialog(String Message, String ButtonText, FormBase FormToOpen = null) : base(Message)
{
this.Buttons.Add(new ButtonBase(ButtonText, "ok"));
@@ -18,6 +18,7 @@ namespace TelegramBotBase.Form
public ButtonBase[][] Buttons { get; set; }
[Obsolete]
public Dictionary<String, FormBase> ButtonForms { get; set; } = new Dictionary<string, FormBase>();
private EventHandlerList __Events { get; set; } = new EventHandlerList();
+1
View File
@@ -15,6 +15,7 @@ namespace TelegramBotBase.Form
public List<ButtonBase> Buttons { get; set; }
[Obsolete]
public Dictionary<String, FormBase> ButtonForms { get; set; } = new Dictionary<string, FormBase>();
private EventHandlerList __Events { get; set; } = new EventHandlerList();
+112
View File
@@ -0,0 +1,112 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Telegram.Bot.Types.Enums;
namespace TelegramBotBase.Markdown
{
public static class Generator
{
public static ParseMode OutputMode { get; set; } = ParseMode.Markdown;
/// <summary>
/// Generates a link with title in Markdown or HTML
/// </summary>
/// <param name="url"></param>
/// <param name="title"></param>
/// <param name="tooltip"></param>
/// <returns></returns>
public static String Link(this String url, String title = null, String tooltip = null)
{
switch (OutputMode)
{
case ParseMode.Markdown:
return "[" + (title ?? url) + "](" + url + " " + (tooltip ?? "") + ")";
case ParseMode.Html:
return $"<a href=\"{url}\" title=\"{tooltip ?? ""}\">{title ?? ""}</b>";
}
return url;
}
/// <summary>
/// Returns a Link to the User, title is optional.
/// </summary>
/// <param name="userId"></param>
/// <param name="title"></param>
/// <returns></returns>
public static String MentionUser(this long userId, String title = null)
{
return Link("tg://user?id=" + userId.ToString(), title);
}
/// <summary>
/// Returns a bold link in Markdown or HTML
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public static String Bold(this String text)
{
switch (OutputMode)
{
case ParseMode.Markdown:
return "**" + text + "**";
case ParseMode.Html:
return "<b>" + text + "</b>";
}
return text;
}
/// <summary>
/// Returns a italic link in Markdown or HTML
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public static String Italic(this String text)
{
switch (OutputMode)
{
case ParseMode.Markdown:
return "__" + text + "__";
case ParseMode.Html:
return "<i>" + text + "</i>";
}
return text;
}
/// <summary>
/// Returns a monospace link in Markdown or HTML
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public static String Monospace(this String text)
{
switch (OutputMode)
{
case ParseMode.Markdown:
return "`" + text + "`";
case ParseMode.Html:
return "<code>" + text + "</code>";
}
return text;
}
/// <summary>
/// Returns a multi monospace link in Markdown or HTML
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public static String MultiMonospace(this String text)
{
switch (OutputMode)
{
case ParseMode.Markdown:
return "```" + text + "```";
case ParseMode.Html:
return "<pre>" + text + "</pre>";
}
return text;
}
}
}
+2 -2
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("1.5.0.0")]
[assembly: AssemblyFileVersion("1.5.0.0")]
[assembly: AssemblyVersion("1.5.2.0")]
[assembly: AssemblyFileVersion("1.5.2.0")]
+34 -8
View File
@@ -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
/// <param name="replyTo"></param>
/// <param name="disableNotification"></param>
/// <returns></returns>
public async Task<Message> Send(String text, ButtonForm buttons = null, int replyTo = 0, bool disableNotification = false)
public async Task<Message> 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
/// <param name="replyTo"></param>
/// <param name="disableNotification"></param>
/// <returns></returns>
public async Task<Message> Send(String text, InlineKeyboardMarkup markup, int replyTo = 0, bool disableNotification = false)
public async Task<Message> 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
/// <param name="replyTo"></param>
/// <param name="disableNotification"></param>
/// <returns></returns>
public async Task<Message> Send(String text, ReplyKeyboardMarkup markup, int replyTo = 0, bool disableNotification = false)
public async Task<Message> 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
/// <param name="replyTo"></param>
/// <param name="disableNotification"></param>
/// <returns></returns>
public async Task<Message> SendPhoto(InputOnlineFile file, ButtonForm buttons = null, int replyTo = 0, bool disableNotification = false)
public async Task<Message> 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));
}