Добавил клавиатуры

This commit is contained in:
Struchkov Mark 2022-04-25 21:47:49 +03:00
parent cd728796c2
commit 2f756579b9
6 changed files with 211 additions and 37 deletions

View File

@ -33,8 +33,8 @@
<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.3</godfather.core.ver> <godfather.core.ver>0.0.4-SNAPSHOT</godfather.core.ver>
<telegrambots.ver>5.7.1</telegrambots.ver> <telegrambots.ver>6.0.1</telegrambots.ver>
<plugin.maven.compiler.ver>3.9.0</plugin.maven.compiler.ver> <plugin.maven.compiler.ver>3.9.0</plugin.maven.compiler.ver>
<plugin.nexus.staging.ver>1.6.12</plugin.nexus.staging.ver> <plugin.nexus.staging.ver>1.6.12</plugin.nexus.staging.ver>

View File

@ -5,11 +5,10 @@
<parent> <parent>
<groupId>dev.struchkov.godfather</groupId> <groupId>dev.struchkov.godfather</groupId>
<artifactId>telegram-bot</artifactId> <artifactId>telegram-bot</artifactId>
<version>0.0.3</version> <version>0.0.4-SNAPSHOT</version>
</parent> </parent>
<artifactId>telegram-core</artifactId> <artifactId>telegram-core</artifactId>
<version>0.0.3</version>
<name>Telegram Core</name> <name>Telegram Core</name>
<description>Allows you to create bots for Telegram</description> <description>Allows you to create bots for Telegram</description>

View File

@ -0,0 +1,23 @@
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.List;
public class InlineKeyBoard extends SimpleKeyBoard {
public static final String TYPE = "INLINE";
@Builder
public InlineKeyBoard(@Singular("line") List<KeyBoardLine> lines) {
super(lines);
}
@Override
public String getType() {
return TYPE;
}
}

View File

@ -0,0 +1,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.Getter;
import lombok.Singular;
import java.util.List;
@Getter
public class MarkupKeyBoard extends SimpleKeyBoard {
public static final String TYPE = "MARKUP";
/**
* Скрыть меню после ответа или нет.
*/
private boolean oneTime = true;
/**
* Изменяет размер клавиатуры по вертикали для оптимального соответствия (например, сделать клавиатуру меньше, если есть только два ряда кнопок).
*/
private boolean resizeKeyboard;
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;
}
public boolean isOneTime() {
return oneTime;
}
@Override
public String getType() {
return TYPE;
}
}

View File

@ -0,0 +1,21 @@
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";
private String label;
private String url;
@Override
public String getType() {
return TYPE;
}
}

View File

