From 9fdb9a10627a47eb651405107a6acef68527b46e Mon Sep 17 00:00:00 2001 From: Ronny Gunawan <3048897+ronnygunawan@users.noreply.github.com> Date: Fri, 2 Feb 2024 22:18:45 +0700 Subject: [PATCH] Implement resilience --- .../AI/Gemini/GeminiTextPromptHandler.cs | 8 ++------ .../AI/OpenAI/AskCommandHandler.cs | 12 ++++++++++-- .../AI/OpenAI/OpenAIImagePromptHandler.cs | 10 +++++++++- .../AI/OpenAI/OpenAITextPromptHandler.cs | 12 ++++++++++-- 4 files changed, 31 insertions(+), 11 deletions(-) diff --git a/BotNet.CommandHandlers/AI/Gemini/GeminiTextPromptHandler.cs b/BotNet.CommandHandlers/AI/Gemini/GeminiTextPromptHandler.cs index 150826b..a38c215 100644 --- a/BotNet.CommandHandlers/AI/Gemini/GeminiTextPromptHandler.cs +++ b/BotNet.CommandHandlers/AI/Gemini/GeminiTextPromptHandler.cs @@ -1,8 +1,5 @@ -using BotNet.CommandHandlers.Art; -using BotNet.Commands; +using BotNet.Commands; using BotNet.Commands.AI.Gemini; -using BotNet.Commands.AI.OpenAI; -using BotNet.Commands.AI.Stability; using BotNet.Commands.BotUpdate.Message; using BotNet.Commands.ChatAggregate; using BotNet.Commands.CommandPrioritization; @@ -14,7 +11,6 @@ using BotNet.Services.TelegramClient; using Microsoft.Extensions.Logging; using Telegram.Bot; -using Telegram.Bot.Exceptions; using Telegram.Bot.Types; using Telegram.Bot.Types.Enums; using Telegram.Bot.Types.ReplyMarkups; @@ -121,7 +117,7 @@ public Task Handle(GeminiTextPrompt textPrompt, CancellationToken cancellationTo responseMessage = await telegramBotClient.EditMessageTextAsync( chatId: textPrompt.Command.Chat.Id, messageId: responseMessage.MessageId, - text: MarkdownV2Sanitizer.Sanitize(response), + text: response, parseModes: [ParseMode.Markdown, ParseMode.MarkdownV2, ParseMode.Html], replyMarkup: new InlineKeyboardMarkup( InlineKeyboardButton.WithUrl( diff --git a/BotNet.CommandHandlers/AI/OpenAI/AskCommandHandler.cs b/BotNet.CommandHandlers/AI/OpenAI/AskCommandHandler.cs index fb8c236..f54e5bf 100644 --- a/BotNet.CommandHandlers/AI/OpenAI/AskCommandHandler.cs +++ b/BotNet.CommandHandlers/AI/OpenAI/AskCommandHandler.cs @@ -8,6 +8,7 @@ using BotNet.Services.OpenAI; using BotNet.Services.OpenAI.Models; using BotNet.Services.RateLimit; +using BotNet.Services.TelegramClient; using Microsoft.Extensions.Logging; using Telegram.Bot; using Telegram.Bot.Types; @@ -82,8 +83,8 @@ select ChatMessage.FromText( responseMessage = await telegramBotClient.EditMessageTextAsync( chatId: askCommand.Command.Chat.Id, messageId: responseMessage.MessageId, - text: MarkdownV2Sanitizer.Sanitize(response), - parseMode: ParseMode.MarkdownV2, + text: response, + parseModes: [ParseMode.MarkdownV2, ParseMode.Markdown, ParseMode.Html], replyMarkup: new InlineKeyboardMarkup( InlineKeyboardButton.WithUrl( text: askCommand switch { @@ -97,6 +98,13 @@ select ChatMessage.FromText( ); } catch (Exception exc) { _logger.LogError(exc, null); + await telegramBotClient.EditMessageTextAsync( + chatId: askCommand.Command.Chat.Id, + messageId: responseMessage.MessageId, + text: "😵", + parseMode: ParseMode.Html, + cancellationToken: cancellationToken + ); throw; } diff --git a/BotNet.CommandHandlers/AI/OpenAI/OpenAIImagePromptHandler.cs b/BotNet.CommandHandlers/AI/OpenAI/OpenAIImagePromptHandler.cs index d67fd5a..e74e851 100644 --- a/BotNet.CommandHandlers/AI/OpenAI/OpenAIImagePromptHandler.cs +++ b/BotNet.CommandHandlers/AI/OpenAI/OpenAIImagePromptHandler.cs @@ -10,6 +10,7 @@ using BotNet.Services.OpenAI; using BotNet.Services.OpenAI.Models; using BotNet.Services.RateLimit; +using BotNet.Services.TelegramClient; using Microsoft.Extensions.Logging; using SkiaSharp; using Telegram.Bot; @@ -181,7 +182,7 @@ await _telegramBotClient.EditMessageTextAsync( chatId: imagePrompt.Command.Chat.Id, messageId: responseMessage.MessageId, text: MarkdownV2Sanitizer.Sanitize(response), - parseMode: ParseMode.MarkdownV2, + parseModes: [ParseMode.MarkdownV2, ParseMode.Markdown, ParseMode.Html], replyMarkup: new InlineKeyboardMarkup( InlineKeyboardButton.WithUrl( text: "Generated by OpenAI GPT-4", @@ -192,6 +193,13 @@ await _telegramBotClient.EditMessageTextAsync( ); } catch (Exception exc) { _logger.LogError(exc, null); + await telegramBotClient.EditMessageTextAsync( + chatId: imagePrompt.Command.Chat.Id, + messageId: responseMessage.MessageId, + text: "😵", + parseMode: ParseMode.Html, + cancellationToken: cancellationToken + ); throw; } diff --git a/BotNet.CommandHandlers/AI/OpenAI/OpenAITextPromptHandler.cs b/BotNet.CommandHandlers/AI/OpenAI/OpenAITextPromptHandler.cs index 070992e..69ff246 100644 --- a/BotNet.CommandHandlers/AI/OpenAI/OpenAITextPromptHandler.cs +++ b/BotNet.CommandHandlers/AI/OpenAI/OpenAITextPromptHandler.cs @@ -10,6 +10,7 @@ using BotNet.Services.OpenAI; using BotNet.Services.OpenAI.Models; using BotNet.Services.RateLimit; +using BotNet.Services.TelegramClient; using Microsoft.Extensions.Logging; using Telegram.Bot; using Telegram.Bot.Types; @@ -146,8 +147,8 @@ await _telegramBotClient.EditMessageTextAsync( responseMessage = await telegramBotClient.EditMessageTextAsync( chatId: textPrompt.Command.Chat.Id, messageId: responseMessage.MessageId, - text: MarkdownV2Sanitizer.Sanitize(response), - parseMode: ParseMode.MarkdownV2, + text: response, + parseModes: [ParseMode.MarkdownV2, ParseMode.Markdown, ParseMode.Html], replyMarkup: new InlineKeyboardMarkup( InlineKeyboardButton.WithUrl( text: textPrompt switch { @@ -161,6 +162,13 @@ await _telegramBotClient.EditMessageTextAsync( ); } catch (Exception exc) { _logger.LogError(exc, null); + await telegramBotClient.EditMessageTextAsync( + chatId: textPrompt.Command.Chat.Id, + messageId: responseMessage.MessageId, + text: "😵", + parseMode: ParseMode.Html, + cancellationToken: cancellationToken + ); throw; }