Skip to content

Commit

Permalink
Sanitize string before passing it to markdown parser
Browse files Browse the repository at this point in the history
  • Loading branch information
ronnygunawan committed Oct 30, 2023
1 parent 6daab2a commit 5e56df0
Showing 1 changed file with 31 additions and 6 deletions.
37 changes: 31 additions & 6 deletions BotNet.Services/BotCommands/OpenAI.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using BotNet.Services.OpenAI;
Expand Down Expand Up @@ -386,22 +388,22 @@ await botClient.SendTextMessageAsync(
if (attachments.Count == 0) {
return await botClient.SendTextMessageAsync(
chatId: message.Chat.Id,
text: result,
text: SanitizeForMarkdownV2(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: result,
caption: SanitizeForMarkdownV2(result),
parseMode: ParseMode.MarkdownV2,
replyToMessageId: message.MessageId,
cancellationToken: cancellationToken);
} else {
Message sentMessage = await botClient.SendTextMessageAsync(
chatId: message.Chat.Id,
text: result,
text: SanitizeForMarkdownV2(result),
parseMode: ParseMode.MarkdownV2,
replyToMessageId: message.MessageId,
cancellationToken: cancellationToken);
Expand Down Expand Up @@ -458,22 +460,22 @@ await botClient.SendTextMessageAsync(
if (attachments.Count == 0) {
return await botClient.SendTextMessageAsync(
chatId: message.Chat.Id,
text: result,
text: SanitizeForMarkdownV2(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: result,
caption: SanitizeForMarkdownV2(result),
parseMode: ParseMode.MarkdownV2,
replyToMessageId: message.MessageId,
cancellationToken: cancellationToken);
} else {
Message sentMessage = await botClient.SendTextMessageAsync(
chatId: message.Chat.Id,
text: result,
text: SanitizeForMarkdownV2(result),
parseMode: ParseMode.MarkdownV2,
replyToMessageId: message.MessageId,
cancellationToken: cancellationToken);
Expand Down Expand Up @@ -757,5 +759,28 @@ 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();
}
}
}

0 comments on commit 5e56df0

Please sign in to comment.