@ -4,22 +4,32 @@ import dev.struchkov.godfather.context.domain.BoxAnswer;
import dev.struchkov.godfather.context.domain.keyboard.KeyBoard; import dev.struchkov.godfather.context.domain.keyboard.KeyBoard;
import dev.struchkov.godfather.context.domain.keyboard.KeyBoardButton; import dev.struchkov.godfather.context.domain.keyboard.KeyBoardButton;
import dev.struchkov.godfather.context.domain.keyboard.KeyBoardLine; import dev.struchkov.godfather.context.domain.keyboard.KeyBoardLine;
import dev.struchkov.godfather.context.domain.keyboard.button.KeyBoardButtonText; import dev.struchkov.godfather.context.domain.keyboard.button.SimpleButton;
import dev.struchkov.godfather.context.domain.keyboard.simple.SimpleKeyBoard;
import dev.struchkov.godfather.context.service.sender.SendType; import dev.struchkov.godfather.context.service.sender.SendType;
import dev.struchkov.godfather.context.service.sender.Sending; 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.service.SendPreProcessing; import dev.struchkov.godfather.telegram.service.SendPreProcessing;
import lombok.Setter; import lombok.Setter;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage; 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;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.InlineKeyboardMarkup; import org.telegram.telegrambots.meta.api.objects.replykeyboard.InlineKeyboardMarkup;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.ReplyKeyboard; import org.telegram.telegrambots.meta.api.objects.replykeyboard.ReplyKeyboard;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.ReplyKeyboardMarkup;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.InlineKeyboardButton; 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.bots.AbsSender; import org.telegram.telegrambots.meta.bots.AbsSender;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException; import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException; import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.Map;
/** /**
* TODO: Добавить описание класса. * TODO: Добавить описание класса.
@ -30,7 +40,7 @@ import java.util.stream.Collectors;
public class TelegramSender implements Sending { public class TelegramSender implements Sending {
private final AbsSender absSender; private final AbsSender absSender;
private Map<Long, Integer> map = new HashMap<>();
@Setter @Setter
private SendPreProcessing sendPreProcessing; private SendPreProcessing sendPreProcessing;
@ -38,18 +48,30 @@ public class TelegramSender implements Sending {
this.absSender = telegramConnect.getAdsSender(); this.absSender = telegramConnect.getAdsSender();
} }
public void send(Long integer, BoxAnswer boxAnswer) { public void send(Long telegramId, BoxAnswer boxAnswer) {
SendMessage sendMessage = new SendMessage();
sendMessage.enableMarkdown(true);
sendMessage.setChatId(String.valueOf(integer));
sendMessage.setText(
sendPreProcessing != null
? sendPreProcessing.pretreatment(boxAnswer.getMessage())
: boxAnswer.getMessage()
);
sendMessage.setReplyMarkup(convertKeyBoard(boxAnswer.getKeyBoard()));
try { try {
absSender.execute(sendMessage); if (boxAnswer.isReplace() && map.containsKey(telegramId)) {
final EditMessageText editMessageText = new EditMessageText();
editMessageText.setChatId(String.valueOf(telegramId));
editMessageText.setMessageId(map.get(telegramId));
editMessageText.enableMarkdown(true);
editMessageText.setText(boxAnswer.getMessage());
editMessageText.setReplyMarkup(convertInlineKeyBoard((InlineKeyBoard) boxAnswer.getKeyBoard()));
absSender.execute(editMessageText);
} else {
final SendMessage sendMessage = new SendMessage();
sendMessage.enableMarkdown(true);
sendMessage.setChatId(String.valueOf(telegramId));
sendMessage.setText(
sendPreProcessing != null
? sendPreProcessing.pretreatment(boxAnswer.getMessage())
: boxAnswer.getMessage()
);
sendMessage.setReplyMarkup(convertKeyBoard(boxAnswer.getKeyBoard()));
final Message execute = absSender.execute(sendMessage);
map.put(telegramId, execute.getMessageId());
}
} catch (TelegramApiRequestException e) { } catch (TelegramApiRequestException e) {
log.error(e.getApiResponse()); log.error(e.getApiResponse());
} catch (TelegramApiException e) { } catch (TelegramApiException e) {
@ -59,31 +81,90 @@ public class TelegramSender implements Sending {
private ReplyKeyboard convertKeyBoard(KeyBoard keyBoard) { private ReplyKeyboard convertKeyBoard(KeyBoard keyBoard) {
if (keyBoard != null) { if (keyBoard != null) {
final InlineKeyboardMarkup inlineKeyboardMarkup = new InlineKeyboardMarkup(); switch (keyBoard.getType()) {
inlineKeyboardMarkup.setKeyboard( case InlineKeyBoard.TYPE:
keyBoard.getKeyBoardLines() return convertInlineKeyBoard((InlineKeyBoard) keyBoard);
.stream() case MarkupKeyBoard.TYPE:
.map(this::convertLint) return convertMarkupKeyBoard((MarkupKeyBoard) keyBoard);
.toList() case SimpleKeyBoard.TYPE:
); return convertSimpleKeyBoard((SimpleKeyBoard) keyBoard);
return inlineKeyboardMarkup; }
} }
return null; throw new RuntimeException("Ошибка преобразования клавиаутры");
} }
private List<InlineKeyboardButton> convertLint(KeyBoardLine line) { private ReplyKeyboard convertSimpleKeyBoard(SimpleKeyBoard keyBoard) {
return line.getKeyBoardButtons().stream().map(this::convertButton).collect(Collectors.toList()); final ReplyKeyboardMarkup keyboardMarkup = new ReplyKeyboardMarkup();
keyboardMarkup.setKeyboard(
keyBoard.getLines().stream()
.map(this::convertMarkupLine)
.toList()
);
return keyboardMarkup;
} }
private InlineKeyboardButton convertButton(KeyBoardButton button) { private ReplyKeyboard convertMarkupKeyBoard(MarkupKeyBoard keyBoard) {
InlineKeyboardButton inlineKeyboardButton = new InlineKeyboardButton(); final ReplyKeyboardMarkup keyboardMarkup = new ReplyKeyboardMarkup();
switch (button.getType()) { keyboardMarkup.setOneTimeKeyboard(keyBoard.isOneTime());
case TEXT: keyboardMarkup.setInputFieldPlaceholder(keyBoard.getInputFieldPlaceholder());
KeyBoardButtonText keyBoardButtonText = (KeyBoardButtonText) button; keyboardMarkup.setResizeKeyboard(keyBoard.isResizeKeyboard());
inlineKeyboardButton.setText(keyBoardButtonText.getLabel()); keyboardMarkup.setKeyboard(
inlineKeyboardButton.setCallbackData(keyBoardButtonText.getLabel()); keyBoard.getLines().stream()
.map(this::convertMarkupLine)
.toList()
);
return keyboardMarkup;
}
private InlineKeyboardMarkup convertInlineKeyBoard(InlineKeyBoard keyBoard) {
final InlineKeyboardMarkup inlineKeyboardMarkup = new InlineKeyboardMarkup();
inlineKeyboardMarkup.setKeyboard(
keyBoard.getLines().stream()
.map(this::convertInlineLine)
.toList()
);
return inlineKeyboardMarkup;
}
private List<InlineKeyboardButton> convertInlineLine(KeyBoardLine line) {
return line.getButtons().stream().map(this::convertInlineButton).toList();
}
private KeyboardRow convertMarkupLine(KeyBoardLine line) {
final List<KeyboardButton> buttons = line.getButtons().stream().map(this::convertMarkupButton).toList();
return new KeyboardRow(buttons);
}
private InlineKeyboardButton convertInlineButton(KeyBoardButton keyBoardButton) {
final InlineKeyboardButton button = new InlineKeyboardButton();
switch (keyBoardButton.getType()) {
case SimpleButton.TYPE -> {
final SimpleButton simpleButton = (SimpleButton) keyBoardButton;
final String callbackData = simpleButton.getCallbackData();
final String label = simpleButton.getLabel();
button.setText(label);
button.setCallbackData(callbackData != null ? callbackData : label);
}
case ButtonUrl.TYPE -> {
final ButtonUrl buttonUrl = (ButtonUrl) keyBoardButton;
button.setUrl(buttonUrl.getUrl());
button.setText(buttonUrl.getLabel());
}
default -> throw new RuntimeException("Ошибка преобразования кнопки");
} }
return inlineKeyboardButton; return button;
}
private KeyboardButton convertMarkupButton(KeyBoardButton keyBoardButton) {
final KeyboardButton button = new KeyboardButton();
switch (keyBoardButton.getType()) {
case SimpleButton.TYPE -> {
final SimpleButton simpleButton = (SimpleButton) keyBoardButton;
button.setText(simpleButton.getLabel());
}
default -> throw new RuntimeException("Ошибка преобразования кнопки");
}
return button;
} }
public void send(Long integer, Long integer1, BoxAnswer boxAnswer) { public void send(Long integer, Long integer1, BoxAnswer boxAnswer) {