This commit is contained in:
Struchkov Mark 2022-06-26 23:16:54 +03:00
parent 06d1262c8b
commit dc7f1cf767
10 changed files with 84 additions and 35 deletions

View File

@ -33,7 +33,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<godfather.core.ver>0.0.6</godfather.core.ver>
<godfather.core.ver>0.0.7</godfather.core.ver>
<telegrambots.ver>6.0.1</telegrambots.ver>
<plugin.maven.compiler.ver>3.10.1</plugin.maven.compiler.ver>

View File

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

View File

@ -2,15 +2,13 @@ package dev.struchkov.godfather.telegram;
import dev.struchkov.godfather.telegram.config.TelegramPollingConfig;
import dev.struchkov.godfather.telegram.listen.EventDistributor;
import dev.struchkov.godfather.telegram.listen.EventDistributorImpl;
import dev.struchkov.godfather.telegram.listen.EventDistributorService;
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;
import org.telegram.telegrambots.meta.bots.AbsSender;
import java.util.Optional;
/**
* TODO: Добавить описание класса.
*
@ -32,8 +30,8 @@ public class TelegramPollingBot extends TelegramLongPollingBot implements Telegr
@Override
public void onUpdateReceived(Update update) {
if (eventDistributor != null) {
Optional.ofNullable(update).ifPresent(newUpdate -> eventDistributor.processing(update));
if (update != null && eventDistributor != null) {
eventDistributor.processing(update);
}
}
@ -53,7 +51,7 @@ public class TelegramPollingBot extends TelegramLongPollingBot implements Telegr
}
@Override
public void initEventDistributor(@NotNull EventDistributorImpl eventDistributor) {
public void initEventDistributor(@NotNull EventDistributorService eventDistributor) {
this.eventDistributor = eventDistributor;
}

View File

@ -1,13 +1,12 @@
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.PersonSettingService;
import dev.struchkov.godfather.context.service.UnitPointerService;
import dev.struchkov.godfather.context.service.sender.Sending;
import dev.struchkov.godfather.core.GeneralAutoResponder;
import dev.struchkov.godfather.core.domain.unit.MainUnit;
import java.util.Set;
import java.util.List;
/**
* TODO: Добавить описание класса.
@ -17,11 +16,12 @@ import java.util.Set;
public class MessageAutoresponderTelegram extends GeneralAutoResponder<Mail> {
public MessageAutoresponderTelegram(
Set<MainUnit> menuUnit, Sending sending,
MessageService<Mail> messageService,
UnitPointerRepository<MainUnit> unitPointerRepository
Sending sending,
PersonSettingService personSettingService,
UnitPointerService unitPointerService,
List<Object> unitConfigurations
) {
super(menuUnit, sending, messageService, unitPointerRepository);
super(sending, personSettingService, unitPointerService, unitConfigurations);
}
}

View File

@ -1,21 +1,22 @@
package dev.struchkov.godfather.telegram.domain.keyboard;
import dev.struchkov.godfather.context.domain.keyboard.KeyBoard;
import dev.struchkov.godfather.context.domain.keyboard.KeyBoardLine;
import dev.struchkov.godfather.context.domain.keyboard.simple.SimpleKeyBoard;
import java.util.ArrayList;
import java.util.List;
public class InlineKeyBoard extends SimpleKeyBoard {
public class InlineKeyBoard implements KeyBoard {
public static final String TYPE = "INLINE";
protected List<KeyBoardLine> lines = new ArrayList<>();
public InlineKeyBoard(List<KeyBoardLine> lines) {
super(lines);
this.lines = lines;
}
private InlineKeyBoard(Builder builder) {
super(builder.lines);
this.lines = builder.lines;
}
public static Builder builder() {
@ -26,7 +27,10 @@ public class InlineKeyBoard extends SimpleKeyBoard {
return builder().line(keyBoardLine).build();
}
@Override
public List<KeyBoardLine> getLines() {
return lines;
}
public String getType() {
return TYPE;
}

View File

@ -14,7 +14,7 @@ public class ButtonUrl implements KeyBoardButton {
this.url = url;
}
public static ButtonUrl link(String label, String url) {
public static ButtonUrl buttonUrl(String label, String url) {
return new ButtonUrl(label, url);
}

View File

@ -0,0 +1,34 @@
package dev.struchkov.godfather.telegram.domain.keyboard.button;
import dev.struchkov.godfather.context.domain.keyboard.KeyBoardButton;
public class ButtonWebApp implements KeyBoardButton {
public static final String TYPE = "WEB_APP";
private final String label;
private final String url;
public ButtonWebApp(String label, String url) {
this.label = label;
this.url = url;
}
public static ButtonWebApp buttonWebApp(String label, String url) {
return new ButtonWebApp(label, url);
}
public String getUrl() {
return url;
}
public String getLabel() {
return label;
}
@Override
public String getType() {
return TYPE;
}
}

View File

@ -1,6 +1,7 @@
package dev.struchkov.godfather.telegram.listen;
import dev.struchkov.godfather.context.service.MailService;
import dev.struchkov.godfather.context.domain.content.Mail;
import dev.struchkov.godfather.context.service.EventProvider;
import dev.struchkov.godfather.telegram.convert.CallbackQueryConvert;
import dev.struchkov.godfather.telegram.convert.MessageMailConvert;
import org.jetbrains.annotations.NotNull;
@ -8,17 +9,19 @@ import org.telegram.telegrambots.meta.api.objects.CallbackQuery;
import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.api.objects.Update;
import java.util.List;
/**
* TODO: Добавить описание класса.
*
* @author upagge [30.01.2020]
*/
public class EventDistributorImpl implements EventDistributor {
public class EventDistributorService implements EventDistributor {
private final MailService mailService;
private final List<EventProvider<Mail>> eventProviders;
public EventDistributorImpl(TelegramConnect telegramConnect, MailService mailService) {
this.mailService = mailService;
public EventDistributorService(TelegramConnect telegramConnect, List<EventProvider<Mail>> eventProviders) {
this.eventProviders = eventProviders;
telegramConnect.initEventDistributor(this);
}
@ -27,10 +30,10 @@ public class EventDistributorImpl implements EventDistributor {
final Message message = update.getMessage();
final CallbackQuery callbackQuery = update.getCallbackQuery();
if (message != null) {
mailService.add(MessageMailConvert.apply(message));
eventProviders.forEach(provider -> provider.sendEvent(MessageMailConvert.apply(message)));
}
if (callbackQuery != null) {
mailService.add(CallbackQueryConvert.apply(callbackQuery));
eventProviders.forEach(provider -> provider.sendEvent(CallbackQueryConvert.apply(callbackQuery)));
}
}

View File

@ -109,7 +109,7 @@ public class TelegramConnect {
return telegramBot.getAdsSender();
}
void initEventDistributor(EventDistributorImpl eventDistributor) {
void initEventDistributor(EventDistributorService eventDistributor) {
telegramBot.initEventDistributor(eventDistributor);
}

View File

@ -11,6 +11,7 @@ import dev.struchkov.godfather.context.service.sender.Sending;
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.domain.keyboard.button.ButtonWebApp;
import dev.struchkov.godfather.telegram.service.SendPreProcessing;
import dev.struchkov.haiti.context.exception.ConvertException;
import dev.struchkov.haiti.utils.Inspector;
@ -26,6 +27,7 @@ import org.telegram.telegrambots.meta.api.objects.replykeyboard.ReplyKeyboardMar
import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.InlineKeyboardButton;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.KeyboardButton;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.KeyboardRow;
import org.telegram.telegrambots.meta.api.objects.webapp.WebAppInfo;
import org.telegram.telegrambots.meta.bots.AbsSender;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException;
@ -186,6 +188,12 @@ public class TelegramSender implements Sending {
button.setUrl(buttonUrl.getUrl());
button.setText(buttonUrl.getLabel());
}
case ButtonWebApp.TYPE -> {
final ButtonWebApp buttonWebApp = (ButtonWebApp) keyBoardButton;
final WebAppInfo webAppInfo = WebAppInfo.builder().url(buttonWebApp.getUrl()).build();
button.setWebApp(webAppInfo);
button.setText(buttonWebApp.getLabel());
}
default -> throw new ConvertException("Ошибка преобразования кнопки");
}
return button;
@ -199,15 +207,17 @@ public class TelegramSender implements Sending {
button.setText(simpleButton.getLabel());
Inspector.isNull(simpleButton.getCallbackData(), ConvertException.supplier("CallbackData поддерживает только Inline клавитаура"));
}
case ButtonWebApp.TYPE -> {
final ButtonWebApp buttonWebApp = (ButtonWebApp) keyBoardButton;
final WebAppInfo webAppInfo = WebAppInfo.builder().url(buttonWebApp.getUrl()).build();
button.setText(buttonWebApp.getLabel());
button.setWebApp(webAppInfo);
}
default -> throw new ConvertException("Ошибка преобразования кнопки");
}
return button;
}
public void send(Long integer, Long integer1, BoxAnswer boxAnswer) {
}
public SendType getType() {
return SendType.PRIVATE;
}