Добавил два eventproviders для подписки и отписки

This commit is contained in:
Struchkov Mark 2022-07-15 17:48:49 +03:00
parent 3e8989b64e
commit 1c43f1d797
9 changed files with 231 additions and 12 deletions

View File

@ -5,7 +5,7 @@
<groupId>dev.struchkov.godfather</groupId> <groupId>dev.struchkov.godfather</groupId>
<artifactId>telegram-bot</artifactId> <artifactId>telegram-bot</artifactId>
<version>0.0.13</version> <version>0.0.14</version>
<packaging>pom</packaging> <packaging>pom</packaging>
<modules> <modules>
@ -33,7 +33,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<godfather.core.ver>0.0.11</godfather.core.ver> <godfather.core.ver>0.0.12</godfather.core.ver>
<telegrambots.ver>6.1.0</telegrambots.ver> <telegrambots.ver>6.1.0</telegrambots.ver>
<plugin.maven.compiler.ver>3.10.1</plugin.maven.compiler.ver> <plugin.maven.compiler.ver>3.10.1</plugin.maven.compiler.ver>

View File

@ -5,7 +5,7 @@
<parent> <parent>
<groupId>dev.struchkov.godfather</groupId> <groupId>dev.struchkov.godfather</groupId>
<artifactId>telegram-bot</artifactId> <artifactId>telegram-bot</artifactId>
<version>0.0.13</version> <version>0.0.14</version>
</parent> </parent>
<artifactId>telegram-core</artifactId> <artifactId>telegram-core</artifactId>

View File

@ -0,0 +1,28 @@
package dev.struchkov.godfather.telegram.convert;
import dev.struchkov.godfather.telegram.domain.event.Subscribe;
import org.telegram.telegrambots.meta.api.objects.Chat;
import org.telegram.telegrambots.meta.api.objects.ChatMemberUpdated;
import java.time.LocalDateTime;
import static dev.struchkov.haiti.utils.Exceptions.utilityClass;
public final class SubscribeConvert {
private SubscribeConvert() {
utilityClass();
}
public static Subscribe apply(ChatMemberUpdated updated) {
final Chat chat = updated.getChat();
final Subscribe subscribe = new Subscribe();
subscribe.setTelegramId(chat.getId());
subscribe.setLastName(chat.getLastName());
subscribe.setFirstName(chat.getFirstName());
subscribe.setSubscriptionDate(LocalDateTime.now());
return subscribe;
}
}

View File

@ -0,0 +1,28 @@
package dev.struchkov.godfather.telegram.convert;
import dev.struchkov.godfather.telegram.domain.event.Unsubscribe;
import org.telegram.telegrambots.meta.api.objects.Chat;
import org.telegram.telegrambots.meta.api.objects.ChatMemberUpdated;
import java.time.LocalDateTime;
import static dev.struchkov.haiti.utils.Exceptions.utilityClass;
public final class UnsubscribeConvert {
private UnsubscribeConvert() {
utilityClass();
}
public static Unsubscribe apply(ChatMemberUpdated updated) {
final Chat chat = updated.getChat();
final Unsubscribe unsubscribe = new Unsubscribe();
unsubscribe.setTelegramId(chat.getId());
unsubscribe.setLastName(chat.getLastName());
unsubscribe.setFirstName(chat.getFirstName());
unsubscribe.setSubscriptionDate(LocalDateTime.now());
return unsubscribe;
}
}

View File

@ -0,0 +1,14 @@
package dev.struchkov.godfather.telegram.domain.event;
import dev.struchkov.godfather.context.domain.event.Event;
public class Command implements Event {
public static final String TYPE = "CMD";
@Override
public String getType() {
return TYPE;
}
}

View File

