From b9d60aee2017bc1b01691003f3a143fdc455a116 Mon Sep 17 00:00:00 2001 From: Struchkov Mark Date: Mon, 27 Mar 2023 02:12:58 +0300 Subject: [PATCH] Added the ability to check the balance --- .../struchkov/example/bot/conf/AppConf.java | 7 +++ .../example/bot/conf/AppProperty.java | 1 + .../example/bot/unit/BalanceUnit.java | 54 +++++++++++++++++++ .../example/bot/unit/PersonalChatGPTUnit.java | 7 ++- .../dev/struchkov/example/bot/util/Cmd.java | 3 +- .../struchkov/example/bot/util/UnitName.java | 2 + src/main/resources/application.yml | 1 + 7 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 src/main/java/dev/struchkov/example/bot/unit/BalanceUnit.java diff --git a/src/main/java/dev/struchkov/example/bot/conf/AppConf.java b/src/main/java/dev/struchkov/example/bot/conf/AppConf.java index 69f16ea..2cada08 100644 --- a/src/main/java/dev/struchkov/example/bot/conf/AppConf.java +++ b/src/main/java/dev/struchkov/example/bot/conf/AppConf.java @@ -2,8 +2,10 @@ package dev.struchkov.example.bot.conf; import dev.struchkov.openai.ChatGptServiceImpl; import dev.struchkov.openai.GPTClientImpl; +import dev.struchkov.openai.OpenAIClientImpl; import dev.struchkov.openai.context.ChatGptService; import dev.struchkov.openai.context.GPTClient; +import dev.struchkov.openai.context.OpenAIClient; import dev.struchkov.openai.data.local.ChatGptLocalStorage; import dev.struchkov.openai.domain.conf.GPTConfig; import org.springframework.boot.context.properties.ConfigurationProperties; @@ -24,6 +26,11 @@ public class AppConf { return new GPTClientImpl(gptConfig); } + @Bean + public OpenAIClient openAIClient(GPTConfig gptConfig) { + return new OpenAIClientImpl(gptConfig); + } + @Bean public ChatGptService chatGptService(GPTClient gptClient) { return new ChatGptServiceImpl( diff --git a/src/main/java/dev/struchkov/example/bot/conf/AppProperty.java b/src/main/java/dev/struchkov/example/bot/conf/AppProperty.java index d37402e..cc71a67 100644 --- a/src/main/java/dev/struchkov/example/bot/conf/AppProperty.java +++ b/src/main/java/dev/struchkov/example/bot/conf/AppProperty.java @@ -13,6 +13,7 @@ import java.util.List; @ConfigurationProperties("app") public class AppProperty { + private List adminTelegramIds; private List telegramIds; private String version; diff --git a/src/main/java/dev/struchkov/example/bot/unit/BalanceUnit.java b/src/main/java/dev/struchkov/example/bot/unit/BalanceUnit.java new file mode 100644 index 0000000..478ab80 --- /dev/null +++ b/src/main/java/dev/struchkov/example/bot/unit/BalanceUnit.java @@ -0,0 +1,54 @@ +package dev.struchkov.example.bot.unit; + +import dev.struchkov.example.bot.conf.AppProperty; +import dev.struchkov.example.bot.util.Cmd; +import dev.struchkov.example.bot.util.UnitName; +import dev.struchkov.godfather.main.domain.annotation.Unit; +import dev.struchkov.godfather.main.domain.content.Attachment; +import dev.struchkov.godfather.main.domain.content.Mail; +import dev.struchkov.godfather.simple.domain.unit.AnswerText; +import dev.struchkov.godfather.telegram.domain.attachment.CommandAttachment; +import dev.struchkov.godfather.telegram.main.core.util.Attachments; +import dev.struchkov.godfather.telegram.starter.PersonUnitConfiguration; +import dev.struchkov.openai.context.OpenAIClient; +import dev.struchkov.openai.domain.response.account.TotalUsageResponse; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; + +import java.util.List; +import java.util.Optional; + +import static dev.struchkov.godfather.simple.domain.BoxAnswer.boxAnswer; + +@Component +@RequiredArgsConstructor +public class BalanceUnit implements PersonUnitConfiguration { + + private final AppProperty appProperty; + private final OpenAIClient openAIClient; + + @Unit(value = UnitName.BALANCE, global = true) + public AnswerText help() { + return AnswerText.builder() + .triggerCheck( + mail -> { + if (appProperty.getAdminTelegramIds().contains(mail.getFromPersonId())) { + final List attachments = mail.getAttachments(); + final Optional optCommand = Attachments.findFirstCommand(attachments); + if (optCommand.isPresent()) { + final CommandAttachment command = optCommand.get(); + return Cmd.BALANCE.equals(command.getCommandType()); + } + } + return false; + } + ) + .answer(() -> { + final TotalUsageResponse totalUsage = openAIClient.getTotalUsage(); + final double balance = totalUsage.getTotalUsage() / 100; + return boxAnswer("Used in this month: $" + balance); + }) + .build(); + } + +} diff --git a/src/main/java/dev/struchkov/example/bot/unit/PersonalChatGPTUnit.java b/src/main/java/dev/struchkov/example/bot/unit/PersonalChatGPTUnit.java index cec75a7..0d53e7f 100644 --- a/src/main/java/dev/struchkov/example/bot/unit/PersonalChatGPTUnit.java +++ b/src/main/java/dev/struchkov/example/bot/unit/PersonalChatGPTUnit.java @@ -69,6 +69,11 @@ public class PersonalChatGPTUnit implements PersonUnitConfiguration { .description("Clears the discussion context. Start a conversation from the beginning.") .build(), + ClientBotCommand.builder() + .key(Cmd.BALANCE) + .description("Find out how much you spent this month.") + .build(), + ClientBotCommand.builder() .key(Cmd.BEHAVIOR) .description("Allows you to set the initial behavior of ChatGPT.") @@ -96,7 +101,7 @@ public class PersonalChatGPTUnit implements PersonUnitConfiguration { .append("\uD83D\uDCAC: ").append(message.getText()); return BoxAnswer.builder() - .recipientPersonId(appProperty.getTelegramIds().get(0)) + .recipientPersonId(appProperty.getAdminTelegramIds().get(0)) .message(messageText.toString()) .payload(ENABLE_MARKDOWN) .build(); diff --git a/src/main/java/dev/struchkov/example/bot/util/Cmd.java b/src/main/java/dev/struchkov/example/bot/util/Cmd.java index 4308a2e..0886600 100644 --- a/src/main/java/dev/struchkov/example/bot/util/Cmd.java +++ b/src/main/java/dev/struchkov/example/bot/util/Cmd.java @@ -7,10 +7,10 @@ public class Cmd { public static final String CLEAR_CONTEXT = "clear_context"; public static final String PROMPT = "prompt"; - public static final String GPT = "gpt"; public static final String HELP = "help"; public static final String SUPPORT_DEV = "support"; public static final String START = "start"; + public static final String BALANCE = "balance"; public static final String BEHAVIOR = "behavior"; public static final String BEHAVIOR_1 = "behavior1"; @@ -20,4 +20,5 @@ public class Cmd { public static final String BEHAVIOR_5 = "behavior5"; public static final String BEHAVIOR_6 = "behavior6"; + } diff --git a/src/main/java/dev/struchkov/example/bot/util/UnitName.java b/src/main/java/dev/struchkov/example/bot/util/UnitName.java index 4c43a94..fa4bc6a 100644 --- a/src/main/java/dev/struchkov/example/bot/util/UnitName.java +++ b/src/main/java/dev/struchkov/example/bot/util/UnitName.java @@ -14,4 +14,6 @@ public class UnitName { public static final String START = "START"; public static final String BEHAVIOR = "BEHAVIOR"; + public static final String BALANCE = "BALANCE"; + } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index a5939ac..d4def67 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1,4 +1,5 @@ app: + admin-telegram-ids: ${ADMIN_TELEGRAM_PERSON_ID} telegram-ids: ${TELEGRAM_PERSON_ID} version: 0.0.1 telegram: