-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlertBot.java
115 lines (104 loc) · 5.17 KB
/
AlertBot.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
package dev.vality.alert.tg.bot.service;
import dev.vality.alert.tg.bot.config.properties.AlertBotProperties;
import dev.vality.alert.tg.bot.exceptions.AlertTgBotException;
import dev.vality.alert.tg.bot.exceptions.UnknownHandlerException;
import dev.vality.alert.tg.bot.handler.CommonHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.thrift.TException;
import org.springframework.stereotype.Service;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.meta.api.methods.AnswerInlineQuery;
import org.telegram.telegrambots.meta.api.methods.commands.SetMyCommands;
import org.telegram.telegrambots.meta.api.methods.groupadministration.GetChatMember;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.api.objects.chatmember.ChatMember;
import org.telegram.telegrambots.meta.api.objects.commands.BotCommand;
import org.telegram.telegrambots.meta.api.objects.commands.scope.BotCommandScopeDefault;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import java.util.List;
import static dev.vality.alert.tg.bot.constants.UserStatuses.ALLOWED_USER_STATUSES;
import static dev.vality.alert.tg.bot.utils.UserUtils.getUserId;
import static dev.vality.alert.tg.bot.utils.UserUtils.getUserName;
@Slf4j
@Service
public class AlertBot extends TelegramLongPollingBot {
private final List<CommonHandler> handlers;
private final AlertBotProperties alertBotProperties;
private final MayDayService mayDayService;
public AlertBot(AlertBotProperties alertBotProperties,
List<CommonHandler> handlers,
MayDayService mayDayService) throws TelegramApiException {
super(alertBotProperties.getToken());
this.handlers = handlers;
this.alertBotProperties = alertBotProperties;
this.mayDayService = mayDayService;
this.execute(new SetMyCommands(List.of(
new BotCommand("/start", "Main Menu")), new BotCommandScopeDefault(), null));
}
@Override
public String getBotUsername() {
return alertBotProperties.getName();
}
@Override
public void onUpdateReceived(Update update) {
try {
if (isUserPermission(update)) {
long userId = getUserId(update);
var answer = handlers.stream()
.filter(handler -> handler.filter(update))
.findFirst()
.orElseThrow(UnknownHandlerException::new)
.handle(update, userId);
if (answer instanceof SendMessage sendMessage) {
sendMessage.setChatId(userId);
execute(sendMessage);
} else if (answer instanceof AnswerInlineQuery answerInlineQuery) {
answerInlineQuery.setIsPersonal(true);
answerInlineQuery.setCacheTime(alertBotProperties.getCacheOptionsSec());
execute(answerInlineQuery);
}
}
} catch (TelegramApiException | TException ex) {
throw new AlertTgBotException(String.format("Received an exception while handle update %s", update), ex);
}
}
public boolean isUserPermission(Update update) throws TException {
Long userId = getUserId(update);
String userName = getUserName(update);
try {
return isChatMemberPermission(userId);
} catch (TelegramApiException e) {
checkExceptionIsUserInChat(e, userName, userId);
throw new AlertTgBotException(String.format("Error while checking user %s permissions", userName), e);
}
}
public boolean isChatMemberPermission(Long userId) throws TelegramApiException, TException {
ChatMember chatMember = execute(new GetChatMember(alertBotProperties.getChatId(), userId));
if (ALLOWED_USER_STATUSES.contains(chatMember.getStatus())) {
return true;
} else {
mayDayService.deleteAllAlerts(String.valueOf(userId));
SendMessage message = new SendMessage();
message.setChatId(userId);
message.setText("У вас не прав на создание алертов, все ранее созданные алерты удалены");
execute(message);
return false;
}
}
private void checkExceptionIsUserInChat(TelegramApiException e, String userName, Long userId) {
if (e.getMessage().contains("[400] Bad Request: user not found")) {
log.info("User {} not found in chat", userName);
SendMessage message = new SendMessage();
message.setChatId(userId);
message.setText("У вас не прав на создание алертов");
try {
execute(message);
throw new AlertTgBotException(String.format("User %s don't have permissions", userName), e);
} catch (TelegramApiException ex) {
throw new AlertTgBotException(
String.format("Error while checking user %s permissions", userName), e);
}
}
}
}