Добавил клавиатуры
This commit is contained in:
parent
cd728796c2
commit
2f756579b9
4
pom.xml
4
pom.xml
@ -33,8 +33,8 @@
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
|
||||
<godfather.core.ver>0.0.3</godfather.core.ver>
|
||||
<telegrambots.ver>5.7.1</telegrambots.ver>
|
||||
<godfather.core.ver>0.0.4-SNAPSHOT</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>
|
||||
|
@ -5,11 +5,10 @@
|
||||
<parent>
|
||||
<groupId>dev.struchkov.godfather</groupId>
|
||||
<artifactId>telegram-bot</artifactId>
|
||||
<version>0.0.3</version>
|
||||
<version>0.0.4-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>telegram-core</artifactId>
|
||||
<version>0.0.3</version>
|
||||
|
||||
<name>Telegram Core</name>
|
||||
<description>Allows you to create bots for Telegram</description>
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
@ -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.KeyBoardButton;
|
||||
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.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 lombok.Setter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
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.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.KeyboardButton;
|
||||
import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.KeyboardRow;
|
||||
import org.telegram.telegrambots.meta.bots.AbsSender;
|
||||
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
|
||||
import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* TODO: Добавить описание класса.
|
||||
@ -30,7 +40,7 @@ import java.util.stream.Collectors;
|
||||
public class TelegramSender implements Sending {
|
||||
|
||||
private final AbsSender absSender;
|
||||
|
||||
private Map<Long, Integer> map = new HashMap<>();
|
||||
@Setter
|
||||
private SendPreProcessing sendPreProcessing;
|
||||
|
||||
@ -38,18 +48,30 @@ public class TelegramSender implements Sending {
|
||||
this.absSender = telegramConnect.getAdsSender();
|
||||
}
|
||||
|
||||
public void send(Long integer, BoxAnswer boxAnswer) {
|
||||
SendMessage sendMessage = new SendMessage();
|
||||
public void send(Long telegramId, BoxAnswer boxAnswer) {
|
||||
try {
|
||||
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(integer));
|
||||
sendMessage.setChatId(String.valueOf(telegramId));
|
||||
sendMessage.setText(
|
||||
sendPreProcessing != null
|
||||
? sendPreProcessing.pretreatment(boxAnswer.getMessage())
|
||||
: boxAnswer.getMessage()
|
||||
);
|
||||
sendMessage.setReplyMarkup(convertKeyBoard(boxAnswer.getKeyBoard()));
|
||||
try {
|
||||
absSender.execute(sendMessage);
|
||||
|
||||
final Message execute = absSender.execute(sendMessage);
|
||||
map.put(telegramId, execute.getMessageId());
|
||||
}
|
||||
} catch (TelegramApiRequestException e) {
|
||||
log.error(e.getApiResponse());
|
||||
} catch (TelegramApiException e) {
|
||||
@ -59,31 +81,90 @@ public class TelegramSender implements Sending {
|
||||
|
||||
private ReplyKeyboard convertKeyBoard(KeyBoard keyBoard) {
|
||||
if (keyBoard != null) {
|
||||
switch (keyBoard.getType()) {
|
||||
case InlineKeyBoard.TYPE:
|
||||
return convertInlineKeyBoard((InlineKeyBoard) keyBoard);
|
||||
case MarkupKeyBoard.TYPE:
|
||||
return convertMarkupKeyBoard((MarkupKeyBoard) keyBoard);
|
||||
case SimpleKeyBoard.TYPE:
|
||||
return convertSimpleKeyBoard((SimpleKeyBoard) keyBoard);
|
||||
}
|
||||
}
|
||||
throw new RuntimeException("Ошибка преобразования клавиаутры");
|
||||
}
|
||||
|
||||
private ReplyKeyboard convertSimpleKeyBoard(SimpleKeyBoard keyBoard) {
|
||||
final ReplyKeyboardMarkup keyboardMarkup = new ReplyKeyboardMarkup();
|
||||
keyboardMarkup.setKeyboard(
|
||||
keyBoard.getLines().stream()
|
||||
.map(this::convertMarkupLine)
|
||||
.toList()
|
||||
);
|
||||
return keyboardMarkup;
|
||||
}
|
||||
|
||||
private ReplyKeyboard convertMarkupKeyBoard(MarkupKeyBoard keyBoard) {
|
||||
final ReplyKeyboardMarkup keyboardMarkup = new ReplyKeyboardMarkup();
|
||||
keyboardMarkup.setOneTimeKeyboard(keyBoard.isOneTime());
|
||||
keyboardMarkup.setInputFieldPlaceholder(keyBoard.getInputFieldPlaceholder());
|
||||
keyboardMarkup.setResizeKeyboard(keyBoard.isResizeKeyboard());
|
||||
keyboardMarkup.setKeyboard(
|
||||
keyBoard.getLines().stream()
|
||||
.map(this::convertMarkupLine)
|
||||
.toList()
|
||||
);
|
||||
return keyboardMarkup;
|
||||
}
|
||||
|
||||
private InlineKeyboardMarkup convertInlineKeyBoard(InlineKeyBoard keyBoard) {
|
||||
final InlineKeyboardMarkup inlineKeyboardMarkup = new InlineKeyboardMarkup();
|
||||
inlineKeyboardMarkup.setKeyboard(
|
||||
keyBoard.getKeyBoardLines()
|
||||
.stream()
|
||||
.map(this::convertLint)
|
||||
keyBoard.getLines().stream()
|
||||
.map(this::convertInlineLine)
|
||||
.toList()
|
||||
);
|
||||
return inlineKeyboardMarkup;
|
||||
}
|
||||
return null;
|
||||
|
||||
private List<InlineKeyboardButton> convertInlineLine(KeyBoardLine line) {
|
||||
return line.getButtons().stream().map(this::convertInlineButton).toList();
|
||||
}
|
||||
|
||||
private List<InlineKeyboardButton> convertLint(KeyBoardLine line) {
|
||||
return line.getKeyBoardButtons().stream().map(this::convertButton).collect(Collectors.toList());
|
||||
private KeyboardRow convertMarkupLine(KeyBoardLine line) {
|
||||
final List<KeyboardButton> buttons = line.getButtons().stream().map(this::convertMarkupButton).toList();
|
||||
return new KeyboardRow(buttons);
|
||||
}
|
||||
|
||||
private InlineKeyboardButton convertButton(KeyBoardButton button) {
|
||||
InlineKeyboardButton inlineKeyboardButton = new InlineKeyboardButton();
|
||||
switch (button.getType()) {
|
||||
case TEXT:
|
||||
KeyBoardButtonText keyBoardButtonText = (KeyBoardButtonText) button;
|
||||
inlineKeyboardButton.setText(keyBoardButtonText.getLabel());
|
||||
inlineKeyboardButton.setCallbackData(keyBoardButtonText.getLabel());
|
||||
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);
|
||||
}
|
||||
return inlineKeyboardButton;
|
||||
case ButtonUrl.TYPE -> {
|
||||
final ButtonUrl buttonUrl = (ButtonUrl) keyBoardButton;
|
||||
button.setUrl(buttonUrl.getUrl());
|
||||
button.setText(buttonUrl.getLabel());
|
||||
}
|
||||
default -> throw new RuntimeException("Ошибка преобразования кнопки");
|
||||
}
|
||||
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) {
|
||||
|
Loading…
Reference in New Issue
Block a user