From 78575149d89607513619033c8ed5d19248d1232c Mon Sep 17 00:00:00 2001 From: Ronny Gunawan <3048897+ronnygunawan@users.noreply.github.com> Date: Fri, 2 Feb 2024 22:11:33 +0700 Subject: [PATCH 1/2] Add resilience extensions --- .../AI/Gemini/GeminiTextPromptHandler.cs | 56 +------------------ .../TelegramBotClientResilienceExtensions.cs | 43 ++++++++++++++ 2 files changed, 45 insertions(+), 54 deletions(-) create mode 100644 BotNet.Services/TelegramClient/TelegramBotClientResilienceExtensions.cs diff --git a/BotNet.CommandHandlers/AI/Gemini/GeminiTextPromptHandler.cs b/BotNet.CommandHandlers/AI/Gemini/GeminiTextPromptHandler.cs index 4b6de59..e9501dc 100644 --- a/BotNet.CommandHandlers/AI/Gemini/GeminiTextPromptHandler.cs +++ b/BotNet.CommandHandlers/AI/Gemini/GeminiTextPromptHandler.cs @@ -11,7 +11,7 @@ using BotNet.Services.Gemini.Models; using BotNet.Services.MarkdownV2; using BotNet.Services.RateLimit; -using Google.Protobuf; +using BotNet.Services.TelegramClient; using Microsoft.Extensions.Logging; using Telegram.Bot; using Telegram.Bot.Exceptions; @@ -181,7 +181,7 @@ await _telegramBotClient.EditMessageTextAsync( chatId: textPrompt.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 Google Gemini Pro", @@ -190,58 +190,6 @@ await _telegramBotClient.EditMessageTextAsync( ), cancellationToken: cancellationToken ); - } catch (ApiRequestException) { - try { - responseMessage = await telegramBotClient.EditMessageTextAsync( - chatId: textPrompt.Command.Chat.Id, - messageId: responseMessage.MessageId, - text: MarkdownV2Sanitizer.Sanitize(response), - parseMode: ParseMode.Markdown, - replyMarkup: new InlineKeyboardMarkup( - InlineKeyboardButton.WithUrl( - text: "Generated by Google Gemini Pro", - url: "https://deepmind.google/technologies/gemini/" - ) - ), - cancellationToken: cancellationToken - ); - } catch (ApiRequestException) { - try { - responseMessage = await telegramBotClient.EditMessageTextAsync( - chatId: textPrompt.Command.Chat.Id, - messageId: responseMessage.MessageId, - text: response, - parseMode: ParseMode.Html, - replyMarkup: new InlineKeyboardMarkup( - InlineKeyboardButton.WithUrl( - text: "Generated by Google Gemini Pro", - url: "https://deepmind.google/technologies/gemini/" - ) - ), - cancellationToken: cancellationToken - ); - } 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; - } - } 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; - } } catch (Exception exc) { _logger.LogError(exc, null); await telegramBotClient.EditMessageTextAsync( diff --git a/BotNet.Services/TelegramClient/TelegramBotClientResilienceExtensions.cs b/BotNet.Services/TelegramClient/TelegramBotClientResilienceExtensions.cs new file mode 100644 index 0000000..066c530 --- /dev/null +++ b/BotNet.Services/TelegramClient/TelegramBotClientResilienceExtensions.cs @@ -0,0 +1,43 @@ +using System; +using System.Threading; +using System.Threading.Tasks; +using BotNet.Services.MarkdownV2; +using Telegram.Bot; +using Telegram.Bot.Exceptions; +using Telegram.Bot.Types; +using Telegram.Bot.Types.Enums; +using Telegram.Bot.Types.ReplyMarkups; + +namespace BotNet.Services.TelegramClient { + public static class TelegramBotClientResilienceExtensions { + public static async Task EditMessageTextAsync( + this ITelegramBotClient telegramBotClient, + ChatId chatId, + int messageId, + string text, + ParseMode[] parseModes, + InlineKeyboardMarkup? replyMarkup = null, + CancellationToken cancellationToken = default + ) { + if (parseModes.Length == 0) throw new ArgumentException("At least one parse mode must be provided.", nameof(parseModes)); + + foreach (ParseMode parseMode in parseModes) { + try { + return await telegramBotClient.EditMessageTextAsync( + chatId: chatId, + messageId: messageId, + text: parseMode == ParseMode.MarkdownV2 + ? MarkdownV2Sanitizer.Sanitize(text) + : text, + parseMode: parseMode, + replyMarkup: replyMarkup, + cancellationToken: cancellationToken + ); + } catch (ApiRequestException) { + continue; + } + } + throw new ApiRequestException("Text could not be parsed."); + } + } +} 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 2/2] 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; }