-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInlineHandler.java
124 lines (110 loc) · 5.64 KB
/
InlineHandler.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package dev.vality.alert.tg.bot.handler;
import dev.vality.alert.tg.bot.constants.InlineCommands;
import dev.vality.alert.tg.bot.constants.ParameterValue;
import dev.vality.alert.tg.bot.dao.ParametersDao;
import dev.vality.alert.tg.bot.domain.tables.pojos.ParametersData;
import dev.vality.alert.tg.bot.exceptions.AlertTgBotException;
import dev.vality.alert.tg.bot.mapper.JsonMapper;
import dev.vality.alert.tg.bot.service.MayDayService;
import dev.vality.alerting.mayday.UserAlert;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
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.AnswerInlineQuery;
import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.api.objects.inlinequery.inputmessagecontent.InputTextMessageContent;
import org.telegram.telegrambots.meta.api.objects.inlinequery.result.InlineQueryResultArticle;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import static dev.vality.alert.tg.bot.utils.StringSearchUtils.*;
@Slf4j
@Component
@RequiredArgsConstructor
@Order(Ordered.HIGHEST_PRECEDENCE)
public class InlineHandler implements CommonHandler<AnswerInlineQuery> {
public static final int MAX_INLINE_LIMIT = 50;
private final MayDayService mayDayService;
private final JsonMapper jsonMapper;
private final ParametersDao parametersDao;
@Override
public boolean filter(Update update) {
return update.hasInlineQuery();
}
@SneakyThrows
@Override
public AnswerInlineQuery handle(Update update, long userId) throws TException {
String inlineQuery = update.getInlineQuery().getQuery();
List<InlineQueryResultArticle> queryResultArticleList = new ArrayList<>();
switch (InlineCommands.valueOfStartInlineCommand(inlineQuery)) {
case SELECT_ALERT -> {
List<UserAlert> userAlerts = mayDayService.getUserAlerts(
String.valueOf(update.getInlineQuery().getFrom().getId()));
userAlerts.forEach(option -> {
if (isAlertInList(option, inlineQuery)) {
queryResultArticleList.add(fillInlineQueryResultArticle(
option.getId(),
option.getName(),
new InputTextMessageContent(
InlineCommands.SELECT_ALERT.getCommand() + option.getId())));
}
});
}
case SELECT_PARAM -> {
String alertId = substringAlertId(inlineQuery);
String paramId = substringParamId(inlineQuery);
ParametersData parametersData = parametersDao.getByAlertIdAndParamId(alertId, paramId);
Set<String> options = jsonMapper.toSet(parametersData.getOptionsValues());
log.info("Supported params: {}", options);
options.forEach(optionValue -> {
if (isParamInList(optionValue, inlineQuery, alertId, paramId)) {
queryResultArticleList.add(fillInlineQueryResultArticle(
optionValue,
null,
new InputTextMessageContent(
getAnswerValueParamsInlineQuery(
alertId,
parametersData.getParamName(),
optionValue))));
}
});
}
default -> throw new AlertTgBotException("Unknown InlineQuery value: " + inlineQuery);
}
List<InlineQueryResultArticle> queryResultArticleListCopy =
sortAndReturnApplicableAmountOfOptions(queryResultArticleList);
return new AnswerInlineQuery(update.getInlineQuery().getId(), new ArrayList<>(queryResultArticleListCopy));
}
private static List<InlineQueryResultArticle> sortAndReturnApplicableAmountOfOptions(
List<InlineQueryResultArticle> queryResultArticleList) {
List<InlineQueryResultArticle> queryResultArticleListCopy = queryResultArticleList;
queryResultArticleListCopy.sort((o1, o2) -> {
if (ParameterValue.EMPTY.getText().equals(o1.getTitle())) {
return -1;
}
if (ParameterValue.EMPTY.getText().equals(o2.getTitle())) {
return 1;
}
return CharSequence.compare(o1.getTitle(), o2.getTitle());
});
if (queryResultArticleListCopy.size() > MAX_INLINE_LIMIT) {
queryResultArticleListCopy = queryResultArticleListCopy.subList(0, MAX_INLINE_LIMIT);
}
return queryResultArticleListCopy;
}
private InlineQueryResultArticle fillInlineQueryResultArticle(String id,
String description,
InputTextMessageContent inputTextMessageContent) {
InlineQueryResultArticle inlineQueryResultArticle = new InlineQueryResultArticle();
inlineQueryResultArticle.setId(DigestUtils.sha256Hex(id));
inlineQueryResultArticle.setTitle(id);
inlineQueryResultArticle.setDescription(description);
inlineQueryResultArticle.setInputMessageContent(inputTextMessageContent);
return inlineQueryResultArticle;
}
}