Skip to content

Commit

Permalink
Merge pull request #109 from teknologi-umum/resilience
Browse files Browse the repository at this point in the history
Resilience
  • Loading branch information
ronnygunawan authored Feb 2, 2024
2 parents c471165 + 9fdb9a1 commit 3889f38
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 64 deletions.
62 changes: 3 additions & 59 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 @@ -11,10 +8,9 @@
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;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
using Telegram.Bot.Types.ReplyMarkups;
Expand Down Expand Up @@ -122,7 +118,7 @@ public Task Handle(GeminiTextPrompt textPrompt, CancellationToken cancellationTo
chatId: textPrompt.Command.Chat.Id,
messageId: responseMessage.MessageId,
text: response,
parseMode: ParseMode.Markdown,
parseModes: [ParseMode.Markdown, ParseMode.MarkdownV2, ParseMode.Html],
replyMarkup: new InlineKeyboardMarkup(
InlineKeyboardButton.WithUrl(
text: "Generated by Google Gemini Pro",
Expand All @@ -131,58 +127,6 @@ public Task Handle(GeminiTextPrompt textPrompt, CancellationToken cancellationTo
),
cancellationToken: cancellationToken
);
} catch (ApiRequestException) {
try {
responseMessage = await telegramBotClient.EditMessageTextAsync(
chatId: textPrompt.Command.Chat.Id,
messageId: responseMessage.MessageId,
text: MarkdownV2Sanitizer.Sanitize(response),
parseMode: ParseMode.MarkdownV2,
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(
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
Original file line number Diff line number Diff line change
@@ -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<Message> 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.");
}
}
}

0 comments on commit 3889f38

Please sign in to comment.