initial commit
This commit is contained in:
41
stbf-rubenlagus/pom.xml
Normal file
41
stbf-rubenlagus/pom.xml
Normal file
@@ -0,0 +1,41 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<artifactId>stbf-parent</artifactId>
|
||||
<groupId>ru.penkrat.stbf</groupId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>stbf-rubenlagus</artifactId>
|
||||
<name>stbf-rubenlagus</name>
|
||||
<description>Simple Telegram Bot Facade with https://github.com/rubenlagus/TelegramBots implementation</description>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>ru.penkrat.stbf</groupId>
|
||||
<artifactId>stbf-api</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.telegram</groupId>
|
||||
<artifactId>telegrambots</artifactId>
|
||||
<version>5.3.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>${java.version}</source>
|
||||
<target>${java.version}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,58 @@
|
||||
package ru.penkrat.stbf.impl.rubenlagus;
|
||||
|
||||
import static lombok.AccessLevel.PROTECTED;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.telegram.telegrambots.meta.api.objects.CallbackQuery;
|
||||
import org.telegram.telegrambots.meta.api.objects.Contact;
|
||||
import org.telegram.telegrambots.meta.api.objects.Message;
|
||||
import org.telegram.telegrambots.meta.api.objects.Update;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import ru.penkrat.stbf.api.BotRequest;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
class BotRequestImpl implements BotRequest {
|
||||
|
||||
@Getter(PROTECTED)
|
||||
private final Update update;
|
||||
|
||||
@Override
|
||||
public Optional<String> getMessageText() {
|
||||
if (update.hasMessage()) {
|
||||
return Optional.ofNullable(update.getMessage().getText());
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<String> getPhoneNumber() {
|
||||
return Optional.of(update)
|
||||
.map(Update::getMessage)
|
||||
.map(Message::getContact)
|
||||
.map(Contact::getPhoneNumber);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<String> getCallbackData() {
|
||||
return Optional.of(update)
|
||||
.map(Update::getCallbackQuery)
|
||||
.map(CallbackQuery::getData);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<String> getCallbackMessageText() {
|
||||
return Optional.of(update)
|
||||
.map(Update::getCallbackQuery)
|
||||
.map(CallbackQuery::getMessage)
|
||||
.map(Message::getText);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long getChatId() {
|
||||
return update.getMessage().getChatId();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
package ru.penkrat.stbf.impl.rubenlagus;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
|
||||
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
||||
import org.telegram.telegrambots.meta.api.methods.updatingmessages.DeleteMessage;
|
||||
import org.telegram.telegrambots.meta.api.methods.updatingmessages.EditMessageText;
|
||||
import org.telegram.telegrambots.meta.api.objects.Update;
|
||||
import org.telegram.telegrambots.meta.api.objects.replykeyboard.InlineKeyboardMarkup;
|
||||
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.KeyboardRow;
|
||||
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import ru.penkrat.stbf.api.BotResponse;
|
||||
import ru.penkrat.stbf.api.Screen;
|
||||
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
class BotResponseImpl implements BotResponse {
|
||||
|
||||
private final Update update;
|
||||
|
||||
private final TelegramLongPollingBot bot;
|
||||
|
||||
@Override
|
||||
public void send(Screen screen) {
|
||||
SendMessage send = new SendMessage(chatId(), screen.getText());
|
||||
|
||||
if (screen.getKeyboard() instanceof KeyboardImpl) {
|
||||
KeyboardImpl kk = (KeyboardImpl) screen.getKeyboard();
|
||||
List<KeyboardRow> keyboard = kk.getKeyboard();
|
||||
List<List<InlineKeyboardButton>> inlineKeyboard = kk.getInlineKeyboard();
|
||||
|
||||
if (inlineKeyboard != null && !inlineKeyboard.isEmpty()) {
|
||||
send.setReplyMarkup(new InlineKeyboardMarkup(inlineKeyboard));
|
||||
} else if (keyboard != null && !keyboard.isEmpty()) {
|
||||
send.setReplyMarkup(new ReplyKeyboardMarkup(keyboard));
|
||||
} else {
|
||||
log.debug("No keyboard");
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
bot.execute(send);
|
||||
} catch (TelegramApiException e) {
|
||||
log.error("Send error", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendFile(String filename, byte[] data) {
|
||||
throw new IllegalStateException("Not inplemented");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void editMessage(String text) {
|
||||
EditMessageText send = new EditMessageText(text);
|
||||
send.setChatId(chatId());
|
||||
send.setMessageId(messageId());
|
||||
if (update.hasCallbackQuery()) {
|
||||
send.setInlineMessageId(update.getCallbackQuery().getInlineMessageId());
|
||||
}
|
||||
try {
|
||||
bot.execute(send);
|
||||
} catch (TelegramApiException e) {
|
||||
log.error("Send error", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteMessage() {
|
||||
DeleteMessage send = new DeleteMessage(chatId(), messageId());
|
||||
try {
|
||||
bot.execute(send);
|
||||
} catch (TelegramApiException e) {
|
||||
log.error("Send error", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void edit(Screen screen) {
|
||||
EditMessageText send = new EditMessageText(screen.getText());
|
||||
send.setChatId(chatId());
|
||||
send.setMessageId(messageId());
|
||||
if (update.hasCallbackQuery()) {
|
||||
send.setInlineMessageId(update.getCallbackQuery().getInlineMessageId());
|
||||
}
|
||||
try {
|
||||
bot.execute(send);
|
||||
} catch (TelegramApiException e) {
|
||||
log.error("Send error", e);
|
||||
}
|
||||
}
|
||||
|
||||
private String chatId() {
|
||||
if (update.hasCallbackQuery()) {
|
||||
return Long.toString(update.getCallbackQuery().getMessage().getChatId());
|
||||
}
|
||||
return Long.toString(update.getMessage().getChatId());
|
||||
}
|
||||
|
||||
private Integer messageId() {
|
||||
if (update.hasCallbackQuery()) {
|
||||
return Integer.parseInt(update.getCallbackQuery().getInlineMessageId());
|
||||
}
|
||||
return update.getMessage().getMessageId();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package ru.penkrat.stbf.impl.rubenlagus;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.InlineKeyboardButton;
|
||||
import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.KeyboardRow;
|
||||
|
||||
import lombok.Value;
|
||||
import ru.penkrat.stbf.api.Keyboard;
|
||||
|
||||
@Value
|
||||
class KeyboardImpl implements Keyboard {
|
||||
|
||||
private List<KeyboardRow> keyboard;
|
||||
|
||||
private List<List<InlineKeyboardButton>> inlineKeyboard;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
package ru.penkrat.stbf.impl.rubenlagus;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
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 ru.penkrat.stbf.api.Action;
|
||||
import ru.penkrat.stbf.api.Keyboard;
|
||||
import ru.penkrat.stbf.api.KeyboardBuilder;
|
||||
|
||||
public class RubenlagusKeyboardBuilder implements KeyboardBuilder {
|
||||
|
||||
private List<KeyboardRow> keyboard;
|
||||
|
||||
private List<List<InlineKeyboardButton>> inlineKeyboard;
|
||||
|
||||
public static Keyboard singleKey(Action action) {
|
||||
return KeyboardBuilder.newKeyboard().add(action).build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Keyboard build() {
|
||||
return new KeyboardImpl(keyboard, inlineKeyboard);
|
||||
}
|
||||
|
||||
public KeyboardBuilder addGetContact(String text) {
|
||||
KeyboardButton keyboardButton = new KeyboardButton(text);
|
||||
keyboardButton.setRequestContact(true);
|
||||
put(keyboardButton);
|
||||
return self();
|
||||
}
|
||||
|
||||
public KeyboardBuilder add(String text) {
|
||||
put(new KeyboardButton(text));
|
||||
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();
|
||||
}
|
||||
|
||||
@Override
|
||||
public KeyboardBuilder row(Action... buttons) {
|
||||
for (int i = 0; i < buttons.length; i++) {
|
||||
put(buttons[i]);
|
||||
}
|
||||
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 RubenlagusKeyboardBuilder add(String text, String data) {
|
||||
InlineKeyboardButton inlineKeyboardButton = new InlineKeyboardButton(text);
|
||||
inlineKeyboardButton.setCallbackData(data);
|
||||
put(inlineKeyboardButton);
|
||||
return self();
|
||||
}
|
||||
|
||||
public KeyboardBuilder addNl(String text, String data) {
|
||||
add(text, data);
|
||||
nextRowI();
|
||||
return self();
|
||||
}
|
||||
|
||||
private void nextRow() {
|
||||
KeyboardRow row = new KeyboardRow();
|
||||
keyboard.add(row);
|
||||
}
|
||||
|
||||
private void nextRowI() {
|
||||
List<InlineKeyboardButton> row = new ArrayList<>();
|
||||
inlineKeyboard.add(row);
|
||||
}
|
||||
|
||||
protected RubenlagusKeyboardBuilder self() {
|
||||
return this;
|
||||
}
|
||||
|
||||
private void put(Action action) {
|
||||
if (action.isInline()) {
|
||||
InlineKeyboardButton inlineKeyboardButton = new InlineKeyboardButton(action.getText());
|
||||
inlineKeyboardButton.setCallbackData(action.getCallbackData());
|
||||
put(inlineKeyboardButton);
|
||||
} else {
|
||||
KeyboardButton keyboardButton = new KeyboardButton(action.getText());
|
||||
keyboardButton.setRequestContact(action.isRequestContact());
|
||||
put(keyboardButton);
|
||||
}
|
||||
}
|
||||
|
||||
private void put(KeyboardButton btn) {
|
||||
if (keyboard == null) {
|
||||
keyboard = new ArrayList<KeyboardRow>();
|
||||
KeyboardRow row = new KeyboardRow();
|
||||
keyboard.add(row);
|
||||
row.add(btn);
|
||||
} else {
|
||||
keyboard.get(keyboard.size() - 1).add(btn);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void put(InlineKeyboardButton btn) {
|
||||
if (inlineKeyboard == null) {
|
||||
inlineKeyboard = new ArrayList<List<InlineKeyboardButton>>();
|
||||
List<InlineKeyboardButton> row = new ArrayList<>();
|
||||
inlineKeyboard.add(row);
|
||||
row.add(btn);
|
||||
} else {
|
||||
inlineKeyboard.get(inlineKeyboard.size() - 1).add(btn);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public KeyboardBuilder newInstance() {
|
||||
return new RubenlagusKeyboardBuilder();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package ru.penkrat.stbf.impl.rubenlagus;
|
||||
|
||||
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
|
||||
import org.telegram.telegrambots.meta.TelegramBotsApi;
|
||||
import org.telegram.telegrambots.meta.api.objects.Update;
|
||||
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
|
||||
import org.telegram.telegrambots.updatesreceivers.DefaultBotSession;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import ru.penkrat.stbf.api.BotCommandChain;
|
||||
|
||||
@Slf4j
|
||||
public class RubenlagusTelegramBot extends TelegramLongPollingBot {
|
||||
|
||||
private final String botUsername;
|
||||
private final String botToken;
|
||||
private final BotCommandChain commandChain;
|
||||
|
||||
public RubenlagusTelegramBot(String botUsername, String botToken, BotCommandChain botCommandChain) {
|
||||
this.botUsername = botUsername;
|
||||
this.botToken = botToken;
|
||||
this.commandChain = botCommandChain;
|
||||
|
||||
TelegramBotsApi telegramBotsApi;
|
||||
try {
|
||||
telegramBotsApi = new TelegramBotsApi(DefaultBotSession.class);
|
||||
telegramBotsApi.registerBot(this);
|
||||
} catch (TelegramApiException e) {
|
||||
log.error("Error", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onUpdateReceived(Update update) {
|
||||
try {
|
||||
commandChain.processCommand(
|
||||
new BotRequestImpl(update),
|
||||
new BotResponseImpl(update, this));
|
||||
} catch (Exception e) {
|
||||
log.error("Bot Error:", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getBotUsername() {
|
||||
return botUsername;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getBotToken() {
|
||||
return botToken;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
ru.penkrat.stbf.impl.rubenlagus.RubenlagusKeyboardBuilder
|
||||
Reference in New Issue
Block a user