Большой рефакторинг

This commit is contained in:
Struchkov Mark 2022-04-29 01:26:12 +03:00
parent 2f756579b9
commit f849c6d620
15 changed files with 206 additions and 104 deletions

View File

@ -33,13 +33,13 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<godfather.core.ver>0.0.4-SNAPSHOT</godfather.core.ver>
<godfather.core.ver>0.0.4</godfather.core.ver>
<telegrambots.ver>6.0.1</telegrambots.ver>
<plugin.maven.compiler.ver>3.9.0</plugin.maven.compiler.ver>
<plugin.nexus.staging.ver>1.6.12</plugin.nexus.staging.ver>
<plugin.maven.compiler.ver>3.10.1</plugin.maven.compiler.ver>
<plugin.nexus.staging.ver>1.6.13</plugin.nexus.staging.ver>
<plugin.maven.source.ver>3.2.1</plugin.maven.source.ver>
<plugin.maven.javadoc.ver>3.3.2</plugin.maven.javadoc.ver>
<plugin.maven.javadoc.ver>3.4.0</plugin.maven.javadoc.ver>
<plugin.maven.gpg.ver>3.0.1</plugin.maven.gpg.ver>
</properties>

View File

@ -1,21 +1,10 @@
package dev.struchkov.godfather.telegram;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
/**
* TODO: Добавить описание класса.
*
* @author upagge [30.01.2020]
*/
@Builder
@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class ProxyConfig {
private String host;
@ -24,8 +13,47 @@ public class ProxyConfig {
private String password;
private Type type;
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public Integer getPort() {
return port;
}
public void setPort(Integer port) {
this.port = port;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Type getType() {
return type;
}
public void setType(Type type) {
this.type = type;
}
public enum Type {
SOCKS5, SOCKS4, HTTP
}
}

View File

@ -1,7 +1,7 @@
package dev.struchkov.godfather.telegram;
import lombok.NonNull;
import dev.struchkov.godfather.telegram.listen.EventDistributorImpl;
import org.jetbrains.annotations.NotNull;
import org.telegram.telegrambots.meta.bots.AbsSender;
/**
@ -13,6 +13,6 @@ public interface TelegramBot {
AbsSender getAdsSender();
void initEventDistributor(@NonNull EventDistributorImpl eventDistributor);
void initEventDistributor(@NotNull EventDistributorImpl eventDistributor);
}

View File

@ -1,9 +1,9 @@
package dev.struchkov.godfather.telegram;
import lombok.NonNull;
import dev.struchkov.godfather.telegram.listen.EventDistributorImpl;
import dev.struchkov.godfather.telegram.config.TelegramPollingConfig;
import dev.struchkov.godfather.telegram.listen.EventDistributor;
import dev.struchkov.godfather.telegram.listen.EventDistributorImpl;
import org.jetbrains.annotations.NotNull;
import org.telegram.telegrambots.bots.DefaultBotOptions;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.meta.api.objects.Update;
@ -53,7 +53,7 @@ public class TelegramPollingBot extends TelegramLongPollingBot implements Telegr
}
@Override
public void initEventDistributor(@NonNull EventDistributorImpl eventDistributor) {
public void initEventDistributor(@NotNull EventDistributorImpl eventDistributor) {
this.eventDistributor = eventDistributor;
}

View File

@ -1,11 +1,11 @@
package dev.struchkov.godfather.telegram.autoresponder;
import dev.struchkov.autoresponder.repository.UnitPointerRepository;
import dev.struchkov.godfather.context.domain.content.Mail;
import dev.struchkov.godfather.context.service.MessageService;
import dev.struchkov.godfather.context.service.sender.Sending;
import dev.struchkov.godfather.core.GeneralAutoResponder;
import dev.struchkov.godfather.core.domain.unit.MainUnit;
import org.sadtech.autoresponder.repository.UnitPointerRepository;
import java.util.Set;
@ -19,7 +19,7 @@ public class MessageAutoresponderTelegram extends GeneralAutoResponder<Mail> {
public MessageAutoresponderTelegram(
Set<MainUnit> menuUnit, Sending sending,
MessageService<Mail> messageService,
UnitPointerRepository unitPointerRepository
UnitPointerRepository<MainUnit> unitPointerRepository
) {
super(menuUnit, sending, messageService, unitPointerRepository);
}

View File

@ -1,29 +1,38 @@
package dev.struchkov.godfather.telegram.config;
import dev.struchkov.godfather.telegram.ProxyConfig;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.NonNull;
/**
* TODO: Добавить описание класса.
*
* @author upagge [18.08.2019]
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class TelegramPollingConfig {
@NonNull
private String botUsername;
private final String botUsername;
private final String botToken;
@NonNull
private String botToken;
private ProxyConfig proxyConfig;
private ProxyConfig proxyConfig = new ProxyConfig();
public TelegramPollingConfig(String botUsername, String botToken) {
this.botUsername = botUsername;
this.botToken = botToken;
}
public String getBotUsername() {
return botUsername;
}
public String getBotToken() {
return botToken;
}
public ProxyConfig getProxyConfig() {
return proxyConfig;
}
public void setProxyConfig(ProxyConfig proxyConfig) {
this.proxyConfig = proxyConfig;
}
}

View File

@ -1,26 +0,0 @@
package dev.struchkov.godfather.telegram.config;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.NonNull;
/**
* TODO: Добавить описание класса.
*
* @author upagge [12.02.2020]
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class TelegramWebHookConfig {
@NonNull
private String botUsername;
@NonNull
private String botToken;
}

View File

@ -3,8 +3,6 @@ package dev.struchkov.godfather.telegram.convert;
import dev.struchkov.godfather.context.domain.content.Mail;
import dev.struchkov.godfather.context.domain.content.attachment.Attachment;
import dev.struchkov.godfather.context.domain.content.attachment.Link;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.api.objects.MessageEntity;
@ -15,23 +13,28 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import static dev.struchkov.haiti.utils.Exceptions.utilityClass;
/**
* TODO: Добавить описание класса.
*
* @author upagge [18.08.2019]
*/
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public class MessageMailConvert {
public final class MessageMailConvert {
public MessageMailConvert() {
utilityClass();
}
public static Mail apply(Message message) {
Mail mail = new Mail();
final Mail mail = new Mail();
mail.setPersonId(message.getChatId());
mail.setAddDate(LocalDateTime.now());
mail.setText(message.getText());
mail.setCreateDate(LocalDateTime.ofInstant(Instant.ofEpochSecond(message.getDate()), ZoneId.systemDefault()));
mail.setFirstName(message.getChat().getFirstName());
mail.setLastName(message.getChat().getLastName());
List<MessageEntity> entities = message.getEntities();
final List<MessageEntity> entities = message.getEntities();
if (entities != null) {
mail.setAttachments(convertAttachments(entities));
}
@ -44,7 +47,7 @@ public class MessageMailConvert {
}
private static List<Attachment> convertAttachments(List<MessageEntity> entities) {
final List<Attachment> attachments = new ArrayList();
final List<Attachment> attachments = new ArrayList<>();
for (MessageEntity entity : entities) {
String type = entity.getType();
if ("text_link".equals(type)) {

View File

@ -2,22 +2,50 @@ package dev.struchkov.godfather.telegram.domain.keyboard;
import dev.struchkov.godfather.context.domain.keyboard.KeyBoardLine;
import dev.struchkov.godfather.context.domain.keyboard.simple.SimpleKeyBoard;
import lombok.Builder;
import lombok.Singular;
import java.util.ArrayList;
import java.util.List;
public class InlineKeyBoard extends SimpleKeyBoard {
public static final String TYPE = "INLINE";
@Builder
public InlineKeyBoard(@Singular("line") List<KeyBoardLine> lines) {
public InlineKeyBoard(List<KeyBoardLine> lines) {
super(lines);
}
private InlineKeyBoard(Builder builder) {
super(builder.lines);
}
public static Builder builder() {
return new Builder();
}
@Override
public String getType() {
return TYPE;
}
public static final class Builder {
private List<KeyBoardLine> lines = new ArrayList<>();
private Builder() {
}
public Builder lines(List<KeyBoardLine> val) {
lines = val;
return this;
}
public Builder line(KeyBoardLine val) {
lines.add(val);
return this;
}
public InlineKeyBoard build() {
return new InlineKeyBoard(this);
}
}
}

View File

@ -2,13 +2,10 @@ package dev.struchkov.godfather.telegram.domain.keyboard;
import dev.struchkov.godfather.context.domain.keyboard.KeyBoardLine;
import dev.struchkov.godfather.context.domain.keyboard.simple.SimpleKeyBoard;
import lombok.Builder;
import lombok.Getter;
import lombok.Singular;
import java.util.ArrayList;
import java.util.List;
@Getter
public class MarkupKeyBoard extends SimpleKeyBoard {
public static final String TYPE = "MARKUP";
@ -25,17 +22,23 @@ public class MarkupKeyBoard extends SimpleKeyBoard {
private String inputFieldPlaceholder;
@Builder
protected MarkupKeyBoard(
@Singular(value = "line") List<KeyBoardLine> keyBoardLines,
boolean oneTime,
boolean resizeKeyboard,
String inputFieldPlaceholder
) {
super(keyBoardLines);
this.oneTime = oneTime;
this.resizeKeyboard = resizeKeyboard;
this.inputFieldPlaceholder = inputFieldPlaceholder;
private MarkupKeyBoard(Builder builder) {
super(builder.lines);
oneTime = builder.oneTime;
resizeKeyboard = builder.resizeKeyboard;
inputFieldPlaceholder = builder.inputFieldPlaceholder;
}
public static Builder newBuilder() {
return new Builder();
}
public boolean isResizeKeyboard() {
return resizeKeyboard;
}
public String getInputFieldPlaceholder() {
return inputFieldPlaceholder;
}
public boolean isOneTime() {
@ -47,4 +50,42 @@ public class MarkupKeyBoard extends SimpleKeyBoard {
return TYPE;
}
public static final class Builder {
private List<KeyBoardLine> lines = new ArrayList<>();
private boolean oneTime = true;
private boolean resizeKeyboard;
private String inputFieldPlaceholder;
private Builder() {
}
public Builder lines(List<KeyBoardLine> val) {
lines = val;
return this;
}
public Builder line(KeyBoardLine val) {
lines.add(val);
return this;
}
public Builder oneTime(boolean val) {
oneTime = val;
return this;
}
public Builder resizeKeyboard(boolean val) {
resizeKeyboard = val;
return this;
}
public Builder inputFieldPlaceholder(String val) {
inputFieldPlaceholder = val;
return this;
}
public MarkupKeyBoard build() {
return new MarkupKeyBoard(this);
}
}
}

View File

@ -1,11 +1,7 @@
package dev.struchkov.godfather.telegram.domain.keyboard.button;
import dev.struchkov.godfather.context.domain.keyboard.KeyBoardButton;
import lombok.Builder;
import lombok.Getter;
@Getter
@Builder
public class ButtonUrl implements KeyBoardButton {
public static final String TYPE = "URL";
@ -13,9 +9,25 @@ public class ButtonUrl implements KeyBoardButton {
private String label;
private String url;
public ButtonUrl(String label, String url) {
this.label = label;
this.url = url;
}
public static ButtonUrl link(String label, String url) {
return new ButtonUrl(label, url);
}
@Override
public String getType() {
return TYPE;
}
public String getLabel() {
return label;
}
public String getUrl() {
return url;
}
}

View File

@ -1,11 +1,11 @@
package dev.struchkov.godfather.telegram.listen;
import lombok.NonNull;
import org.jetbrains.annotations.NotNull;
import org.telegram.telegrambots.meta.api.objects.Update;
public interface EventDistributor {
void processing(@NonNull Update update);
void processing(@NotNull Update update);
}

View File

@ -1,9 +1,9 @@
package dev.struchkov.godfather.telegram.listen;
import dev.struchkov.godfather.context.service.MailService;
import lombok.NonNull;
import dev.struchkov.godfather.telegram.convert.CallbackQueryConvert;
import dev.struchkov.godfather.telegram.convert.MessageMailConvert;
import org.jetbrains.annotations.NotNull;
import org.telegram.telegrambots.meta.api.objects.CallbackQuery;
import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.api.objects.Update;
@ -23,7 +23,7 @@ public class EventDistributorImpl implements EventDistributor {
}
@Override
public void processing(@NonNull Update update) {
public void processing(@NotNull Update update) {
final Message message = update.getMessage();
final CallbackQuery callbackQuery = update.getCallbackQuery();
if (message != null) {

View File

@ -4,7 +4,8 @@ import dev.struchkov.godfather.telegram.ProxyConfig;
import dev.struchkov.godfather.telegram.TelegramBot;
import dev.struchkov.godfather.telegram.TelegramPollingBot;
import dev.struchkov.godfather.telegram.config.TelegramPollingConfig;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.telegram.telegrambots.bots.DefaultBotOptions;
import org.telegram.telegrambots.meta.TelegramBotsApi;
import org.telegram.telegrambots.meta.bots.AbsSender;
@ -19,9 +20,10 @@ import java.net.PasswordAuthentication;
*
* @author upagge [30.01.2020]
*/
@Slf4j
public class TelegramConnect {
private static final Logger log = LoggerFactory.getLogger(TelegramConnect.class);
private TelegramBot telegramBot;
public TelegramConnect(TelegramPollingConfig telegramPollingConfig) {

View File

@ -12,8 +12,8 @@ import dev.struchkov.godfather.telegram.domain.keyboard.InlineKeyBoard;
import dev.struchkov.godfather.telegram.domain.keyboard.MarkupKeyBoard;
import dev.struchkov.godfather.telegram.domain.keyboard.button.ButtonUrl;
import dev.struchkov.godfather.telegram.service.SendPreProcessing;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
import org.telegram.telegrambots.meta.api.methods.updatingmessages.EditMessageText;
import org.telegram.telegrambots.meta.api.objects.Message;
@ -36,18 +36,23 @@ import java.util.Map;
*
* @author upagge [15/07/2019]
*/
@Slf4j
public class TelegramSender implements Sending {
private static final Logger log = LoggerFactory.getLogger(TelegramSender.class);
private final AbsSender absSender;
private Map<Long, Integer> map = new HashMap<>();
@Setter
private SendPreProcessing sendPreProcessing;
public TelegramSender(TelegramConnect telegramConnect) {
this.absSender = telegramConnect.getAdsSender();
}
public void setSendPreProcessing(SendPreProcessing sendPreProcessing) {
this.sendPreProcessing = sendPreProcessing;
}
public void send(Long telegramId, BoxAnswer boxAnswer) {
try {
if (boxAnswer.isReplace() && map.containsKey(telegramId)) {
@ -90,7 +95,7 @@ public class TelegramSender implements Sending {
return convertSimpleKeyBoard((SimpleKeyBoard) keyBoard);
}
}
throw new RuntimeException("Ошибка преобразования клавиаутры");
return null;
}
private ReplyKeyboard convertSimpleKeyBoard(SimpleKeyBoard keyBoard) {