From a7f43ba4329184b826e3f83e17b8f480856911cc Mon Sep 17 00:00:00 2001 From: Ronny Gunawan <3048897+ronnygunawan@users.noreply.github.com> Date: Tue, 31 Oct 2023 19:34:52 +0700 Subject: [PATCH] Use syntax highlighting --- BotNet.Services/BotCommands/Exec.cs | 14 +++----- BotNet.Services/BotCommands/OpenAI.cs | 36 ++++--------------- .../MarkdownV2/MarkdownV2Sanitizer.cs | 29 +++++++++++++++ 3 files changed, 41 insertions(+), 38 deletions(-) create mode 100644 BotNet.Services/MarkdownV2/MarkdownV2Sanitizer.cs diff --git a/BotNet.Services/BotCommands/Exec.cs b/BotNet.Services/BotCommands/Exec.cs index 0ee35b4..18eba71 100644 --- a/BotNet.Services/BotCommands/Exec.cs +++ b/BotNet.Services/BotCommands/Exec.cs @@ -1,16 +1,12 @@ using System; -using System.Globalization; using System.Linq; using System.Net; using System.Threading; using System.Threading.Tasks; -using BotNet.Services.Pesto; -using BotNet.Services.Pesto.Exceptions; -using BotNet.Services.Pesto.Models; +using BotNet.Services.MarkdownV2; using BotNet.Services.Piston; using BotNet.Services.Piston.Models; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; using Telegram.Bot; using Telegram.Bot.Types; using Telegram.Bot.Types.Enums; @@ -52,8 +48,8 @@ await botClient.SendTextMessageAsync( } else { await botClient.SendTextMessageAsync( chatId: message.Chat.Id, - text: $"Code:\n{WebUtility.HtmlEncode(commandArgument)}\n\nOutput:\n{WebUtility.HtmlEncode(result.Run.Output)}", - parseMode: ParseMode.Html, + text: $"Code:\n```{language}\n{MarkdownV2Sanitizer.Sanitize(commandArgument)}\n```\n\nOutput:\n```\n{MarkdownV2Sanitizer.Sanitize(result.Run.Output)}\n```", + parseMode: ParseMode.MarkdownV2, replyToMessageId: message.MessageId, cancellationToken: cancellationToken); } @@ -103,8 +99,8 @@ await botClient.SendTextMessageAsync( } else { await botClient.SendTextMessageAsync( chatId: message.Chat.Id, - text: $"Code:\n{WebUtility.HtmlEncode(repliedToMessage)}\n\nOutput:\n{WebUtility.HtmlEncode(result.Run.Output)}", - parseMode: ParseMode.Html, + text: $"Code:\n```{language}\n{MarkdownV2Sanitizer.Sanitize(repliedToMessage)}\n```\n\nOutput:\n```\n{MarkdownV2Sanitizer.Sanitize(result.Run.Output)}\n```", + parseMode: ParseMode.MarkdownV2, replyToMessageId: message.ReplyToMessage.MessageId, cancellationToken: cancellationToken); } diff --git a/BotNet.Services/BotCommands/OpenAI.cs b/BotNet.Services/BotCommands/OpenAI.cs index 072e538..1686981 100644 --- a/BotNet.Services/BotCommands/OpenAI.cs +++ b/BotNet.Services/BotCommands/OpenAI.cs @@ -6,6 +6,7 @@ using System.Text; using System.Threading; using System.Threading.Tasks; +using BotNet.Services.MarkdownV2; using BotNet.Services.OpenAI; using BotNet.Services.RateLimit; using Microsoft.Extensions.DependencyInjection; @@ -388,7 +389,7 @@ await botClient.SendTextMessageAsync( if (attachments.Count == 0) { return await botClient.SendTextMessageAsync( chatId: message.Chat.Id, - text: SanitizeForMarkdownV2(result), + text: MarkdownV2Sanitizer.Sanitize(result), parseMode: ParseMode.MarkdownV2, replyToMessageId: message.MessageId, cancellationToken: cancellationToken); @@ -396,14 +397,14 @@ await botClient.SendTextMessageAsync( return await botClient.SendPhotoAsync( chatId: message.Chat.Id, photo: new InputOnlineFile(attachments[0]), - caption: SanitizeForMarkdownV2(result), + caption: MarkdownV2Sanitizer.Sanitize(result), parseMode: ParseMode.MarkdownV2, replyToMessageId: message.MessageId, cancellationToken: cancellationToken); } else { Message sentMessage = await botClient.SendTextMessageAsync( chatId: message.Chat.Id, - text: SanitizeForMarkdownV2(result), + text: MarkdownV2Sanitizer.Sanitize(result), parseMode: ParseMode.MarkdownV2, replyToMessageId: message.MessageId, cancellationToken: cancellationToken); @@ -460,7 +461,7 @@ await botClient.SendTextMessageAsync( if (attachments.Count == 0) { return await botClient.SendTextMessageAsync( chatId: message.Chat.Id, - text: SanitizeForMarkdownV2(result), + text: MarkdownV2Sanitizer.Sanitize(result), parseMode: ParseMode.MarkdownV2, replyToMessageId: message.MessageId, cancellationToken: cancellationToken); @@ -468,14 +469,14 @@ await botClient.SendTextMessageAsync( return await botClient.SendPhotoAsync( chatId: message.Chat.Id, photo: new InputOnlineFile(attachments[0]), - caption: SanitizeForMarkdownV2(result), + caption: MarkdownV2Sanitizer.Sanitize(result), parseMode: ParseMode.MarkdownV2, replyToMessageId: message.MessageId, cancellationToken: cancellationToken); } else { Message sentMessage = await botClient.SendTextMessageAsync( chatId: message.Chat.Id, - text: SanitizeForMarkdownV2(result), + text: MarkdownV2Sanitizer.Sanitize(result), parseMode: ParseMode.MarkdownV2, replyToMessageId: message.MessageId, cancellationToken: cancellationToken); @@ -759,28 +760,5 @@ await botClient.SendTextMessageAsync( } } } - - private static readonly HashSet CHARACTERS_TO_ESCAPE = new() { - '_', '*', '[', ']', '(', ')', '~', '>', '#', - '+', '-', '=', '|', '{', '}', '.', '!' - }; - - private static string SanitizeForMarkdownV2(string input) { - if (string.IsNullOrEmpty(input)) - return input; - - // Use StringBuilder for efficient string manipulation - StringBuilder sanitized = new(input.Length); - - foreach (char character in input) { - // If the character is in our list, append a backslash before it - if (CHARACTERS_TO_ESCAPE.Contains(character)) { - sanitized.Append('\\'); - } - sanitized.Append(character); - } - - return sanitized.ToString(); - } } } diff --git a/BotNet.Services/MarkdownV2/MarkdownV2Sanitizer.cs b/BotNet.Services/MarkdownV2/MarkdownV2Sanitizer.cs new file mode 100644 index 0000000..27906cb --- /dev/null +++ b/BotNet.Services/MarkdownV2/MarkdownV2Sanitizer.cs @@ -0,0 +1,29 @@ +using System.Collections.Generic; +using System.Text; + +namespace BotNet.Services.MarkdownV2 { + public static class MarkdownV2Sanitizer { + private static readonly HashSet CHARACTERS_TO_ESCAPE = new() { + '_', '*', '[', ']', '(', ')', '~', '>', '#', + '+', '-', '=', '|', '{', '}', '.', '!' + }; + + public static string Sanitize(string input) { + if (string.IsNullOrEmpty(input)) + return input; + + // Use StringBuilder for efficient string manipulation + StringBuilder sanitized = new(input.Length); + + foreach (char character in input) { + // If the character is in our list, append a backslash before it + if (CHARACTERS_TO_ESCAPE.Contains(character)) { + sanitized.Append('\\'); + } + sanitized.Append(character); + } + + return sanitized.ToString(); + } + } +}