Skip to content

Commit

Permalink
Implement resilience
Browse files Browse the repository at this point in the history
  • Loading branch information
ronnygunawan committed Feb 2, 2024
1 parent eab8596 commit 9fdb9a1
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
8 changes: 2 additions & 6 deletions BotNet.CommandHandlers/AI/Gemini/GeminiTextPromptHandler.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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(
Expand Down
12 changes: 10 additions & 2 deletions BotNet.CommandHandlers/AI/OpenAI/AskCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand All @@ -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;
}

Expand Down
10 changes: 9 additions & 1 deletion BotNet.CommandHandlers/AI/OpenAI/OpenAIImagePromptHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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",
Expand All @@ -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;
}

Expand Down
12 changes: 10 additions & 2 deletions BotNet.CommandHandlers/AI/OpenAI/OpenAITextPromptHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand All @@ -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;
}

Expand Down

0 comments on commit 9fdb9a1

Please sign in to comment.