diff --git a/BotNet.Services/BotCommands/Art.cs b/BotNet.Services/BotCommands/Art.cs index a4dc82d..77f7bea 100644 --- a/BotNet.Services/BotCommands/Art.cs +++ b/BotNet.Services/BotCommands/Art.cs @@ -4,6 +4,7 @@ using System.Threading; using System.Threading.Tasks; using BotNet.Services.RateLimit; +using BotNet.Services.Stability.Models; using BotNet.Services.ThisXDoesNotExist; using Microsoft.Extensions.DependencyInjection; using Telegram.Bot; @@ -50,6 +51,13 @@ await botClient.SendPhotoAsync( photo: new InputFileStream(imageStream, "art.png"), replyToMessageId: message.MessageId, cancellationToken: cancellationToken); + } catch (ContentFilteredException) { + await botClient.SendTextMessageAsync( + chatId: message.Chat.Id, + text: "Content filtered", + parseMode: ParseMode.Html, + replyToMessageId: message.MessageId, + cancellationToken: cancellationToken); } catch { await botClient.SendTextMessageAsync( chatId: message.Chat.Id, diff --git a/BotNet.Services/BotCommands/OpenAI.cs b/BotNet.Services/BotCommands/OpenAI.cs index 510894c..72569fd 100644 --- a/BotNet.Services/BotCommands/OpenAI.cs +++ b/BotNet.Services/BotCommands/OpenAI.cs @@ -12,6 +12,7 @@ using BotNet.Services.OpenAI.Models; using BotNet.Services.OpenAI.Skills; using BotNet.Services.RateLimit; +using BotNet.Services.Stability.Models; using BotNet.Services.Stability.Skills; using Microsoft.Extensions.DependencyInjection; using RG.Ninja; @@ -851,26 +852,35 @@ await serviceProvider.GetRequiredService().StreamChatAsync( // prompt: message.Text!, // cancellationToken: cancellationToken //); - byte[] generatedImage = await serviceProvider.GetRequiredService().GenerateImageAsync( - prompt: message.Text!, - cancellationToken: cancellationToken - ); - using MemoryStream generatedImageStream = new(generatedImage); try { - await botClient.DeleteMessageAsync( + byte[] generatedImage = await serviceProvider.GetRequiredService().GenerateImageAsync( + prompt: message.Text!, + cancellationToken: cancellationToken + ); + using MemoryStream generatedImageStream = new(generatedImage); + try { + await botClient.DeleteMessageAsync( + chatId: busyMessage.Chat.Id, + messageId: busyMessage.MessageId, + cancellationToken: cancellationToken + ); + } catch (OperationCanceledException) { + throw; + } + await botClient.SendPhotoAsync( + chatId: message.Chat.Id, + photo: new InputFileStream(generatedImageStream, "art.png"), + replyToMessageId: message.MessageId, + cancellationToken: cancellationToken + ); + } catch (ContentFilteredException) { + await botClient.EditMessageTextAsync( chatId: busyMessage.Chat.Id, messageId: busyMessage.MessageId, - cancellationToken: cancellationToken + text: "Content filtered.", + parseMode: ParseMode.Html ); - } catch (OperationCanceledException) { - throw; } - await botClient.SendPhotoAsync( - chatId: message.Chat.Id, - photo: new InputFileStream(generatedImageStream, "art.png"), - replyToMessageId: message.MessageId, - cancellationToken: cancellationToken - ); break; } } @@ -901,6 +911,13 @@ await botClient.SendTextMessageAsync( parseMode: ParseMode.Html, replyToMessageId: message.MessageId, cancellationToken: cancellationToken); + } catch (HttpRequestException exc) { + await botClient.SendTextMessageAsync( + chatId: message.Chat.Id, + text: "Unknown error.", + parseMode: ParseMode.Html, + replyToMessageId: message.MessageId, + cancellationToken: cancellationToken); } catch (OperationCanceledException) { await botClient.SendTextMessageAsync( chatId: message.Chat.Id, diff --git a/BotNet.Services/Stability/Models/ContentFilteredException.cs b/BotNet.Services/Stability/Models/ContentFilteredException.cs new file mode 100644 index 0000000..29cd174 --- /dev/null +++ b/BotNet.Services/Stability/Models/ContentFilteredException.cs @@ -0,0 +1,5 @@ +using System; + +namespace BotNet.Services.Stability.Models { + public sealed class ContentFilteredException : Exception { } +} diff --git a/BotNet.Services/Stability/Models/TextToImageResponse.cs b/BotNet.Services/Stability/Models/TextToImageResponse.cs index dd36981..6eb79d7 100644 --- a/BotNet.Services/Stability/Models/TextToImageResponse.cs +++ b/BotNet.Services/Stability/Models/TextToImageResponse.cs @@ -7,7 +7,6 @@ List Artifacts internal sealed record Artifact( string Base64, - string FinishReason, - int Seed + string FinishReason ); } diff --git a/BotNet.Services/Stability/StabilityClient.cs b/BotNet.Services/Stability/StabilityClient.cs index a58a87c..7d6583b 100644 --- a/BotNet.Services/Stability/StabilityClient.cs +++ b/BotNet.Services/Stability/StabilityClient.cs @@ -69,6 +69,10 @@ CancellationToken cancellationToken TextToImageResponse? responseData = JsonSerializer.Deserialize(responseJson, CAMEL_CASE_SERIALIZER_OPTIONS); + if (responseData is { Artifacts: [Artifact { FinishReason: "CONTENT_FILTERED" }] }) { + throw new ContentFilteredException(); + } + if (responseData is not { Artifacts: [Artifact { FinishReason: "SUCCESS", Base64: var base64 }] }) { throw new HttpRequestException(); }