This commit is contained in:
parent
6bfb169e84
commit
5dab5e3fa5
@ -4,6 +4,7 @@ import dev.struchkov.example.bot.conf.AppProperty;
|
|||||||
import dev.struchkov.example.bot.service.InlineStorage;
|
import dev.struchkov.example.bot.service.InlineStorage;
|
||||||
import dev.struchkov.godfather.main.domain.EventContainer;
|
import dev.struchkov.godfather.main.domain.EventContainer;
|
||||||
import dev.struchkov.godfather.simple.context.service.EventHandler;
|
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.godfather.telegram.simple.context.service.TelegramSending;
|
||||||
import dev.struchkov.openai.context.GPTClient;
|
import dev.struchkov.openai.context.GPTClient;
|
||||||
import dev.struchkov.openai.domain.common.GptMessage;
|
import dev.struchkov.openai.domain.common.GptMessage;
|
||||||
@ -16,10 +17,11 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.telegram.telegrambots.meta.api.objects.inlinequery.ChosenInlineQuery;
|
import org.telegram.telegrambots.meta.api.objects.inlinequery.ChosenInlineQuery;
|
||||||
|
|
||||||
|
import java.text.MessageFormat;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
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
|
@Slf4j
|
||||||
@Component
|
@Component
|
||||||
@ -39,18 +41,22 @@ public class InlineChoseHandler implements EventHandler<ChosenInlineQuery> {
|
|||||||
if (appProperty.getTelegramIds().contains(personId)) {
|
if (appProperty.getTelegramIds().contains(personId)) {
|
||||||
final Optional<String> optRequest = inlineStorage.getQuery(chosenInlineQuery.getResultId());
|
final Optional<String> optRequest = inlineStorage.getQuery(chosenInlineQuery.getResultId());
|
||||||
if (optRequest.isPresent()) {
|
if (optRequest.isPresent()) {
|
||||||
final String request = optRequest.get();
|
final String question = optRequest.get();
|
||||||
final GptResponse gptResponse = gptClient.execute(
|
final GptResponse gptResponse = gptClient.execute(
|
||||||
GptRequest.builder()
|
GptRequest.builder()
|
||||||
.model(GPT3Model.GPT_3_5_TURBO)
|
.model(GPT3Model.GPT_3_5_TURBO)
|
||||||
.message(
|
.message(GptMessage.fromUser(question))
|
||||||
GptMessage.fromUser(request)
|
|
||||||
)
|
|
||||||
.build()
|
.build()
|
||||||
);
|
);
|
||||||
final List<Choice> choices = gptResponse.getChoices();
|
final List<Choice> choices = gptResponse.getChoices();
|
||||||
final String answer = choices.get(choices.size() - 1).getMessage().getContent();
|
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()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user