Skip to content

Commit

Permalink
Gemini handler directly send answer instead of editing placeholder
Browse files Browse the repository at this point in the history
  • Loading branch information
ronnygunawan committed Feb 3, 2024
1 parent 3889f38 commit 57392d5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 14 deletions.
21 changes: 7 additions & 14 deletions BotNet.CommandHandlers/AI/Gemini/GeminiTextPromptHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using BotNet.Commands.SenderAggregate;
using BotNet.Services.Gemini;
using BotNet.Services.Gemini.Models;
using BotNet.Services.MarkdownV2;
using BotNet.Services.RateLimit;
using BotNet.Services.TelegramClient;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -99,26 +98,20 @@ public Task Handle(GeminiTextPrompt textPrompt, CancellationToken cancellationTo
messages.Add(Content.FromText("user", textPrompt.Prompt));
}

Message responseMessage = await _telegramBotClient.SendTextMessageAsync(
chatId: textPrompt.Command.Chat.Id,
text: MarkdownV2Sanitizer.Sanitize("… ⏳"),
parseMode: ParseMode.MarkdownV2,
replyToMessageId: textPrompt.Command.MessageId
);

string response = await _geminiClient.ChatAsync(
messages: messages,
maxTokens: 512,
cancellationToken: cancellationToken
);

// Finalize message
// Send response
Message responseMessage;
try {
responseMessage = await telegramBotClient.EditMessageTextAsync(
responseMessage = await telegramBotClient.SendTextMessageAsync(
chatId: textPrompt.Command.Chat.Id,
messageId: responseMessage.MessageId,
text: response,
parseModes: [ParseMode.Markdown, ParseMode.MarkdownV2, ParseMode.Html],
parseModes: [ParseMode.MarkdownV2, ParseMode.Markdown, ParseMode.Html],
replyToMessageId: textPrompt.Command.MessageId,
replyMarkup: new InlineKeyboardMarkup(
InlineKeyboardButton.WithUrl(
text: "Generated by Google Gemini Pro",
Expand All @@ -129,11 +122,11 @@ public Task Handle(GeminiTextPrompt textPrompt, CancellationToken cancellationTo
);
} catch (Exception exc) {
_logger.LogError(exc, null);
await telegramBotClient.EditMessageTextAsync(
await telegramBotClient.SendTextMessageAsync(
chatId: textPrompt.Command.Chat.Id,
messageId: responseMessage.MessageId,
text: "😵",
parseMode: ParseMode.Html,
replyToMessageId: textPrompt.Command.MessageId,
cancellationToken: cancellationToken
);
throw;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,36 @@

namespace BotNet.Services.TelegramClient {
public static class TelegramBotClientResilienceExtensions {
public static async Task<Message> SendTextMessageAsync(
this ITelegramBotClient telegramBotClient,
ChatId chatId,
string text,
ParseMode[] parseModes,
int? replyToMessageId = null,
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.SendTextMessageAsync(
chatId: chatId,
text: parseMode == ParseMode.MarkdownV2
? MarkdownV2Sanitizer.Sanitize(text)
: text,
parseMode: parseMode,
replyToMessageId: replyToMessageId,
replyMarkup: replyMarkup,
cancellationToken: cancellationToken
);
} catch (ApiRequestException) {
continue;
}
}
throw new ApiRequestException("Text could not be parsed.");
}

public static async Task<Message> EditMessageTextAsync(
this ITelegramBotClient telegramBotClient,
ChatId chatId,
Expand Down

0 comments on commit 57392d5

Please sign in to comment.