Skip to content

Commit

Permalink
Use syntax highlighting
Browse files Browse the repository at this point in the history
  • Loading branch information
ronnygunawan committed Oct 31, 2023
1 parent 5e56df0 commit a7f43ba
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 38 deletions.
14 changes: 5 additions & 9 deletions BotNet.Services/BotCommands/Exec.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -52,8 +48,8 @@ await botClient.SendTextMessageAsync(
} else {
await botClient.SendTextMessageAsync(
chatId: message.Chat.Id,
text: $"Code:\n<code>{WebUtility.HtmlEncode(commandArgument)}</code>\n\nOutput:\n<code>{WebUtility.HtmlEncode(result.Run.Output)}</code>",
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);
}
Expand Down Expand Up @@ -103,8 +99,8 @@ await botClient.SendTextMessageAsync(
} else {
await botClient.SendTextMessageAsync(
chatId: message.Chat.Id,
text: $"Code:\n<code>{WebUtility.HtmlEncode(repliedToMessage)}</code>\n\nOutput:\n<code>{WebUtility.HtmlEncode(result.Run.Output)}</code>",
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);
}
Expand Down
36 changes: 7 additions & 29 deletions BotNet.Services/BotCommands/OpenAI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -388,22 +389,22 @@ 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);
} else if (attachments.Count == 1) {
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);
Expand Down Expand Up @@ -460,22 +461,22 @@ 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);
} else if (attachments.Count == 1) {
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);
Expand Down Expand Up @@ -759,28 +760,5 @@ await botClient.SendTextMessageAsync(
}
}
}

private static readonly HashSet<char> 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();
}
}
}
29 changes: 29 additions & 0 deletions BotNet.Services/MarkdownV2/MarkdownV2Sanitizer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Collections.Generic;
using System.Text;

namespace BotNet.Services.MarkdownV2 {
public static class MarkdownV2Sanitizer {
private static readonly HashSet<char> 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();
}
}
}

0 comments on commit a7f43ba

Please sign in to comment.