Skip to content

Commit

Permalink
Support customizing the text sent after triggering the /help command
Browse files Browse the repository at this point in the history
  • Loading branch information
kylelin1998 committed Oct 18, 2023
1 parent 6e05b11 commit 21ba783
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 4 deletions.
10 changes: 9 additions & 1 deletion src/main/java/code/commands/HelpCommand.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 HelpCommand 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.getHelpText())) {
MessageHandle.sendMessage(chatId, I18nHandle.getText(fromId, I18nEnum.HelpText), false);
} else {
MessageHandle.sendMessage(chatId, GlobalConfig.getHelpText(), false);
}
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/code/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
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 @@ -61,4 +61,8 @@ public class ConfigSettings {
@ConfigField(isNotNull = false)
private String startText;

@JSONField(name = "help_text")
@ConfigField(isNotNull = false)
private String helpText;

}
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 @@ -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"),

;

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 @@ -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),
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/code/handler/commands/AdminCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<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.PleaseSendMeHelpText), 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.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()
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 @@ -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.
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.
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 @@ -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
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

0 comments on commit 21ba783

Please sign in to comment.