|
1
|
|
|
package com.github.netkorp.telegram.framework.commands.basic; |
|
2
|
|
|
|
|
3
|
|
|
import com.github.netkorp.telegram.framework.annotations.CommandGroup; |
|
4
|
|
|
import com.github.netkorp.telegram.framework.commands.abstracts.AbstractCommand; |
|
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.managers.SecurityManager; |
|
8
|
|
|
import org.apache.logging.log4j.util.Strings; |
|
9
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
10
|
|
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate; |
|
11
|
|
|
import org.springframework.stereotype.Component; |
|
12
|
|
|
import org.telegram.telegrambots.meta.api.objects.Update; |
|
13
|
|
|
|
|
14
|
|
|
import java.util.Collection; |
|
15
|
|
|
import java.util.LinkedList; |
|
16
|
|
|
import java.util.List; |
|
17
|
|
|
import java.util.SortedMap; |
|
18
|
|
|
import java.util.StringJoiner; |
|
19
|
|
|
import java.util.TreeMap; |
|
20
|
|
|
|
|
21
|
|
|
@Component |
|
22
|
|
|
@CommandGroup("Basic") |
|
23
|
|
|
@ConditionalOnSingleCandidate(HelpCommand.class) |
|
24
|
|
|
public class BasicHelpCommand extends AbstractCommand implements HelpCommand { |
|
25
|
|
|
|
|
26
|
|
|
private final SecurityManager securityManager; |
|
27
|
|
|
|
|
28
|
|
|
@Autowired |
|
29
|
|
|
public BasicHelpCommand(SecurityManager securityManager) { |
|
30
|
|
|
this.securityManager = securityManager; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Returns the commands that will be executed on the chat. |
|
35
|
|
|
* |
|
36
|
|
|
* @return Command to be executed. |
|
37
|
|
|
*/ |
|
38
|
|
|
@Override |
|
39
|
|
|
public String getName() { |
|
40
|
|
|
return "help"; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Processes the data of the commands. |
|
45
|
|
|
* |
|
46
|
|
|
* @param update The received message. |
|
47
|
|
|
*/ |
|
48
|
|
|
@Override |
|
49
|
|
|
public void execute(Update update) { |
|
50
|
|
|
StringJoiner stringJoiner = new StringJoiner(System.lineSeparator()); |
|
51
|
|
|
stringJoiner.add("You can control me by sending these commands:"); |
|
52
|
|
|
commandsByGroup(getAvailableCommands(update.getMessage().getChatId())) |
|
53
|
|
|
.forEach((group, commands) -> stringJoiner.add(helpForGroup(group, commands))); |
|
54
|
|
|
bot.sendMessage(stringJoiner.toString(), update.getMessage().getChatId(), true); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Returns the help for a given group of commands. |
|
59
|
|
|
* |
|
60
|
|
|
* @param group The name of the group. |
|
61
|
|
|
* @param commands The commands into the group. |
|
62
|
|
|
* @return The help. |
|
63
|
|
|
*/ |
|
64
|
|
|
private String helpForGroup(String group, List<Command> commands) { |
|
65
|
|
|
StringJoiner stringJoiner = new StringJoiner(System.lineSeparator()); |
|
66
|
|
|
if (!Strings.isEmpty(group)) { |
|
67
|
|
|
stringJoiner.add(String.format("<b>%s</b>", group)); |
|
68
|
|
|
} |
|
69
|
|
|
commands.forEach(command -> stringJoiner.add(String.format("%s - %s", command.command(), command.description()))); |
|
70
|
|
|
return System.lineSeparator() + stringJoiner.toString(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Groups the commands by groups. |
|
75
|
|
|
* |
|
76
|
|
|
* @param commands A list with all the commands. |
|
77
|
|
|
* @return The grouped commands. |
|
78
|
|
|
*/ |
|
79
|
|
|
private SortedMap<String, List<Command>> commandsByGroup(Collection<Command> commands) { |
|
80
|
|
|
SortedMap<String, List<Command>> commandsByGroup = new TreeMap<>(); |
|
81
|
|
|
|
|
82
|
|
|
commands.forEach(command -> { |
|
83
|
|
|
// Groups |
|
84
|
|
|
String group = command.getClass().isAnnotationPresent(CommandGroup.class) ? |
|
85
|
|
|
command.getClass().getAnnotation(CommandGroup.class).value() : ""; |
|
86
|
|
|
|
|
87
|
|
|
List<Command> commandList = commandsByGroup.getOrDefault(group, new LinkedList<>()); |
|
88
|
|
|
commandList.add(command); |
|
89
|
|
|
|
|
90
|
|
|
commandsByGroup.put(group, commandList); |
|
91
|
|
|
}); |
|
92
|
|
|
|
|
93
|
|
|
return commandsByGroup; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* The commands to be shown on help. |
|
98
|
|
|
* |
|
99
|
|
|
* @param chatId The ID of current chat. |
|
100
|
|
|
* @return The commands. |
|
101
|
|
|
*/ |
|
102
|
|
|
private Collection<Command> getAvailableCommands(Long chatId) { |
|
103
|
|
|
if (securityManager.isAuthorized(chatId)) { |
|
104
|
|
|
return commandManager.getAvailableCommands(); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return commandManager.getAvailableFreeCommands(); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Returns the description of the commands. |
|
112
|
|
|
* |
|
113
|
|
|
* @return The description. |
|
114
|
|
|
*/ |
|
115
|
|
|
@Override |
|
116
|
|
|
public String description() { |
|
117
|
|
|
return "Shows this message"; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|