|
1
|
|
|
package com.github.netkorp.telegram.framework.commands.basic; |
|
|
|
|
|
|
2
|
|
|
|
|
3
|
|
|
import com.github.netkorp.telegram.framework.annotations.TelegramCommand; |
|
4
|
|
|
import com.github.netkorp.telegram.framework.commands.abstracts.AbstractSimpleCommand; |
|
5
|
|
|
import com.github.netkorp.telegram.framework.commands.interfaces.Command; |
|
6
|
|
|
import com.github.netkorp.telegram.framework.commands.interfaces.HelpCommand; |
|
7
|
|
|
import com.github.netkorp.telegram.framework.condition.ExcludeCondition; |
|
8
|
|
|
import com.github.netkorp.telegram.framework.exceptions.CommandNotFound; |
|
9
|
|
|
import com.github.netkorp.telegram.framework.managers.CommandManager; |
|
10
|
|
|
import com.github.netkorp.telegram.framework.managers.SecurityManager; |
|
11
|
|
|
import org.apache.logging.log4j.util.Strings; |
|
12
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
13
|
|
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate; |
|
14
|
|
|
import org.springframework.context.NoSuchMessageException; |
|
15
|
|
|
import org.springframework.context.annotation.Conditional; |
|
16
|
|
|
import org.springframework.context.i18n.LocaleContextHolder; |
|
17
|
|
|
import org.telegram.telegrambots.meta.api.objects.Update; |
|
18
|
|
|
|
|
19
|
|
|
import java.util.Collection; |
|
20
|
|
|
import java.util.LinkedList; |
|
21
|
|
|
import java.util.List; |
|
22
|
|
|
import java.util.SortedMap; |
|
23
|
|
|
import java.util.StringJoiner; |
|
24
|
|
|
import java.util.TreeMap; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Displays the bot's help. |
|
28
|
|
|
*/ |
|
29
|
|
|
@TelegramCommand(name = "help", group = "Basic") |
|
30
|
|
|
@Conditional(ExcludeCondition.class) |
|
31
|
|
|
@ConditionalOnSingleCandidate(HelpCommand.class) |
|
32
|
|
|
public class BasicHelpCommand extends AbstractSimpleCommand implements HelpCommand { |
|
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* The component to know which user is authorized. |
|
36
|
|
|
*/ |
|
37
|
|
|
private final SecurityManager securityManager; |
|
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Constructs a new {@link BasicHelpCommand} instance with the {@link SecurityManager} component instance. |
|
41
|
|
|
* |
|
42
|
|
|
* @param securityManager the {@link SecurityManager} component instance. |
|
43
|
|
|
*/ |
|
44
|
|
|
@Autowired |
|
45
|
|
|
public BasicHelpCommand(SecurityManager securityManager) { |
|
|
|
|
|
|
46
|
|
|
this.securityManager = securityManager; |
|
|
|
|
|
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Processes the data sent by the users. |
|
51
|
|
|
* |
|
52
|
|
|
* @param update the received message. |
|
53
|
|
|
* @param args the parameters passed to the command execution. |
|
54
|
|
|
*/ |
|
55
|
|
|
@Override |
|
56
|
|
|
public void execute(final Update update, String[] args) { |
|
|
|
|
|
|
57
|
|
|
try { |
|
|
|
|
|
|
58
|
|
|
if (args.length == 0) { |
|
|
|
|
|
|
59
|
|
|
execute(update); |
|
|
|
|
|
|
60
|
|
|
return; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
StringJoiner stringJoiner = new StringJoiner(System.lineSeparator()); |
|
64
|
|
|
|
|
65
|
|
|
for (String arg : args) { |
|
|
|
|
|
|
66
|
|
|
try { |
|
|
|
|
|
|
67
|
|
|
stringJoiner.add(helpForCommand(commandManager.getCommand(CommandManager.getCommandFullName(arg)))); |
|
|
|
|
|
|
68
|
|
|
} catch (CommandNotFound commandNotFound) { |
|
|
|
|
|
|
69
|
|
|
bot.sendMessage(String.format("%s: %s", commandNotFound.getMessage(), arg), update.getMessage().getChatId(), true); |
|
|
|
|
|
|
70
|
|
|
execute(update); |
|
71
|
|
|
throw commandNotFound; |
|
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
bot.sendMessage(stringJoiner.toString(), update.getMessage().getChatId(), true); |
|
76
|
|
|
} catch (CommandNotFound commandNotFound) { |
|
|
|
|
|
|
77
|
|
|
// Do nothing |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Executes the command's logic without taking parameters. |
|
83
|
|
|
* |
|
84
|
|
|
* @param update the received message. |
|
85
|
|
|
*/ |
|
86
|
|
|
@Override |
|
87
|
|
|
public void execute(Update update) { |
|
|
|
|
|
|
88
|
|
|
StringJoiner stringJoiner = new StringJoiner(System.lineSeparator()); |
|
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
stringJoiner.add(String.format("%s:", messageSource.getMessage("commands.basic.help.title", null, |
|
91
|
|
|
LocaleContextHolder.getLocale()))); |
|
92
|
|
|
|
|
93
|
|
|
commandsByGroup(getAvailableCommands(update.getMessage().getChatId())) |
|
94
|
|
|
.forEach((group, commands) -> stringJoiner.add(helpForGroup(group, commands))); |
|
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
bot.sendMessage(stringJoiner.toString(), update.getMessage().getChatId(), true); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Returns the help for the group of commands. |
|
101
|
|
|
* |
|
102
|
|
|
* @param group the name of the group. |
|
103
|
|
|
* @param commands the commands into the group. |
|
104
|
|
|
* @return the help for the group. |
|
105
|
|
|
*/ |
|
106
|
|
|
private String helpForGroup(String group, List<Command> commands) { |
|
|
|
|
|
|
107
|
|
|
StringJoiner stringJoiner = new StringJoiner(System.lineSeparator()); |
|
|
|
|
|
|
108
|
|
|
if (!Strings.isEmpty(group)) { |
|
|
|
|
|
|
109
|
|
|
stringJoiner.add(String.format("<b>%s</b>", group)); |
|
|
|
|
|
|
110
|
|
|
} |
|
111
|
|
|
commands.forEach(command -> stringJoiner.add(helpForCommand(command))); |
|
|
|
|
|
|
112
|
|
|
return System.lineSeparator() + stringJoiner.toString(); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Returns the help for a single command. |
|
117
|
|
|
* |
|
118
|
|
|
* @param command the command. |
|
119
|
|
|
* @return the help of the command. |
|
120
|
|
|
*/ |
|
121
|
|
|
private String helpForCommand(Command command) { |
|
|
|
|
|
|
122
|
|
|
StringJoiner stringJoiner = new StringJoiner(", "); |
|
|
|
|
|
|
123
|
|
|
CommandManager.getCommandFullNames(command).forEach(stringJoiner::add); |
|
124
|
|
|
|
|
125
|
|
|
String description; |
|
126
|
|
|
|
|
127
|
|
|
try { |
|
|
|
|
|
|
128
|
|
|
description = messageSource.getMessage(command.descriptionKey(), null, LocaleContextHolder.getLocale()); |
|
|
|
|
|
|
129
|
|
|
} catch (NoSuchMessageException exception) { |
|
|
|
|
|
|
130
|
|
|
description = messageSource.getMessage("commands.basic.help.default-description", null, |
|
|
|
|
|
|
131
|
|
|
LocaleContextHolder.getLocale()); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
return String.format("%s - %s", stringJoiner.toString(), description); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Organizes the available commands into groups. |
|
139
|
|
|
* |
|
140
|
|
|
* @param commands the list with all the available commands. |
|
141
|
|
|
* @return the available commands in groups sorted by the group's name. |
|
142
|
|
|
*/ |
|
143
|
|
|
private SortedMap<String, List<Command>> commandsByGroup(Collection<Command> commands) { |
|
|
|
|
|
|
144
|
|
|
SortedMap<String, List<Command>> commandsByGroup = new TreeMap<>(); |
|
|
|
|
|
|
145
|
|
|
|
|
146
|
|
|
commands.forEach(command -> { |
|
|
|
|
|
|
147
|
|
|
// Groups |
|
148
|
|
|
String group = command.getClass().getAnnotation(TelegramCommand.class).group(); |
|
|
|
|
|
|
149
|
|
|
|
|
150
|
|
|
List<Command> commandList = commandsByGroup.getOrDefault(group, new LinkedList<>()); |
|
151
|
|
|
commandList.add(command); |
|
152
|
|
|
|
|
153
|
|
|
commandsByGroup.put(group, commandList); |
|
154
|
|
|
}); |
|
155
|
|
|
|
|
156
|
|
|
return commandsByGroup; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Returns the list of available commands for the user. It takes into account whether the user is authorized or not. |
|
161
|
|
|
* |
|
162
|
|
|
* @param chatId the identification of the user's chat. |
|
163
|
|
|
* @return the list of available commands. |
|
164
|
|
|
*/ |
|
165
|
|
|
private Collection<Command> getAvailableCommands(Long chatId) { |
|
|
|
|
|
|
166
|
|
|
if (securityManager.isAuthorized(chatId)) { |
|
|
|
|
|
|
167
|
|
|
return commandManager.getAvailableCommands(); |
|
|
|
|
|
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
return commandManager.getAvailableNonSecureCommands(); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Returns the command's description key, used to retrieve the help message. |
|
175
|
|
|
* |
|
176
|
|
|
* @return the command's description key. |
|
177
|
|
|
*/ |
|
178
|
|
|
@Override |
|
179
|
|
|
public String descriptionKey() { |
|
|
|
|
|
|
180
|
|
|
return "commands.description.help"; |
|
|
|
|
|
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
|