-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathViaBotReplyHandler.java
49 lines (42 loc) · 1.95 KB
/
ViaBotReplyHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package dev.vality.alert.tg.bot.handler;
import dev.vality.alert.tg.bot.constants.InlineCommands;
import dev.vality.alert.tg.bot.mapper.CreateParamsRequestMapper;
import dev.vality.alert.tg.bot.mapper.ReplyMessagesMapper;
import lombok.RequiredArgsConstructor;
import org.apache.thrift.TException;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
import org.telegram.telegrambots.meta.api.objects.Update;
import static dev.vality.alert.tg.bot.utils.StringSearchUtils.substringParamId;
import static dev.vality.alert.tg.bot.utils.StringSearchUtils.substringParamValue;
import static dev.vality.alert.tg.bot.utils.UserUtils.isUserInBot;
@Component
@RequiredArgsConstructor
@Order(Ordered.HIGHEST_PRECEDENCE)
public class ViaBotReplyHandler implements CommonHandler<SendMessage> {
private final ReplyMessagesMapper replyMessagesMapper;
private final CreateParamsRequestMapper createParamsRequestMapper;
@Override
public boolean filter(Update update) {
return update.hasMessage()
&& update.getMessage().hasText()
&& update.getMessage().getReplyToMessage() == null
&& update.getMessage().hasViaBot()
&& isUserInBot(update);
}
@Override
public SendMessage handle(Update update, long userId) throws TException {
String text = update.getMessage().getText();
return switch (InlineCommands.valueOfStartInlineCommand(text)) {
case SELECT_ALERT -> replyMessagesMapper.deleteAlert(
userId,
text.substring(InlineCommands.SELECT_ALERT.getCommand().length()).trim());
case SELECT_PARAM -> createParamsRequestMapper.createRequest(
userId,
substringParamId(text),
substringParamValue(text));
};
}
}