diff --git a/application/src/main/java/org/togetherjava/tjbot/features/moderation/TransferQuestionCommand.java b/application/src/main/java/org/togetherjava/tjbot/features/moderation/TransferQuestionCommand.java index 58ad3a5766..9778bb6cc4 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/moderation/TransferQuestionCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/moderation/TransferQuestionCommand.java @@ -1,5 +1,7 @@ package org.togetherjava.tjbot.features.moderation; + +import net.dv8tion.jda.api.JDAConstants; import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.entities.Guild; @@ -84,6 +86,32 @@ public TransferQuestionCommand(Config config, ChatGptService chatGptService) { this.chatGptService = chatGptService; } + + public class TitleGenerator { + + // Define regex patterns for leading and trailing quotation marks + private static final Pattern LEADING_TRAILING_QUOTES = Pattern.compile("^['\"]|['\"]$"); + + public static String generateTitle(String originalMessage, ChatGptService chatGptService) { + String chatGptPrompt = + "Summarize the following text into a concise title or heading not more than 4-5 words, remove quotations if any: %s" + .formatted(originalMessage); + Optional chatGptResponse = chatGptService.ask(chatGptPrompt, ""); + String title = chatGptResponse.orElse(createTitle(originalMessage)); + title = LEADING_TRAILING_QUOTES.matcher(title).replaceAll(""); + if (title.length() > JDAConstants.MAX_TITLE_LENGTH) { + title = title.substring(0, JDAConstants.MAX_TITLE_LENGTH); + } + + return title; + } + + private static String createTitle(String originalMessage) { + return originalMessage.substring(0, + Math.min(originalMessage.length(), JDAConstants.MAX_TITLE_LENGTH)); + } + } + @Override public void onMessageContext(MessageContextInteractionEvent event) { @@ -96,15 +124,18 @@ public void onMessageContext(MessageContextInteractionEvent event) { String originalChannelId = event.getTarget().getChannel().getId(); String authorId = event.getTarget().getAuthor().getId(); String mostCommonTag = tags.getFirst(); + String chatGptPrompt = "Summarize the following text into a concise title or heading not more than 4-5 words, remove quotations if any: %s" .formatted(originalMessage); Optional chatGptResponse = chatGptService.ask(chatGptPrompt, ""); String title = chatGptResponse.orElse(createTitle(originalMessage)); + title = title.replaceAll("^\"|\"$", "").replaceAll("^'|'$", ""); if (title.length() > TITLE_MAX_LENGTH) { title = title.substring(0, TITLE_MAX_LENGTH); } + TextInput modalTitle = TextInput.create(MODAL_TITLE_ID, "Title", TextInputStyle.SHORT) .setMaxLength(TITLE_MAX_LENGTH) .setMinLength(TITLE_MIN_LENGTH) @@ -176,8 +207,7 @@ private void transferFlow(ModalInteractionEvent event, String channelId, String .retrieveUserById(authorId) .flatMap(fetchedUser -> createForumPost(event, fetchedUser)) .flatMap(createdForumPost -> dmUser(event.getChannel(), createdForumPost, - event.getGuild()) - .and(sendMessageToTransferrer.apply(createdForumPost))) + event.getGuild()).and(sendMessageToTransferrer.apply(createdForumPost))) .flatMap(dmSent -> deleteOriginalMessage(event.getJDA(), channelId, messageId)) .queue(); }