@ -0,0 +1,53 @@
package dev.struchkov.godfather.telegram.domain.event;
import dev.struchkov.godfather.context.domain.event.Event;
import java.time.LocalDateTime;
public class Subscribe implements Event {
public static final String TYPE = "SUBSCRIBE";
private Long telegramId;
private String firstName;
private String lastName;
private LocalDateTime subscriptionDate;
public Long getTelegramId() {
return telegramId;
}
public void setTelegramId(Long telegramId) {
this.telegramId = telegramId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public LocalDateTime getSubscriptionDate() {
return subscriptionDate;
}
public void setSubscriptionDate(LocalDateTime subscriptionDate) {
this.subscriptionDate = subscriptionDate;
}
@Override
public String getType() {
return TYPE;
}
}

View File

@ -0,0 +1,53 @@
package dev.struchkov.godfather.telegram.domain.event;
import dev.struchkov.godfather.context.domain.event.Event;
import java.time.LocalDateTime;
public class Unsubscribe implements Event {
public static final String TYPE = "UNSUBSCRIBE";
private Long telegramId;
private String firstName;
private String lastName;
private LocalDateTime subscriptionDate;
public Long getTelegramId() {
return telegramId;
}
public void setTelegramId(Long telegramId) {
this.telegramId = telegramId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public LocalDateTime getSubscriptionDate() {
return subscriptionDate;
}
public void setSubscriptionDate(LocalDateTime subscriptionDate) {
this.subscriptionDate = subscriptionDate;
}
@Override
public String getType() {
return TYPE;
}
}

View File

@ -5,12 +5,20 @@ import dev.struchkov.godfather.context.service.EventProvider;
import dev.struchkov.godfather.telegram.TelegramConnect; import dev.struchkov.godfather.telegram.TelegramConnect;
import dev.struchkov.godfather.telegram.convert.CallbackQueryConvert; import dev.struchkov.godfather.telegram.convert.CallbackQueryConvert;
import dev.struchkov.godfather.telegram.convert.MessageMailConvert; import dev.struchkov.godfather.telegram.convert.MessageMailConvert;
import dev.struchkov.godfather.telegram.convert.SubscribeConvert;
import dev.struchkov.godfather.telegram.convert.UnsubscribeConvert;
import dev.struchkov.godfather.telegram.domain.event.Subscribe;
import dev.struchkov.godfather.telegram.domain.event.Unsubscribe;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.telegram.telegrambots.meta.api.objects.CallbackQuery; import org.telegram.telegrambots.meta.api.objects.CallbackQuery;
import org.telegram.telegrambots.meta.api.objects.ChatMemberUpdated;
import org.telegram.telegrambots.meta.api.objects.Message; import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.api.objects.Update; import org.telegram.telegrambots.meta.api.objects.Update;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
/** /**
* TODO: Добавить описание класса. * TODO: Добавить описание класса.
@ -19,23 +27,40 @@ import java.util.List;
*/ */
public class EventDistributorService implements EventDistributor { public class EventDistributorService implements EventDistributor {
private final List<EventProvider<Mail>> eventProviders; private final Map<String, List<EventProvider>> eventProviderMap;
public EventDistributorService(TelegramConnect telegramConnect, List<EventProvider<Mail>> eventProviders) { public EventDistributorService(TelegramConnect telegramConnect, List<EventProvider> eventProviders) {
this.eventProviders = eventProviders; this.eventProviderMap = eventProviders.stream().collect(Collectors.groupingBy(EventProvider::getEventType));
telegramConnect.initEventDistributor(this); telegramConnect.initEventDistributor(this);
} }
@Override @Override
public void processing(@NotNull Update update) { public void processing(@NotNull Update update) {
if (update.getMessage() != null) {
final Message message = update.getMessage(); final Message message = update.getMessage();
final CallbackQuery callbackQuery = update.getCallbackQuery(); getEventProvider(Mail.TYPE)
if (message != null) { .ifPresent(eventProviders -> eventProviders.forEach(eventProvider -> eventProvider.sendEvent(MessageMailConvert.apply(message))));
eventProviders.forEach(provider -> provider.sendEvent(MessageMailConvert.apply(message))); }
if (update.getCallbackQuery() != null) {
final CallbackQuery callbackQuery = update.getCallbackQuery();
getEventProvider(Mail.TYPE)
.ifPresent(eventProviders -> eventProviders.forEach(eventProvider -> eventProvider.sendEvent(CallbackQueryConvert.apply(callbackQuery))));
}
if (update.getMyChatMember() != null) {
final ChatMemberUpdated chatMember = update.getMyChatMember();
if ("kicked".equals(chatMember.getNewChatMember().getStatus())) {
getEventProvider(Unsubscribe.TYPE)
.ifPresent(providers -> providers.forEach(provider -> provider.sendEvent(UnsubscribeConvert.apply(chatMember))));
}
if ("member".equals(chatMember.getNewChatMember().getStatus())) {
getEventProvider(Subscribe.TYPE)
.ifPresent(eventProviders -> eventProviders.forEach(eventProvider -> eventProvider.sendEvent(SubscribeConvert.apply(chatMember))));
} }
if (callbackQuery != null) {
eventProviders.forEach(provider -> provider.sendEvent(CallbackQueryConvert.apply(callbackQuery)));
} }
} }
private Optional<List<EventProvider>> getEventProvider(String type) {
return Optional.ofNullable(eventProviderMap.get(type));
}
} }

View File

@ -0,0 +1,18 @@
package dev.struchkov.godfather.telegram.service.provider;
import dev.struchkov.godfather.context.service.EventProvider;
import dev.struchkov.godfather.telegram.domain.event.Command;
public class CommandProvider implements EventProvider<Command> {
@Override
public void sendEvent(Command event) {
}
@Override
public String getEventType() {
return Command.TYPE;
}
}