From 1fa16921ab3955ed055574dda5682ff1bf96c403 Mon Sep 17 00:00:00 2001 From: kylelin1998 Date: Fri, 13 Oct 2023 21:19:52 +0800 Subject: [PATCH] Add a command to set the starter text, allowing users to customize the reply text for the /start command --- src/main/java/code/commands/StartCommand.java | 10 ++++- src/main/java/code/config/ConfigSettings.java | 4 ++ src/main/java/code/config/I18nEnum.java | 2 + src/main/java/code/handler/Command.java | 1 + .../code/handler/commands/AdminCommands.java | 37 +++++++++++++++++++ src/main/resources/i18n/i18n_en.properties | 4 +- src/main/resources/i18n/i18n_zh_CN.properties | 4 +- 7 files changed, 59 insertions(+), 3 deletions(-) diff --git a/src/main/java/code/commands/StartCommand.java b/src/main/java/code/commands/StartCommand.java index ac094c5..ab42532 100644 --- a/src/main/java/code/commands/StartCommand.java +++ b/src/main/java/code/commands/StartCommand.java @@ -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 { @@ -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 diff --git a/src/main/java/code/config/ConfigSettings.java b/src/main/java/code/config/ConfigSettings.java index 404668a..a1b6678 100644 --- a/src/main/java/code/config/ConfigSettings.java +++ b/src/main/java/code/config/ConfigSettings.java @@ -57,4 +57,8 @@ public class ConfigSettings { @ConfigField(isNotNull = true) private String openaiAPIPrefix; + @JSONField(name = "start_text") + @ConfigField(isNotNull = false) + private String startText; + } diff --git a/src/main/java/code/config/I18nEnum.java b/src/main/java/code/config/I18nEnum.java index 5da06e3..bdbe8a2 100644 --- a/src/main/java/code/config/I18nEnum.java +++ b/src/main/java/code/config/I18nEnum.java @@ -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"), ; diff --git a/src/main/java/code/handler/Command.java b/src/main/java/code/handler/Command.java index 297bc80..9451c4f 100644 --- a/src/main/java/code/handler/Command.java +++ b/src/main/java/code/handler/Command.java @@ -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), diff --git a/src/main/java/code/handler/commands/AdminCommands.java b/src/main/java/code/handler/commands/AdminCommands.java index 041319e..6618b5d 100644 --- a/src/main/java/code/handler/commands/AdminCommands.java +++ b/src/main/java/code/handler/commands/AdminCommands.java @@ -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)) @@ -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 list, Map 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 list, Map 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() diff --git a/src/main/resources/i18n/i18n_en.properties b/src/main/resources/i18n/i18n_en.properties index e4c7f86..f7da298 100644 --- a/src/main/resources/i18n/i18n_en.properties +++ b/src/main/resources/i18n/i18n_en.properties @@ -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 \ No newline at end of file +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. \ No newline at end of file diff --git a/src/main/resources/i18n/i18n_zh_CN.properties b/src/main/resources/i18n/i18n_zh_CN.properties index 796b5a5..ffd020a 100644 --- a/src/main/resources/i18n/i18n_zh_CN.properties +++ b/src/main/resources/i18n/i18n_zh_CN.properties @@ -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 \ No newline at end of file +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 \ No newline at end of file