Skip to content

Commit

Permalink
Upgrade to .NET 9
Browse files Browse the repository at this point in the history
  • Loading branch information
ronnygunawan committed Dec 2, 2024
1 parent b4d8100 commit 311c04e
Show file tree
Hide file tree
Showing 40 changed files with 497 additions and 340 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 8.0.x
dotnet-version: 9.0.x
- name: Restore dependencies
run: dotnet restore
- name: Build
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 8.0.x
dotnet-version: 9.0.x
- name: Restore dependencies
run: dotnet restore
- name: Build
Expand Down
18 changes: 12 additions & 6 deletions BotNet.CommandHandlers/AI/Gemini/GeminiTextPromptHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@ public Task Handle(GeminiTextPrompt textPrompt, CancellationToken cancellationTo
userId: textPrompt.Command.Sender.Id
);
} catch (RateLimitExceededException exc) {
return _telegramBotClient.SendTextMessageAsync(
return _telegramBotClient.SendMessage(
chatId: textPrompt.Command.Chat.Id,
text: $"<code>Anda terlalu banyak memanggil AI. Coba lagi {exc.Cooldown} atau lanjutkan di private chat.</code>",
parseMode: ParseMode.Html,
replyToMessageId: textPrompt.Command.MessageId,
replyParameters: new ReplyParameters {
MessageId = textPrompt.Command.MessageId
},
replyMarkup: new InlineKeyboardMarkup(
InlineKeyboardButton.WithUrl("Private chat 💬", "t.me/TeknumBot")
),
Expand All @@ -67,11 +69,13 @@ public Task Handle(GeminiTextPrompt textPrompt, CancellationToken cancellationTo
);
}
} catch (RateLimitExceededException exc) {
return _telegramBotClient.SendTextMessageAsync(
return _telegramBotClient.SendMessage(
chatId: textPrompt.Command.Chat.Id,
text: $"<code>Anda terlalu banyak memanggil AI. Coba lagi {exc.Cooldown}.</code>",
parseMode: ParseMode.Html,
replyToMessageId: textPrompt.Command.MessageId,
replyParameters: new ReplyParameters {
MessageId = textPrompt.Command.MessageId
},
cancellationToken: cancellationToken
);
}
Expand Down Expand Up @@ -142,11 +146,13 @@ public Task Handle(GeminiTextPrompt textPrompt, CancellationToken cancellationTo
);
} catch (Exception exc) {
_logger.LogError(exc, null);
await telegramBotClient.SendTextMessageAsync(
await telegramBotClient.SendMessage(
chatId: textPrompt.Command.Chat.Id,
text: "😵",
parseMode: ParseMode.Html,
replyToMessageId: textPrompt.Command.MessageId,
replyParameters: new ReplyParameters {
MessageId = textPrompt.Command.MessageId
},
cancellationToken: cancellationToken
);
return;
Expand Down
20 changes: 13 additions & 7 deletions BotNet.CommandHandlers/AI/OpenAI/AskCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ public async Task Handle(AskCommand askCommand, CancellationToken cancellationTo
userId: askCommand.Command.Sender.Id
);
} catch (RateLimitExceededException exc) {
await _telegramBotClient.SendTextMessageAsync(
await _telegramBotClient.SendMessage(
chatId: askCommand.Command.Chat.Id,
text: $"<code>Anda terlalu banyak memanggil AI. Coba lagi {exc.Cooldown} atau lanjutkan di private chat.</code>",
parseMode: ParseMode.Html,
replyToMessageId: askCommand.Command.MessageId,
replyParameters: new ReplyParameters {
MessageId = askCommand.Command.MessageId
},
replyMarkup: new InlineKeyboardMarkup(
InlineKeyboardButton.WithUrl("Private chat 💬", "t.me/TeknumBot")
),
Expand All @@ -56,11 +58,13 @@ await _telegramBotClient.SendTextMessageAsync(
userId: askCommand.Command.Sender.Id
);
} catch (RateLimitExceededException exc) {
await _telegramBotClient.SendTextMessageAsync(
await _telegramBotClient.SendMessage(
chatId: askCommand.Command.Chat.Id,
text: $"<code>Anda terlalu banyak memanggil AI. Coba lagi {exc.Cooldown}.</code>",
parseMode: ParseMode.Html,
replyToMessageId: askCommand.Command.MessageId,
replyParameters: new ReplyParameters {
MessageId = askCommand.Command.MessageId
},
cancellationToken: cancellationToken
);
return;
Expand All @@ -82,11 +86,13 @@ select ChatMessage.FromText(
)
);

Message responseMessage = await _telegramBotClient.SendTextMessageAsync(
Message responseMessage = await _telegramBotClient.SendMessage(
chatId: askCommand.Command.Chat.Id,
text: MarkdownV2Sanitizer.Sanitize("… ⏳"),
parseMode: ParseMode.MarkdownV2,
replyToMessageId: askCommand.Command.MessageId
replyParameters: new ReplyParameters {
MessageId = askCommand.Command.MessageId
}
);

string response = await _openAIClient.ChatAsync(
Expand Down Expand Up @@ -119,7 +125,7 @@ select ChatMessage.FromText(
);
} catch (Exception exc) {
_logger.LogError(exc, null);
await telegramBotClient.EditMessageTextAsync(
await telegramBotClient.EditMessageText(
chatId: askCommand.Command.Chat.Id,
messageId: responseMessage.MessageId,
text: "😵",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public Task Handle(OpenAIImageGenerationPrompt command, CancellationToken cancel
);
} catch (Exception exc) {
_logger.LogError(exc, "Could not generate image");
await _telegramBotClient.EditMessageTextAsync(
await _telegramBotClient.EditMessageText(
chatId: command.Chat.Id,
messageId: command.ResponseMessageId,
text: "<code>Failed to generate image.</code>",
Expand All @@ -47,7 +47,7 @@ await _telegramBotClient.EditMessageTextAsync(

// Delete busy message
try {
await _telegramBotClient.DeleteMessageAsync(
await _telegramBotClient.DeleteMessage(
chatId: command.Chat.Id,
messageId: command.ResponseMessageId,
cancellationToken: cancellationToken
Expand All @@ -57,7 +57,7 @@ await _telegramBotClient.DeleteMessageAsync(
}

// Send generated image
Message responseMessage = await _telegramBotClient.SendPhotoAsync(
Message responseMessage = await _telegramBotClient.SendPhoto(
chatId: command.Chat.Id,
photo: new InputFileUrl(generatedImageUrl),
replyMarkup: new InlineKeyboardMarkup(
Expand All @@ -66,7 +66,9 @@ await _telegramBotClient.DeleteMessageAsync(
url: "https://openai.com/dall-e-3"
)
),
replyToMessageId: command.PromptMessageId,
replyParameters: new ReplyParameters {
MessageId = command.PromptMessageId
},
cancellationToken: cancellationToken
);

Expand Down
36 changes: 23 additions & 13 deletions BotNet.CommandHandlers/AI/OpenAI/OpenAIImagePromptHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@ ILogger<OpenAIImageGenerationPromptHandler> logger
public Task Handle(OpenAIImagePrompt imagePrompt, CancellationToken cancellationToken) {
if (imagePrompt.Command.Sender is not VIPSender
&& imagePrompt.Command.Chat is not HomeGroupChat) {
return _telegramBotClient.SendTextMessageAsync(
return _telegramBotClient.SendMessage(
chatId: imagePrompt.Command.Chat.Id,
text: MarkdownV2Sanitizer.Sanitize("Vision tidak bisa dipakai di sini."),
parseMode: ParseMode.MarkdownV2,
replyToMessageId: imagePrompt.Command.MessageId,
replyParameters: new ReplyParameters {
MessageId = imagePrompt.Command.MessageId
},
cancellationToken: cancellationToken
);
}
Expand All @@ -62,11 +64,13 @@ public Task Handle(OpenAIImagePrompt imagePrompt, CancellationToken cancellation
);
}
} catch (RateLimitExceededException exc) {
return _telegramBotClient.SendTextMessageAsync(
return _telegramBotClient.SendMessage(
chatId: imagePrompt.Command.Chat.Id,
text: $"<code>Anda terlalu banyak menggunakan vision. Coba lagi {exc.Cooldown}.</code>",
parseMode: ParseMode.Html,
replyToMessageId: imagePrompt.Command.MessageId,
replyParameters: new ReplyParameters {
MessageId = imagePrompt.Command.MessageId
},
cancellationToken: cancellationToken
);
}
Expand All @@ -80,11 +84,13 @@ public Task Handle(OpenAIImagePrompt imagePrompt, CancellationToken cancellation
);

if (error is not null) {
await _telegramBotClient.SendTextMessageAsync(
await _telegramBotClient.SendMessage(
chatId: imagePrompt.Command.Chat.Id,
text: $"<code>{error}</code>",
parseMode: ParseMode.Html,
replyToMessageId: imagePrompt.Command.MessageId,
replyParameters: new ReplyParameters {
MessageId = imagePrompt.Command.MessageId
},
cancellationToken: cancellationToken
);
return;
Expand All @@ -106,11 +112,13 @@ select ChatMessage.FromText(
ChatMessage.FromTextWithImageBase64("user", imagePrompt.Prompt, imageBase64!)
);

Message responseMessage = await _telegramBotClient.SendTextMessageAsync(
Message responseMessage = await _telegramBotClient.SendMessage(
chatId: imagePrompt.Command.Chat.Id,
text: MarkdownV2Sanitizer.Sanitize("… ⏳"),
parseMode: ParseMode.MarkdownV2,
replyToMessageId: imagePrompt.Command.MessageId
replyParameters: new ReplyParameters {
MessageId = imagePrompt.Command.MessageId
}
);

string response = await _openAIClient.ChatAsync(
Expand All @@ -126,11 +134,13 @@ select ChatMessage.FromText(
try {
ArtCommandHandler.IMAGE_GENERATION_RATE_LIMITER.ValidateActionRate(imagePrompt.Command.Chat.Id, imagePrompt.Command.Sender.Id);
} catch (RateLimitExceededException exc) {
await _telegramBotClient.SendTextMessageAsync(
await _telegramBotClient.SendMessage(
chatId: imagePrompt.Command.Chat.Id,
text: $"Anda belum mendapat giliran. Coba lagi {exc.Cooldown}.",
parseMode: ParseMode.Html,
replyToMessageId: imagePrompt.Command.MessageId,
replyParameters: new ReplyParameters {
MessageId = imagePrompt.Command.MessageId
},
cancellationToken: cancellationToken
);
return;
Expand Down Expand Up @@ -164,7 +174,7 @@ await _commandQueue.DispatchAsync(
);
break;
default:
await _telegramBotClient.EditMessageTextAsync(
await _telegramBotClient.EditMessageText(
chatId: imagePrompt.Command.Chat.Id,
messageId: responseMessage.MessageId,
text: MarkdownV2Sanitizer.Sanitize("Image generation tidak bisa dipakai di sini."),
Expand Down Expand Up @@ -193,7 +203,7 @@ await _telegramBotClient.EditMessageTextAsync(
);
} catch (Exception exc) {
_logger.LogError(exc, null);
await telegramBotClient.EditMessageTextAsync(
await telegramBotClient.EditMessageText(
chatId: imagePrompt.Command.Chat.Id,
messageId: responseMessage.MessageId,
text: "😵",
Expand All @@ -220,7 +230,7 @@ await telegramBotClient.EditMessageTextAsync(
private static async Task<(string? ImageBase64, string? Error)> GetImageBase64Async(ITelegramBotClient botClient, string fileId, CancellationToken cancellationToken) {
// Download photo
using MemoryStream originalImageStream = new();
await botClient.GetInfoAndDownloadFileAsync(
await botClient.GetInfoAndDownloadFile(
fileId: fileId,
destination: originalImageStream,
cancellationToken: cancellationToken);
Expand Down
28 changes: 18 additions & 10 deletions BotNet.CommandHandlers/AI/OpenAI/OpenAITextPromptHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,13 @@ public Task Handle(OpenAITextPrompt textPrompt, CancellationToken cancellationTo
userId: textPrompt.Command.Sender.Id
);
} catch (RateLimitExceededException exc) {
return _telegramBotClient.SendTextMessageAsync(
return _telegramBotClient.SendMessage(
chatId: textPrompt.Command.Chat.Id,
text: $"<code>Anda terlalu banyak memanggil AI. Coba lagi {exc.Cooldown} atau lanjutkan di private chat.</code>",
parseMode: ParseMode.Html,
replyToMessageId: textPrompt.Command.MessageId,
replyParameters: new ReplyParameters {
MessageId = textPrompt.Command.MessageId
},
replyMarkup: new InlineKeyboardMarkup(
InlineKeyboardButton.WithUrl("Private chat 💬", "t.me/TeknumBot")
),
Expand All @@ -62,11 +64,13 @@ public Task Handle(OpenAITextPrompt textPrompt, CancellationToken cancellationTo
userId: textPrompt.Command.Sender.Id
);
} catch (RateLimitExceededException exc) {
return _telegramBotClient.SendTextMessageAsync(
return _telegramBotClient.SendMessage(
chatId: textPrompt.Command.Chat.Id,
text: $"<code>Anda terlalu banyak memanggil AI. Coba lagi {exc.Cooldown}.</code>",
parseMode: ParseMode.Html,
replyToMessageId: textPrompt.Command.MessageId,
replyParameters: new ReplyParameters {
MessageId = textPrompt.Command.MessageId
},
cancellationToken: cancellationToken
);
}
Expand All @@ -90,11 +94,13 @@ select ChatMessage.FromText(
ChatMessage.FromText("user", textPrompt.Prompt)
);

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

string response = await _openAIClient.ChatAsync(
Expand All @@ -113,11 +119,13 @@ select ChatMessage.FromText(
try {
ArtCommandHandler.IMAGE_GENERATION_RATE_LIMITER.ValidateActionRate(textPrompt.Command.Chat.Id, textPrompt.Command.Sender.Id);
} catch (RateLimitExceededException exc) {
await _telegramBotClient.SendTextMessageAsync(
await _telegramBotClient.SendMessage(
chatId: textPrompt.Command.Chat.Id,
text: $"Anda belum mendapat giliran. Coba lagi {exc.Cooldown}.",
parseMode: ParseMode.Html,
replyToMessageId: textPrompt.Command.MessageId,
replyParameters: new ReplyParameters {
MessageId = textPrompt.Command.MessageId
},
cancellationToken: cancellationToken
);
return;
Expand Down Expand Up @@ -151,7 +159,7 @@ await _commandQueue.DispatchAsync(
);
break;
default:
await _telegramBotClient.EditMessageTextAsync(
await _telegramBotClient.EditMessageText(
chatId: textPrompt.Command.Chat.Id,
messageId: responseMessage.MessageId,
text: MarkdownV2Sanitizer.Sanitize("Image generation tidak bisa dipakai di sini."),
Expand Down Expand Up @@ -183,7 +191,7 @@ await _telegramBotClient.EditMessageTextAsync(
);
} catch (Exception exc) {
_logger.LogError(exc, null);
await telegramBotClient.EditMessageTextAsync(
await telegramBotClient.EditMessageText(
chatId: textPrompt.Command.Chat.Id,
messageId: responseMessage.MessageId,
text: "😵",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public Task Handle(StabilityTextToImagePrompt command, CancellationToken cancell
cancellationToken: cancellationToken
);
} catch (ContentFilteredException exc) {
await _telegramBotClient.EditMessageTextAsync(
await _telegramBotClient.EditMessageText(
chatId: command.Chat.Id,
messageId: command.ResponseMessageId,
text: $"<code>{exc.Message ?? "Content filtered."}</code>",
Expand All @@ -41,7 +41,7 @@ await _telegramBotClient.EditMessageTextAsync(
);
return;
} catch {
await _telegramBotClient.EditMessageTextAsync(
await _telegramBotClient.EditMessageText(
chatId: command.Chat.Id,
messageId: command.ResponseMessageId,
text: "<code>Failed to generate image.</code>",
Expand All @@ -53,7 +53,7 @@ await _telegramBotClient.EditMessageTextAsync(

// Delete busy message
try {
await _telegramBotClient.DeleteMessageAsync(
await _telegramBotClient.DeleteMessage(
chatId: command.Chat.Id,
messageId: command.ResponseMessageId,
cancellationToken: cancellationToken
Expand All @@ -64,7 +64,7 @@ await _telegramBotClient.DeleteMessageAsync(

// Send generated image
using MemoryStream generatedImageStream = new(generatedImage);
Message responseMessage = await _telegramBotClient.SendPhotoAsync(
Message responseMessage = await _telegramBotClient.SendPhoto(
chatId: command.Chat.Id,
photo: new InputFileStream(generatedImageStream, "art.png"),
replyMarkup: new InlineKeyboardMarkup(
Expand All @@ -73,7 +73,9 @@ await _telegramBotClient.DeleteMessageAsync(
url: "https://stability.ai/stable-image"
)
),
replyToMessageId: command.PromptMessageId,
replyParameters: new ReplyParameters {
MessageId = command.PromptMessageId
},
cancellationToken: cancellationToken
);

Expand Down
Loading

0 comments on commit 311c04e

Please sign in to comment.