Skip to content

Commit

Permalink
Add a command to set the starter text, allowing users to customize th…
Browse files Browse the repository at this point in the history
…e reply text for the /start command
  • Loading branch information
kylelin1998 committed Oct 13, 2023
1 parent 7b5a930 commit 1fa1692
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 3 deletions.
10 changes: 9 additions & 1 deletion src/main/java/code/commands/StartCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
import code.handler.I18nHandle;
import code.handler.message.MessageHandle;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.telegram.telegrambots.extensions.bots.commandbot.commands.BotCommand;
import org.telegram.telegrambots.meta.api.objects.Chat;
import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.api.objects.User;
import org.telegram.telegrambots.meta.bots.AbsSender;

import static code.Main.GlobalConfig;

@Slf4j
public class StartCommand extends BotCommand {

Expand All @@ -25,7 +28,12 @@ public void processMessage(AbsSender absSender, Message message, String[] argume
public void execute(AbsSender absSender, Message message, String[] arguments) {
String chatId = message.getChat().getId().toString();
String fromId = String.valueOf(message.getFrom().getId());
MessageHandle.sendMessage(chatId, I18nHandle.getText(fromId, I18nEnum.HelpText), false);

if (StringUtils.isBlank(GlobalConfig.getStartText())) {
MessageHandle.sendMessage(chatId, I18nHandle.getText(fromId, I18nEnum.HelpText), false);
} else {
MessageHandle.sendMessage(chatId, GlobalConfig.getStartText(), false);
}
}

@Override
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/code/config/ConfigSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,8 @@ public class ConfigSettings {
@ConfigField(isNotNull = true)
private String openaiAPIPrefix;

@JSONField(name = "start_text")
@ConfigField(isNotNull = false)
private String startText;

}
2 changes: 2 additions & 0 deletions src/main/java/code/config/I18nEnum.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ public enum I18nEnum {
PleaseChooseConciseReplies("please_choose_concise_replies"),
SetConciseReplies("set_concise_replies"),
SetOpenAIAPIPrefix("set_openai_api_prefix"),
SetStartText("set_start_text"),
PleaseSendMeStartText("please_send_me_start_text"),

;

Expand Down
1 change: 1 addition & 0 deletions src/main/java/code/handler/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public enum Command {
SetGptToken("set_gpt_token", false),
SetConciseReplies("set_concise_replies", false),
SetOpenAIAPIPrefix("set_openai_api_prefix", false),
SetStartText("set_start_text", false),
UpdateConfig("uc", false),
Language("language", false),
Restart("restart", false),
Expand Down
37 changes: 37 additions & 0 deletions src/main/java/code/handler/commands/AdminCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ public static void init() {
.add(I18nHandle.getText(session.getFromId(), I18nEnum.SetConciseReplies), StepsCenter.buildCallbackData(true, session, Command.SetConciseReplies, null))
.build()
)
.add(InlineKeyboardButtonBuilder
.create()
.add(I18nHandle.getText(session.getFromId(), I18nEnum.SetStartText), StepsCenter.buildCallbackData(true, session, Command.SetStartText, null))
.build()
)
.add(InlineKeyboardButtonBuilder
.create()
.add(I18nHandle.getText(session.getFromId(), I18nEnum.Restart), StepsCenter.buildCallbackData(true, session, Command.Restart, null))
Expand Down Expand Up @@ -462,6 +467,38 @@ public static void init() {
})
.build();

StepsBuilder
.create()
.bindCommand(Command.SetStartText)
.debug(GlobalConfig.getDebug())
.error((Exception e, StepsChatSession session) -> {
log.error(ExceptionUtil.getStackTraceWithCustomInfoToStr(e));
MessageHandle.sendMessage(session.getChatId(), I18nHandle.getText(session.getFromId(), I18nEnum.UnknownError), false);
})
.init((StepsChatSession session, int index, List<String> list, Map<String, Object> context) -> {
if (!isAdmin(session.getFromId())) {
MessageHandle.sendMessage(session.getChatId(), session.getReplyToMessageId(), I18nHandle.getText(session.getFromId(), I18nEnum.YouAreNotAnAdmin), false);
return StepResult.end();
}

MessageHandle.sendMessage(session.getChatId(), session.getReplyToMessageId(), I18nHandle.getText(session.getFromId(), I18nEnum.PleaseSendMeStartText), false);
return StepResult.ok();
})
.steps((StepsChatSession session, int index, List<String> list, Map<String, Object> context) -> {
String text = session.getText();

ConfigSettings config = Config.readConfig();
config.setStartText(text);
boolean b = Config.saveConfig(config);
if (b) {
MessageHandle.sendMessage(session.getChatId(), session.getReplyToMessageId(), I18nHandle.getText(session.getFromId(), I18nEnum.UpdateSucceeded), false);
} else {
MessageHandle.sendMessage(session.getChatId(), session.getReplyToMessageId(), I18nHandle.getText(session.getFromId(), I18nEnum.UpdateFailed), false);
}
return StepResult.ok();
})
.build();

// Update config
StepsBuilder
.create()
Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/i18n/i18n_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,6 @@ alive=Alive
die=Die
please_choose_concise_replies=Please select whether to enable concise replies. If enabled, some commands will remove the display of the question and exit prompt, and only show the reply content
set_concise_replies=Set Concise Replies
set_openai_api_prefix=Set the OpenAI API request prefix
set_openai_api_prefix=Set the OpenAI API request prefix
set_start_text=Set Start Text
please_send_me_start_text=Please send me the starter text, which will be used to reply to the user after they use the /start command.
4 changes: 3 additions & 1 deletion src/main/resources/i18n/i18n_zh_CN.properties
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,6 @@ alive=\u5B58\u6D3B
die=\u6B7B\u4EA1
please_choose_concise_replies=\u8BF7\u9009\u62E9\u662F\u5426\u5F00\u542F\u7CBE\u7B80\u56DE\u590D\uFF1F \u5F00\u542F\u540E\u90E8\u5206\u547D\u4EE4\u4F1A\u53BB\u9664\u56DE\u590D\u7684\u95EE\u9898\u663E\u793A\u548C\u9000\u51FA\u63D0\u793A\uFF0C \u4EC5\u663E\u793A\u56DE\u590D\u5185\u5BB9
set_concise_replies=\u8BBE\u7F6E\u7CBE\u7B80\u56DE\u590D
set_openai_api_prefix=\u8BBE\u7F6EOpenAI API\u8BF7\u6C42\u524D\u7F00
set_openai_api_prefix=\u8BBE\u7F6EOpenAI API\u8BF7\u6C42\u524D\u7F00
set_start_text=\u8BBE\u7F6E\u5F00\u59CB\u6587\u672C
please_send_me_start_text=\u8BF7\u53D1\u9001\u7ED9\u6211\u5F00\u59CB\u6587\u672C\uFF0C \u8FD9\u4E2A\u6587\u672C\u5C06\u5728\u7528\u6237\u4F7F\u7528 /start \u547D\u4EE4\u540E\u8FDB\u884C\u56DE\u590D

0 comments on commit 1fa1692

Please sign in to comment.