From 5dab5e3fa57b5aa6e9723cb0a524f1dbb6595100 Mon Sep 17 00:00:00 2001 From: Struchkov Mark Date: Sun, 26 Mar 2023 22:36:50 +0300 Subject: [PATCH] fix inline mode --- .../bot/handler/InlineChoseHandler.java | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/main/java/dev/struchkov/example/bot/handler/InlineChoseHandler.java b/src/main/java/dev/struchkov/example/bot/handler/InlineChoseHandler.java index aae8072..8669b35 100644 --- a/src/main/java/dev/struchkov/example/bot/handler/InlineChoseHandler.java +++ b/src/main/java/dev/struchkov/example/bot/handler/InlineChoseHandler.java @@ -4,6 +4,7 @@ import dev.struchkov.example.bot.conf.AppProperty; import dev.struchkov.example.bot.service.InlineStorage; import dev.struchkov.godfather.main.domain.EventContainer; import dev.struchkov.godfather.simple.context.service.EventHandler; +import dev.struchkov.godfather.simple.domain.BoxAnswer; import dev.struchkov.godfather.telegram.simple.context.service.TelegramSending; import dev.struchkov.openai.context.GPTClient; import dev.struchkov.openai.domain.common.GptMessage; @@ -16,10 +17,11 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; import org.telegram.telegrambots.meta.api.objects.inlinequery.ChosenInlineQuery; +import java.text.MessageFormat; import java.util.List; import java.util.Optional; -import static dev.struchkov.godfather.simple.domain.BoxAnswer.boxAnswer; +import static dev.struchkov.godfather.telegram.main.context.BoxAnswerPayload.ENABLE_MARKDOWN; @Slf4j @Component @@ -39,18 +41,22 @@ public class InlineChoseHandler implements EventHandler { if (appProperty.getTelegramIds().contains(personId)) { final Optional optRequest = inlineStorage.getQuery(chosenInlineQuery.getResultId()); if (optRequest.isPresent()) { - final String request = optRequest.get(); + final String question = optRequest.get(); final GptResponse gptResponse = gptClient.execute( GptRequest.builder() .model(GPT3Model.GPT_3_5_TURBO) - .message( - GptMessage.fromUser(request) - ) + .message(GptMessage.fromUser(question)) .build() ); final List choices = gptResponse.getChoices(); final String answer = choices.get(choices.size() - 1).getMessage().getContent(); - telegramSending.replaceInlineMessage(chosenInlineQuery.getInlineMessageId(), boxAnswer(answer)); + telegramSending.replaceInlineMessage( + chosenInlineQuery.getInlineMessageId(), + BoxAnswer.builder() + .message(MessageFormat.format("Your question to ChatGPT:\n{0}\n\nAnswer ChatGpt:\n{1}\n-- -- -- -- --\n[Launch your personal ChatGPT Telegram bot](https://docs.struchkov.dev/chatgpt-telegram-bot/en/latest/)", question, answer)) + .payload(ENABLE_MARKDOWN) + .build() + ); } } }