Added the ability to check the balance
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Struchkov Mark 2023-03-27 02:12:58 +03:00
parent 76e60263ad
commit b9d60aee20
Signed by: upagge
GPG Key ID: D3018BE7BA428CA6
7 changed files with 73 additions and 2 deletions

View File

@ -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(

View File

@ -13,6 +13,7 @@ import java.util.List;
@ConfigurationProperties("app")
public class AppProperty {
private List<String> adminTelegramIds;
private List<String> telegramIds;
private String version;

View File

@ -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<Mail> help() {
return AnswerText.<Mail>builder()
.triggerCheck(
mail -> {
if (appProperty.getAdminTelegramIds().contains(mail.getFromPersonId())) {
final List<Attachment> attachments = mail.getAttachments();
final Optional<CommandAttachment> 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();
}
}

View File

@ -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();

View File

@ -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";
}

View File

@ -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";
}

View File

@ -1,4 +1,5 @@
app:
admin-telegram-ids: ${ADMIN_TELEGRAM_PERSON_ID}
telegram-ids: ${TELEGRAM_PERSON_ID}
version: 0.0.1
telegram: