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 org.apache.logging.log4j.util.Strings; |
8
|
|
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate; |
9
|
|
|
import org.springframework.stereotype.Component; |
10
|
|
|
import org.telegram.telegrambots.meta.api.objects.Update; |
11
|
|
|
|
12
|
|
|
import java.util.LinkedList; |
13
|
|
|
import java.util.List; |
14
|
|
|
import java.util.SortedMap; |
15
|
|
|
import java.util.StringJoiner; |
16
|
|
|
import java.util.TreeMap; |
17
|
|
|
|
18
|
|
|
@Component |
19
|
|
|
@CommandGroup("Basic") |
20
|
|
|
@ConditionalOnSingleCandidate(HelpCommand.class) |
21
|
|
|
public class BasicHelpCommand extends AbstractCommand implements HelpCommand { |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Returns the commands that will be executed on the chat. |
25
|
|
|
* |
26
|
|
|
* @return Command to be executed. |
27
|
|
|
*/ |
28
|
|
|
@Override |
29
|
|
|
public String getName() { |
30
|
|
|
return "help"; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Processes the data of the commands. |
35
|
|
|
* |
36
|
|
|
* @param update The received message. |
37
|
|
|
*/ |
38
|
|
|
@Override |
39
|
|
|
public void execute(Update update) { |
40
|
|
|
StringJoiner stringJoiner = new StringJoiner(System.lineSeparator()); |
41
|
|
|
stringJoiner.add("You can control me by sending these commands:" + System.lineSeparator()); |
42
|
|
|
commandsByGroup().forEach((group, commands) -> { |
43
|
|
|
if (!Strings.isEmpty(group)) { |
44
|
|
|
stringJoiner.add(String.format("%n<b>%s</b>", group)); |
45
|
|
|
} |
46
|
|
|
commands.forEach(command -> stringJoiner.add(String.format("%s - %s", command.command(), command.description()))); |
47
|
|
|
}); |
48
|
|
|
bot.sendMessage(stringJoiner.toString(), update.getMessage().getChatId(), true); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Groups the commands by groups. |
53
|
|
|
* |
54
|
|
|
* @return The grouped commands. |
55
|
|
|
*/ |
56
|
|
|
private SortedMap<String, List<Command>> commandsByGroup() { |
57
|
|
|
SortedMap<String, List<Command>> commandsByGroup = new TreeMap<>(); |
58
|
|
|
|
59
|
|
|
commandManager.getAvailableCommands().forEach(command -> { |
60
|
|
|
// Groups |
61
|
|
|
String group = command.getClass().isAnnotationPresent(CommandGroup.class) ? |
62
|
|
|
command.getClass().getAnnotation(CommandGroup.class).value() : ""; |
63
|
|
|
|
64
|
|
|
List<Command> commandList = commandsByGroup.getOrDefault(group, new LinkedList<>()); |
65
|
|
|
commandList.add(command); |
66
|
|
|
|
67
|
|
|
commandsByGroup.put(group, commandList); |
68
|
|
|
}); |
69
|
|
|
|
70
|
|
|
return commandsByGroup; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Returns the description of the commands. |
75
|
|
|
* |
76
|
|
|
* @return The description. |
77
|
|
|
*/ |
78
|
|
|
@Override |
79
|
|
|
public String description() { |
80
|
|
|
return "Shows this message"; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|