Fix exception when user blocked bot (#22)

This commit is contained in:
malkoas 2023-08-04 16:06:15 +03:00 committed by GitHub
parent a48ecc2155
commit 9f7995ebb9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 51 additions and 9 deletions

View File

@ -1,4 +1,4 @@
package dev.vality.alert.tg.bot.exeptions;
package dev.vality.alert.tg.bot.exceptions;
public class AlertTgBotException extends RuntimeException {

View File

@ -1,4 +1,4 @@
package dev.vality.alert.tg.bot.exeptions;
package dev.vality.alert.tg.bot.exceptions;
public class UnknownHandlerException extends RuntimeException {

View File

@ -3,7 +3,7 @@ package dev.vality.alert.tg.bot.handler;
import dev.vality.alert.tg.bot.constants.InlineCommands;
import dev.vality.alert.tg.bot.dao.ParametersDao;
import dev.vality.alert.tg.bot.domain.tables.pojos.ParametersData;
import dev.vality.alert.tg.bot.exeptions.AlertTgBotException;
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;

View File

@ -0,0 +1,39 @@
package dev.vality.alert.tg.bot.handler;
import dev.vality.alert.tg.bot.service.MayDayService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
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 org.telegram.telegrambots.meta.api.objects.MemberStatus.KICKED;
import static org.telegram.telegrambots.meta.api.objects.MemberStatus.MEMBER;
@Slf4j
@Component
@RequiredArgsConstructor
@Order(Ordered.HIGHEST_PRECEDENCE)
public class MyChatMemberHandler implements CommonHandler<SendMessage> {
private final MayDayService mayDayService;
@Override
public boolean filter(Update update) {
return update.hasMyChatMember();
}
@Override
public SendMessage handle(Update update, long userId) throws TException {
if (update.getMyChatMember().getOldChatMember().getStatus().equals(MEMBER)
&& update.getMyChatMember().getNewChatMember().getStatus().equals(KICKED)) {
mayDayService.deleteAllAlerts(String.valueOf(userId));
log.info("User {} blocked bot, all alerts was deleted for user", userId);
}
return null;
}
}

View File

@ -1,8 +1,8 @@
package dev.vality.alert.tg.bot.service;
import dev.vality.alert.tg.bot.config.properties.AlertBotProperties;
import dev.vality.alert.tg.bot.exeptions.AlertTgBotException;
import dev.vality.alert.tg.bot.exeptions.UnknownHandlerException;
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;

View File

@ -1,7 +1,6 @@
package dev.vality.alert.tg.bot.service;
import dev.vality.alert.tg.bot.exeptions.AlertTgBotException;
import dev.vality.alerting.tg_bot.Notification;
import dev.vality.alerting.tg_bot.NotifierServiceSrv;
import lombok.RequiredArgsConstructor;
@ -32,7 +31,7 @@ public class NotifierService implements NotifierServiceSrv.Iface {
notification.getReceiverId());
}
} catch (TelegramApiException | TException ex) {
log.error("Received an exception while notify receiver {} with message{} with ex {}",
log.error("Received an exception while notify receiver {} with message {}",
notification.getReceiverId(),
notification.getMessage(),
ex);

View File

@ -13,7 +13,9 @@ public class UserUtils {
? update.getMessage().getFrom().getId()
: update.hasCallbackQuery()
? update.getCallbackQuery().getFrom().getId()
: update.getInlineQuery().getFrom().getId();
: update.hasInlineQuery()
? update.getInlineQuery().getFrom().getId()
: update.getMyChatMember().getFrom().getId();
}
public static String getUserName(Update update) {
@ -21,7 +23,9 @@ public class UserUtils {
? update.getMessage().getFrom().getUserName()
: update.hasCallbackQuery()
? update.getCallbackQuery().getMessage().getFrom().getUserName()
: update.getInlineQuery().getFrom().getUserName();
: update.hasInlineQuery()
? update.getInlineQuery().getFrom().getUserName()
: update.getMyChatMember().getFrom().getUserName();
}
public static boolean isUserInBot(Update update) {