commands with callbackData support
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2021-09-09 23:55:14 +03:00
parent 932da0549a
commit 24b15ca4fe
15 changed files with 353 additions and 176 deletions

View File

@@ -2,7 +2,11 @@ package ru.penkrat.stbf.impl.pengrad;
import com.pengrad.telegrambot.TelegramBot;
import com.pengrad.telegrambot.model.Update;
import com.pengrad.telegrambot.model.request.*;
import com.pengrad.telegrambot.model.request.InlineKeyboardButton;
import com.pengrad.telegrambot.model.request.InlineKeyboardMarkup;
import com.pengrad.telegrambot.model.request.KeyboardButton;
import com.pengrad.telegrambot.model.request.ParseMode;
import com.pengrad.telegrambot.model.request.ReplyKeyboardMarkup;
import com.pengrad.telegrambot.request.DeleteMessage;
import com.pengrad.telegrambot.request.EditMessageText;
import com.pengrad.telegrambot.request.SendDocument;

View File

@@ -0,0 +1,14 @@
package ru.penkrat.stbf.impl.pengrad;
import ru.penkrat.stbf.api.Keyboard;
class NoKeyboard implements Keyboard {
@Override
public String toString() {
return "EmptyKeyboard(pengrad)";
}
}

View File

@@ -2,149 +2,166 @@ package ru.penkrat.stbf.impl.pengrad;
import com.pengrad.telegrambot.model.request.InlineKeyboardButton;
import com.pengrad.telegrambot.model.request.KeyboardButton;
import ru.penkrat.stbf.api.Action;
import ru.penkrat.stbf.api.Keyboard;
import ru.penkrat.stbf.api.KeyboardBuilder;
public class PengradKeyboardBuilder implements KeyboardBuilder {
private KeyboardButton[][] keyboard;
private KeyboardButton[][] keyboard;
private InlineKeyboardButton[][] inlineKeyboard;
private InlineKeyboardButton[][] inlineKeyboard;
private String keyboardStr = "";
private String keyboardStr = "";
public static Keyboard singleKey(Action action) {
return KeyboardBuilder.newKeyboard().add(action).build();
}
public static Keyboard singleKey(Action action) {
return KeyboardBuilder.newKeyboard().add(action).build();
}
@Override
public Keyboard build() {
return new KeyboardImpl(keyboard, inlineKeyboard);
}
@Override
public Keyboard build() {
if (keyboard != null) {
return new KeyboardImpl(keyboard, null);
}
if (inlineKeyboard != null) {
InlineKeyboardButton[][] smallerArray = new InlineKeyboardButton[inlineKeyboard.length - 1][];
System.arraycopy(inlineKeyboard, 0, smallerArray, 0, inlineKeyboard.length - 1);
return new KeyboardImpl(null, smallerArray);
}
return new NoKeyboard();
}
public KeyboardBuilder addGetContact(String text) {
put(new KeyboardButton(text).requestContact(true));
return self();
}
public KeyboardBuilder addGetContact(String text) {
put(new KeyboardButton(text).requestContact(true));
return self();
}
public KeyboardBuilder add(String text) {
put(new KeyboardButton(text));
return self();
}
public KeyboardBuilder add(String text) {
put(new KeyboardButton(text));
return self();
}
@Override
public KeyboardBuilder add(Action action) {
put(action);
return self();
}
@Override
public KeyboardBuilder add(Action action) {
put(action);
return self();
}
public KeyboardBuilder row(KeyboardButton... buttons) {
for (int i = 0; i < buttons.length; i++) {
put(buttons[i]);
}
nextRow();
return self();
}
public KeyboardBuilder row(KeyboardButton... buttons) {
for (int i = 0; i < buttons.length; i++) {
put(buttons[i]);
}
nextRow();
return self();
}
@Override
public KeyboardBuilder row(Action... buttons) {
for (int i = 0; i < buttons.length; i++) {
put(buttons[i]);
}
nextRow();
return self();
}
@Override
public KeyboardBuilder row(Action... buttons) {
for (int i = 0; i < buttons.length; i++) {
put(buttons[i]);
}
if (buttons.length > 0) {
if (buttons[0].isInline()) {
nextRowI();
} else {
nextRow();
}
}
return self();
}
@Override
public KeyboardBuilder column(Action... buttons) {
for (int i = 0; i < buttons.length; i++) {
put(buttons[i]);
if (buttons[i].isInline()) {
nextRowI();
} else {
nextRow();
}
}
return self();
}
@Override
public KeyboardBuilder column(Action... buttons) {
for (int i = 0; i < buttons.length; i++) {
put(buttons[i]);
if (buttons[i].isInline()) {
nextRowI();
} else {
nextRow();
}
}
return self();
}
public KeyboardBuilder addNl(String text) {
add(text);
nextRow();
return self();
}
public KeyboardBuilder addNl(String text) {
add(text);
nextRow();
return self();
}
public PengradKeyboardBuilder add(String text, String data) {
put(new InlineKeyboardButton(text).callbackData(data));
return self();
}
public PengradKeyboardBuilder add(String text, String data) {
put(new InlineKeyboardButton(text).callbackData(data));
return self();
}
public KeyboardBuilder addNl(String text, String data) {
add(text, data);
nextRowI();
return self();
}
public KeyboardBuilder addNl(String text, String data) {
add(text, data);
nextRowI();
return self();
}
private void nextRow() {
KeyboardButton[][] n = new KeyboardButton[keyboard.length + 1][];
System.arraycopy(keyboard, 0, n, 0, keyboard.length);
keyboard = n;
keyboard[keyboard.length - 1] = new KeyboardButton[] {};
}
private void nextRow() {
KeyboardButton[][] n = new KeyboardButton[keyboard.length + 1][];
System.arraycopy(keyboard, 0, n, 0, keyboard.length);
keyboard = n;
keyboard[keyboard.length - 1] = new KeyboardButton[]{};
}
private void nextRowI() {
InlineKeyboardButton[][] n = new InlineKeyboardButton[inlineKeyboard.length + 1][];
System.arraycopy(inlineKeyboard, 0, n, 0, inlineKeyboard.length);
inlineKeyboard = n;
inlineKeyboard[inlineKeyboard.length - 1] = new InlineKeyboardButton[] {};
}
private void nextRowI() {
InlineKeyboardButton[][] n = new InlineKeyboardButton[inlineKeyboard.length + 1][];
System.arraycopy(inlineKeyboard, 0, n, 0, inlineKeyboard.length);
inlineKeyboard = n;
inlineKeyboard[inlineKeyboard.length - 1] = new InlineKeyboardButton[]{};
}
protected PengradKeyboardBuilder self() {
return this;
}
protected PengradKeyboardBuilder self() {
return this;
}
private void put(Action action) {
if (action.isInline()) {
put(new InlineKeyboardButton(action.getText()).callbackData(action.getCallbackData()));
} else {
put(new KeyboardButton(action.getText()).requestContact(action.isRequestContact()));
}
}
private void put(Action action) {
if (action.isInline()) {
put(new InlineKeyboardButton(action.getText())
.callbackData(action.getCallbackData())
.url(action.getUrl()));
} else {
put(new KeyboardButton(action.getText())
.requestContact(action.isRequestContact())
.requestLocation(action.isRequestLocation()));
}
}
private void put(KeyboardButton btn) {
if (keyboard == null) {
keyboard = new KeyboardButton[][] {
new KeyboardButton[] { btn }
};
} else {
KeyboardButton[] k = keyboard[keyboard.length - 1];
KeyboardButton[] n = new KeyboardButton[k.length + 1];
System.arraycopy(k, 0, n, 0, k.length);
n[k.length] = btn;
keyboard[keyboard.length - 1] = n;
}
}
private void put(KeyboardButton btn) {
if (keyboard == null) {
keyboard = new KeyboardButton[][]{
new KeyboardButton[]{btn}
};
} else {
KeyboardButton[] k = keyboard[keyboard.length - 1];
KeyboardButton[] n = new KeyboardButton[k.length + 1];
System.arraycopy(k, 0, n, 0, k.length);
n[k.length] = btn;
keyboard[keyboard.length - 1] = n;
}
}
private void put(InlineKeyboardButton btn) {
if (inlineKeyboard == null) {
inlineKeyboard = new InlineKeyboardButton[][] {
new InlineKeyboardButton[] { btn }
};
} else {
InlineKeyboardButton[] k = inlineKeyboard[inlineKeyboard.length - 1];
InlineKeyboardButton[] n = new InlineKeyboardButton[k.length + 1];
System.arraycopy(k, 0, n, 0, k.length);
n[k.length] = btn;
inlineKeyboard[inlineKeyboard.length - 1] = n;
}
private void put(InlineKeyboardButton btn) {
if (inlineKeyboard == null) {
inlineKeyboard = new InlineKeyboardButton[][]{
new InlineKeyboardButton[]{btn}
};
} else {
InlineKeyboardButton[] k = inlineKeyboard[inlineKeyboard.length - 1];
InlineKeyboardButton[] n = new InlineKeyboardButton[k.length + 1];
System.arraycopy(k, 0, n, 0, k.length);
n[k.length] = btn;
inlineKeyboard[inlineKeyboard.length - 1] = n;
}
}
}
@Override
public KeyboardBuilder newInstance() {
return new PengradKeyboardBuilder();
}
@Override
public KeyboardBuilder newInstance() {
return new PengradKeyboardBuilder();
}
}