1
|
|
|
package com.github.netkorp.telegram.framework.managers; |
2
|
|
|
|
3
|
|
|
import com.github.netkorp.telegram.framework.annotations.FreeCommand; |
4
|
|
|
import com.github.netkorp.telegram.framework.bots.PollingTelegramBot; |
5
|
|
|
import com.github.netkorp.telegram.framework.commands.interfaces.CloseCommand; |
6
|
|
|
import com.github.netkorp.telegram.framework.commands.interfaces.Command; |
7
|
|
|
import com.github.netkorp.telegram.framework.commands.interfaces.DoneCommand; |
8
|
|
|
import com.github.netkorp.telegram.framework.commands.interfaces.HelpCommand; |
9
|
|
|
import com.github.netkorp.telegram.framework.commands.interfaces.MultistageCommand; |
10
|
|
|
import com.github.netkorp.telegram.framework.exceptions.CommandNotActive; |
11
|
|
|
import com.github.netkorp.telegram.framework.exceptions.CommandNotFound; |
12
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
13
|
|
|
import org.springframework.beans.factory.annotation.Value; |
14
|
|
|
import org.springframework.stereotype.Component; |
15
|
|
|
|
16
|
|
|
import java.util.Arrays; |
17
|
|
|
import java.util.Collection; |
18
|
|
|
import java.util.HashMap; |
19
|
|
|
import java.util.List; |
20
|
|
|
import java.util.Map; |
21
|
|
|
import java.util.Optional; |
22
|
|
|
|
23
|
|
|
@Component |
24
|
|
|
public class CommandManager { |
25
|
|
|
private final Map<String, Command> commands; |
26
|
|
|
private final Map<String, Command> freeCommands; |
27
|
|
|
|
28
|
|
|
private final Map<Long, MultistageCommand> activeCommand; |
29
|
|
|
|
30
|
|
|
private String closeCommand; |
31
|
|
|
private String doneCommand; |
32
|
|
|
private String helpCommand; |
33
|
|
|
|
34
|
|
|
@Autowired |
35
|
|
|
public CommandManager(List<Command> commands, PollingTelegramBot bot, @Value("${telegram.commands.free}") String freeCommands) { |
36
|
|
|
this.commands = new HashMap<>(); |
37
|
|
|
this.freeCommands = new HashMap<>(); |
38
|
|
|
|
39
|
|
|
this.activeCommand = new HashMap<>(); |
40
|
|
|
|
41
|
|
|
commands.forEach(command -> addCommand(command, bot, Arrays.asList(freeCommands.split(",")))); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
private void addCommand(Command command, PollingTelegramBot bot, List<String> freeCommandNames) { |
45
|
|
|
command.setBot(bot); |
46
|
|
|
command.setCommandManager(this); |
47
|
|
|
this.commands.put(command.command(), command); |
48
|
|
|
|
49
|
|
|
if (isFreeCommand(command, freeCommandNames)) { |
50
|
|
|
this.freeCommands.put(command.command(), command); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if (command instanceof CloseCommand) { |
54
|
|
|
closeCommand = command.command(); |
55
|
|
|
} else if (command instanceof DoneCommand) { |
56
|
|
|
doneCommand = command.command(); |
57
|
|
|
} else if (command instanceof HelpCommand) { |
58
|
|
|
helpCommand = command.command(); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
private boolean isFreeCommand(Command command, List<String> freeCommandNames) { |
63
|
|
|
return freeCommandNames.contains(command.getName()) |
64
|
|
|
|| freeCommandNames.contains(command.command()) |
65
|
|
|
|| command.getClass().isAnnotationPresent(FreeCommand.class); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Returns the {@link Command} instance from a given getName. |
70
|
|
|
* |
71
|
|
|
* @param command The commands as {@link String}. |
72
|
|
|
* @return The {@link Command} instance. |
73
|
|
|
* @throws CommandNotFound If the given name is not related to any commands. |
74
|
|
|
*/ |
75
|
|
|
public Command getCommand(String command) throws CommandNotFound { |
76
|
|
|
if (!this.commands.containsKey(command)) { |
77
|
|
|
throw new CommandNotFound(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return this.commands.get(command); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public Command getFreeCommand(String command) throws CommandNotFound { |
84
|
|
|
if (!this.freeCommands.containsKey(command)) { |
85
|
|
|
throw new CommandNotFound(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return this.freeCommands.get(command); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public void setActiveCommand(final Long idChat, final MultistageCommand command) { |
92
|
|
|
this.activeCommand.put(idChat, command); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Gets the active commands for a given user. |
97
|
|
|
* |
98
|
|
|
* @param idChat Chat id of the user. |
99
|
|
|
* @return The active commands. |
100
|
|
|
*/ |
101
|
|
|
public MultistageCommand getActiveCommand(Long idChat) throws CommandNotActive { |
102
|
|
|
if (!hasActiveCommand(idChat)) { |
103
|
|
|
throw new CommandNotActive(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return activeCommand.get(idChat); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Removes the active commands. |
111
|
|
|
* |
112
|
|
|
* @param idChat Chat id of the user. |
113
|
|
|
*/ |
114
|
|
|
public void removeActiveCommand(Long idChat) { |
115
|
|
|
activeCommand.remove(idChat); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Is there an active commands or not? |
120
|
|
|
* |
121
|
|
|
* @param idChat Chat id of the user. |
122
|
|
|
* @return If there is active commands or not. |
123
|
|
|
*/ |
124
|
|
|
public boolean hasActiveCommand(Long idChat) { |
125
|
|
|
return activeCommand.containsKey(idChat); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Returns the {@link CloseCommand} if it exists. |
130
|
|
|
* |
131
|
|
|
* @return The {@link CloseCommand} instance. |
132
|
|
|
*/ |
133
|
|
|
public Optional<Command> getCloseCommand() { |
134
|
|
|
try { |
135
|
|
|
return Optional.ofNullable(getCommand(this.closeCommand)); |
136
|
|
|
} catch (CommandNotFound commandNotFound) { |
137
|
|
|
return Optional.empty(); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Returns the {@link DoneCommand} if it exists. |
143
|
|
|
* |
144
|
|
|
* @return The {@link DoneCommand} instance. |
145
|
|
|
*/ |
146
|
|
|
public Optional<Command> getDoneCommand() { |
147
|
|
|
try { |
148
|
|
|
return Optional.ofNullable(getCommand(this.doneCommand)); |
149
|
|
|
} catch (CommandNotFound commandNotFound) { |
150
|
|
|
return Optional.empty(); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Returns the {@link HelpCommand} if it exists. |
156
|
|
|
* |
157
|
|
|
* @return The {@link HelpCommand} instance. |
158
|
|
|
*/ |
159
|
|
|
public Optional<Command> getHelpCommand() { |
160
|
|
|
try { |
161
|
|
|
return Optional.ofNullable(getCommand(this.helpCommand)); |
162
|
|
|
} catch (CommandNotFound commandNotFound) { |
163
|
|
|
return Optional.empty(); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Returns a list with the available commands. |
169
|
|
|
* |
170
|
|
|
* @return Available commands. |
171
|
|
|
*/ |
172
|
|
|
public Collection<Command> getAvailableCommands() { |
173
|
|
|
return commands.values(); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Returns a list with the available free commands. |
178
|
|
|
* |
179
|
|
|
* @return Available free commands. |
180
|
|
|
*/ |
181
|
|
|
public Collection<Command> getAvailableFreeCommands() { |
182
|
|
|
return freeCommands.values(); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|