From 21ba78375174ddcdf6d2ff9f785eceff509d04af Mon Sep 17 00:00:00 2001 From: kylelin1998 Date: Wed, 18 Oct 2023 10:57:20 +0800 Subject: [PATCH] Support customizing the text sent after triggering the /help command --- src/main/java/code/commands/HelpCommand.java | 10 +++++- src/main/java/code/config/Config.java | 2 +- 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 | 33 +++++++++++++++++++ src/main/resources/i18n/i18n_en.properties | 4 ++- src/main/resources/i18n/i18n_zh_CN.properties | 4 ++- 8 files changed, 56 insertions(+), 4 deletions(-) diff --git a/src/main/java/code/commands/HelpCommand.java b/src/main/java/code/commands/HelpCommand.java index cc74f5b..1e434c3 100644 --- a/src/main/java/code/commands/HelpCommand.java +++ b/src/main/java/code/commands/HelpCommand.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 HelpCommand 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.getHelpText())) { + MessageHandle.sendMessage(chatId, I18nHandle.getText(fromId, I18nEnum.HelpText), false); + } else { + MessageHandle.sendMessage(chatId, GlobalConfig.getHelpText(), false); + } } @Override diff --git a/src/main/java/code/config/Config.java b/src/main/java/code/config/Config.java index bd6400f..c5787a2 100644 --- a/src/main/java/code/config/Config.java +++ b/src/main/java/code/config/Config.java @@ -39,7 +39,7 @@ public class Config { private static ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock(); public static class MetaData { - public final static String CurrentVersion = "1.0.60"; + public final static String CurrentVersion = "1.0.62"; public final static String GitOwner = "kylelin1998"; public final static String GitRepo = "ChatGPTForTelegram"; public final static String ProcessName = "ChatGPTForTelegram-universal.jar"; diff --git a/src/main/java/code/config/ConfigSettings.java b/src/main/java/code/config/ConfigSettings.java index a1b6678..c1d8583 100644 --- a/src/main/java/code/config/ConfigSettings.java +++ b/src/main/java/code/config/ConfigSettings.java @@ -61,4 +61,8 @@ public class ConfigSettings { @ConfigField(isNotNull = false) private String startText; + @JSONField(name = "help_text") + @ConfigField(isNotNull = false) + private String helpText; + } diff --git a/src/main/java/code/config/I18nEnum.java b/src/main/java/code/config/I18nEnum.java index bdbe8a2..84a065c 100644 --- a/src/main/java/code/config/I18nEnum.java +++ b/src/main/java/code/config/I18nEnum.java @@ -104,7 +104,9 @@ public enum I18nEnum { SetConciseReplies("set_concise_replies"), SetOpenAIAPIPrefix("set_openai_api_prefix"), SetStartText("set_start_text"), + SetHelpText("set_help_text"), PleaseSendMeStartText("please_send_me_start_text"), + PleaseSendMeHelpText("please_send_me_help_text"), ; diff --git a/src/main/java/code/handler/Command.java b/src/main/java/code/handler/Command.java index 9451c4f..e02af78 100644 --- a/src/main/java/code/handler/Command.java +++ b/src/main/java/code/handler/Command.java @@ -34,6 +34,7 @@ public enum Command { SetConciseReplies("set_concise_replies", false), SetOpenAIAPIPrefix("set_openai_api_prefix", false), SetStartText("set_start_text", false), + SetHelpText("set_help_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 6618b5d..7f0f20a 100644 --- a/src/main/java/code/handler/commands/AdminCommands.java +++ b/src/main/java/code/handler/commands/AdminCommands.java @@ -82,6 +82,7 @@ public static void init() { .add(InlineKeyboardButtonBuilder .create() .add(I18nHandle.getText(session.getFromId(), I18nEnum.SetStartText), StepsCenter.buildCallbackData(true, session, Command.SetStartText, null)) + .add(I18nHandle.getText(session.getFromId(), I18nEnum.SetHelpText), StepsCenter.buildCallbackData(true, session, Command.SetHelpText, null)) .build() ) .add(InlineKeyboardButtonBuilder @@ -499,6 +500,38 @@ public static void init() { }) .build(); + StepsBuilder + .create() + .bindCommand(Command.SetHelpText) + .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.PleaseSendMeHelpText), false); + return StepResult.ok(); + }) + .steps((StepsChatSession session, int index, List list, Map context) -> { + String text = session.getText(); + + ConfigSettings config = Config.readConfig(); + config.setHelpText(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 f7da298..56ef124 100644 --- a/src/main/resources/i18n/i18n_en.properties +++ b/src/main/resources/i18n/i18n_en.properties @@ -116,4 +116,6 @@ please_choose_concise_replies=Please select whether to enable concise replies. I set_concise_replies=Set Concise Replies 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 +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. +set_help_text=Set Help Text +please_send_me_help_text=Please send me the setup text, which will be replied to the users after they use the /help 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 ffd020a..93bd89b 100644 --- a/src/main/resources/i18n/i18n_zh_CN.properties +++ b/src/main/resources/i18n/i18n_zh_CN.properties @@ -114,4 +114,6 @@ please_choose_concise_replies=\u8BF7\u9009\u62E9\u662F\u5426\u5F00\u542F\u7CBE\u set_concise_replies=\u8BBE\u7F6E\u7CBE\u7B80\u56DE\u590D 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 +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 +set_help_text=\u8BBE\u7F6E\u5E2E\u52A9\u6587\u672C +please_send_me_help_text=\u8BF7\u53D1\u9001\u7ED9\u6211\u8BBE\u7F6E\u6587\u672C\uFF0C \u8FD9\u4E2A\u6587\u672C\u5C06\u5728\u7528\u6237\u4F7F\u7528 /help \u547D\u4EE4\u540E\u8FDB\u884C\u56DE\u590D \ No newline at end of file