7 Commits

Author SHA1 Message Date
ruslan 154f6471d8 [maven-release-plugin] prepare release stbf-parent-0.0.3 2026-04-21 11:11:51 +03:00
ruslan 10b1b430e6 Log additional details for missing chat IDs and simplify close method signature. 2026-03-29 09:38:56 +03:00
ruslan fc7ba16fab Fix keyboard builder 2026-03-28 21:55:22 +03:00
ruslan 0e1b44a920 Update Telegram Bot API version and enhance message handling
- Upgrade `java-telegram-bot-api` dependency from version 7.1.1 to 9.4.1 in `pom.xml`.
- Modify `sendFile` method in `BotResponseImpl` to log the sent message ID.
- Replace deprecated `disableWebPagePreview` with `linkPreviewOptions` in `editMessage` and `edit` methods.
- Refactor `createFromScreen` and related methods in `SendMethodUtils` to handle chat ID as a long type for better type safety.
2026-03-28 18:45:17 +03:00
ruslan 6fc2748779 [maven-release-plugin] prepare for next development iteration 2026-03-03 13:28:02 +03:00
ruslan 87de3ff874 [maven-release-plugin] prepare release 0.0.2 2026-03-03 13:28:00 +03:00
ruslan c43ba2ea8b Update PengradTelegramBot to improve chat ID retrieval and upgrade Telegram API version
- Refactor chat ID extraction logic into a separate method for clarity and maintainability.
- Handle cases where chat ID may not be accessible, logging a warning when not found.
- Upgrade Telegram Bot API dependency version from 6.2.0 to 7.1.1.
- Adjust BotRequestImpl to accommodate changes in chat ID retrieval logic.
- Make botAtomicReference in App class final for better thread safety.
2026-03-03 13:26:14 +03:00
16 changed files with 110 additions and 54 deletions
+39 -2
View File
@@ -6,7 +6,7 @@
<groupId>ru.penkrat.stbf</groupId>
<artifactId>stbf-parent</artifactId>
<version>0.0.2-SNAPSHOT</version>
<version>0.0.3</version>
<name>stbf-parent</name>
<description>Simple Telegram Bot Facade</description>
@@ -14,7 +14,7 @@
<scm>
<connection>scm:git:https://git.penkrat.ru/ruslan/stbf.git</connection>
<developerConnection>scm:git:https://git.penkrat.ru/ruslan/stbf.git</developerConnection>
<tag>HEAD</tag>
<tag>stbf-parent-0.0.3</tag>
</scm>
<licenses>
@@ -65,6 +65,43 @@
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<phase>deploy</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<executions>
<execution>
<id>attach-javadocs</id>
<phase>deploy</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<!-- explicitly define maven-deploy-plugin after other to force exec order -->
<artifactId>maven-deploy-plugin</artifactId>
<executions>
<execution>
<id>deploy</id>
<phase>deploy</phase>
<goals>
<goal>deploy</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
+1 -1
View File
@@ -5,7 +5,7 @@
<parent>
<groupId>ru.penkrat.stbf</groupId>
<artifactId>stbf-parent</artifactId>
<version>0.0.2-SNAPSHOT</version>
<version>0.0.3</version>
</parent>
<artifactId>stbf-api</artifactId>
+2 -2
View File
@@ -4,7 +4,7 @@
<parent>
<artifactId>stbf-parent</artifactId>
<groupId>ru.penkrat.stbf</groupId>
<version>0.0.2-SNAPSHOT</version>
<version>0.0.3</version>
</parent>
<artifactId>stbf-common</artifactId>
@@ -14,7 +14,7 @@
<dependency>
<groupId>ru.penkrat.stbf</groupId>
<artifactId>stbf-api</artifactId>
<version>0.0.2-SNAPSHOT</version>
<version>0.0.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
+1 -1
View File
@@ -4,7 +4,7 @@
<parent>
<artifactId>stbf-parent</artifactId>
<groupId>ru.penkrat.stbf</groupId>
<version>0.0.2-SNAPSHOT</version>
<version>0.0.3</version>
</parent>
<groupId>ru.penkrat.stbf</groupId>
@@ -19,7 +19,7 @@ public class App implements Runnable {
private static final Logger log = LoggerFactory.getLogger(App.class);
private static AtomicReference<PengradTelegramBot> botAtomicReference = new AtomicReference<>();
private static final AtomicReference<PengradTelegramBot> botAtomicReference = new AtomicReference<>();
@CommandLine.Option(names = {"-f", "--file"}, description = "The file with config.")
private String flowFile = "classpath:/flow.xml";
+3 -3
View File
@@ -5,7 +5,7 @@
<parent>
<groupId>ru.penkrat.stbf</groupId>
<artifactId>stbf-parent</artifactId>
<version>0.0.2-SNAPSHOT</version>
<version>0.0.3</version>
</parent>
<artifactId>stbf-pengrad</artifactId>
@@ -16,12 +16,12 @@
<dependency>
<groupId>ru.penkrat.stbf</groupId>
<artifactId>stbf-api</artifactId>
<version>0.0.2-SNAPSHOT</version>
<version>0.0.3</version>
</dependency>
<dependency>
<groupId>com.github.pengrad</groupId>
<artifactId>java-telegram-bot-api</artifactId>
<version>6.2.0</version>
<version>9.4.1</version>
</dependency>
</dependencies>
@@ -44,18 +44,16 @@ public class BotRequestImpl implements BotRequest {
@Override
public Optional<String> getCallbackMessageText() {
return Optional.of(update)
.map(Update::callbackQuery)
.map(CallbackQuery::message)
.map(Message::text);
// unsupported ? todo: check doc
return Optional.empty();
}
@Override
public Long getChatId() {
return Optional.of(update)
.map(Update::callbackQuery)
.map(CallbackQuery::message)
.orElseGet(() -> update.message()).chat().id();
.map(CallbackQuery::maybeInaccessibleMessage)
.orElseGet(update::message).chat().id();
}
@Override
@@ -1,6 +1,8 @@
package ru.penkrat.stbf.impl.pengrad;
import com.pengrad.telegrambot.TelegramBot;
import com.pengrad.telegrambot.model.LinkPreviewOptions;
import com.pengrad.telegrambot.model.Message;
import com.pengrad.telegrambot.model.Update;
import com.pengrad.telegrambot.model.request.InlineKeyboardButton;
import com.pengrad.telegrambot.model.request.InlineKeyboardMarkup;
@@ -30,7 +32,7 @@ public class BotResponseImpl implements BotResponse {
public void send(Screen screen) {
log.trace("Send trace: \n============\n{}\n============", screen.getText().trim());
AbstractSendRequest<? extends AbstractSendRequest> sendMessage = SendMethodUtils.createFromScreen(chatId(), screen);
AbstractSendRequest<? extends AbstractSendRequest<?>> sendMessage = SendMethodUtils.createFromScreen(chatId(), screen);
if (screen.getKeyboard() instanceof KeyboardImpl) {
KeyboardImpl kk = (KeyboardImpl) screen.getKeyboard();
@@ -58,7 +60,10 @@ public class BotResponseImpl implements BotResponse {
@Override
public void sendFile(String filename, byte[] data) {
telegramBot.execute(new SendDocument(chatId(), data).fileName(filename));
Message message = telegramBot.execute(new SendDocument((long) chatId(), data)
.fileName(filename)
).message();
log.debug("Send file message = {}", message.messageId());
}
@Override
@@ -66,14 +71,14 @@ public class BotResponseImpl implements BotResponse {
public void editMessage(String text) {
telegramBot.execute(new EditMessageText(chatId(), messageId(), text)
.parseMode(ParseMode.HTML)
.disableWebPagePreview(true));
.linkPreviewOptions(new LinkPreviewOptions().isDisabled(true)));
}
@Override
public void edit(Screen screen) {
EditMessageText editMessage = new EditMessageText(chatId(), messageId(), screen.getText())
.parseMode(screen.getScreenProperties().isParseModeHtml() ? ParseMode.HTML : ParseMode.MarkdownV2)
.disableWebPagePreview(screen.getScreenProperties().isDisableWebPagePreview());
.linkPreviewOptions(new LinkPreviewOptions().isDisabled(screen.getScreenProperties().isDisableWebPagePreview()));
if (screen.getKeyboard() instanceof KeyboardImpl) {
KeyboardImpl kk = (KeyboardImpl) screen.getKeyboard();
@@ -105,14 +110,14 @@ public class BotResponseImpl implements BotResponse {
private Long chatId() {
if (update.callbackQuery() != null) {
return update.callbackQuery().message().chat().id();
return update.callbackQuery().maybeInaccessibleMessage().chat().id();
}
return update.message().chat().id();
}
private Integer messageId() {
if (update.callbackQuery() != null) {
return update.callbackQuery().message().messageId();
return update.callbackQuery().maybeInaccessibleMessage().messageId();
}
return update.message().messageId();
}
@@ -15,11 +15,11 @@ import java.util.stream.Stream;
@Value
class KeyboardImpl implements Keyboard {
private KeyboardButton[][] keyboard;
KeyboardButton[][] keyboard;
private InlineKeyboardButton[][] inlineKeyboard;
InlineKeyboardButton[][] inlineKeyboard;
private Map<Object, String> text = new HashMap<>();
Map<Object, String> text = new HashMap<>();
@Override
public String toString() {
@@ -118,9 +118,14 @@ public class PengradKeyboardBuilder implements KeyboardBuilder {
private void put(Action action) {
if (action.isInline()) {
put(new InlineKeyboardButton(action.getText())
.callbackData(action.getCallbackData())
.url(action.getUrl()));
InlineKeyboardButton inlineKeyboardButton = new InlineKeyboardButton(action.getText());
if (action.getCallbackData() != null) {
inlineKeyboardButton.setCallbackData(action.getCallbackData());
}
if (action.getUrl() != null) {
inlineKeyboardButton.url(action.getUrl());
}
put(inlineKeyboardButton);
} else {
put(new KeyboardButton(action.getText())
.requestContact(action.isRequestContact())
@@ -2,13 +2,10 @@ package ru.penkrat.stbf.impl.pengrad;
import com.pengrad.telegrambot.TelegramBot;
import com.pengrad.telegrambot.UpdatesListener;
import com.pengrad.telegrambot.model.CallbackQuery;
import com.pengrad.telegrambot.model.Update;
import lombok.extern.slf4j.Slf4j;
import ru.penkrat.stbf.api.CommandChain;
import java.util.Optional;
@Slf4j
public class PengradTelegramBot extends TelegramBot implements AutoCloseable {
@@ -17,11 +14,12 @@ public class PengradTelegramBot extends TelegramBot implements AutoCloseable {
this.setUpdatesListener(updates -> {
for (Update update : updates) {
try {
final Long chatId = Optional.of(update)
.map(Update::callbackQuery)
.map(CallbackQuery::message)
.orElseGet(() -> update.message()).chat().id();
final Long chatId = findChatId(update);
if (chatId == null) {
log.warn("Chat not found, Update [id={}]", update.updateId());
log.info("Details: {}", update);
continue;
}
log.debug("New message in chat {}", chatId);
commandChain.processCommand(
@@ -36,9 +34,20 @@ public class PengradTelegramBot extends TelegramBot implements AutoCloseable {
}
@Override
public void close() throws Exception {
public void close() {
removeGetUpdatesListener();
log.debug("Bot closed.");
}
private Long findChatId(Update update) {
if (update.callbackQuery() != null && update.callbackQuery().maybeInaccessibleMessage() != null) {
return update.callbackQuery().maybeInaccessibleMessage().chat().id();
}
if (update.message() != null) {
return update.message().chat().id();
}
return null;
}
}
@@ -1,5 +1,6 @@
package ru.penkrat.stbf.impl.pengrad;
import com.pengrad.telegrambot.model.LinkPreviewOptions;
import com.pengrad.telegrambot.model.Message;
import com.pengrad.telegrambot.model.request.ParseMode;
import com.pengrad.telegrambot.request.AbstractSendRequest;
@@ -23,7 +24,8 @@ import java.util.function.Consumer;
@UtilityClass
class SendMethodUtils {
public AbstractSendRequest<? extends AbstractSendRequest> createFromScreen(@NonNull Object chatId, @NonNull Screen screen) {
public AbstractSendRequest<? extends AbstractSendRequest<?>> createFromScreen(@NonNull Object chatIdObj, @NonNull Screen screen) {
long chatId = chatIdObj instanceof Long ? (Long) chatIdObj : 0;
if (isMedia(screen)) {
final Media media = screen.getMedia();
switch (media.getMediaType()) {
@@ -77,7 +79,7 @@ class SendMethodUtils {
return new SendMessage(chatId, screen.getText().trim())
.parseMode(screen.getScreenProperties().isParseModeHtml() ? ParseMode.HTML : ParseMode.MarkdownV2)
.disableWebPagePreview(screen.getScreenProperties().isDisableWebPagePreview())
.linkPreviewOptions(new LinkPreviewOptions().isDisabled(screen.getScreenProperties().isDisableWebPagePreview()))
.disableNotification(screen.getScreenProperties().isDisableNotification());
}
@@ -102,7 +104,7 @@ class SendMethodUtils {
}
@NotNull
private SendVoice createSendVoice(@NotNull Object chatId, Media media) {
private SendVoice createSendVoice(long chatId, Media media) {
if (media.getData() != null) {
return new SendVoice(chatId, media.getData().get());
}
@@ -113,7 +115,7 @@ class SendMethodUtils {
}
@NotNull
private SendVideo createSendVideo(@NotNull Object chatId, Media media) {
private SendVideo createSendVideo(long chatId, Media media) {
if (media.getData() != null) {
return new SendVideo(chatId, media.getData().get());
}
@@ -124,7 +126,7 @@ class SendMethodUtils {
}
@NotNull
private SendAudio createSendAudio(@NotNull Object chatId, Media media) {
private SendAudio createSendAudio(long chatId, Media media) {
if (media.getData() != null) {
return new SendAudio(chatId, media.getData().get());
}
@@ -135,7 +137,7 @@ class SendMethodUtils {
}
@NotNull
private SendAnimation createSendAnimation(@NotNull Object chatId, Media media) {
private SendAnimation createSendAnimation(long chatId, Media media) {
if (media.getData() != null) {
return new SendAnimation(chatId, media.getData().get());
}
@@ -146,7 +148,7 @@ class SendMethodUtils {
}
@NotNull
private SendPhoto createSendPhoto(@NotNull Object chatId, Media media) {
private SendPhoto createSendPhoto(long chatId, Media media) {
if (media.getData() != null) {
return new SendPhoto(chatId, media.getData().get());
}
+2 -2
View File
@@ -4,7 +4,7 @@
<parent>
<artifactId>stbf-parent</artifactId>
<groupId>ru.penkrat.stbf</groupId>
<version>0.0.2-SNAPSHOT</version>
<version>0.0.3</version>
</parent>
<artifactId>stbf-rubenlagus</artifactId>
@@ -15,7 +15,7 @@
<dependency>
<groupId>ru.penkrat.stbf</groupId>
<artifactId>stbf-api</artifactId>
<version>0.0.2-SNAPSHOT</version>
<version>0.0.3</version>
</dependency>
<dependency>
<groupId>org.telegram</groupId>
+1 -1
View File
@@ -4,7 +4,7 @@
<parent>
<artifactId>stbf-parent</artifactId>
<groupId>ru.penkrat.stbf</groupId>
<version>0.0.2-SNAPSHOT</version>
<version>0.0.3</version>
</parent>
<artifactId>stbf-templates</artifactId>
<properties>
@@ -83,7 +83,7 @@ class FlowScreenResolverDelegate implements ScreenResolver {
if (context instanceof KeyboardProvider) {
final Keyboard keyboard = ((KeyboardProvider) context).getKeyboard();
if (keyboard == null) {
log.warn("Method 'getKeyboard' returns NULL value!", wrapper.getFactoryMethod());
log.warn("Method 'getKeyboard' {} returns NULL value!", wrapper.getFactoryMethod());
}
return keyboard;
}
@@ -106,12 +106,12 @@ class FlowScreenResolverDelegate implements ScreenResolver {
for (ButtonsRow row : wrapper.getRows()) {
List<Action> buttons = row.getButtons().stream()
.filter(btn -> checkIfCondition(context, btn.getIfCondition()))
.map(btn -> getAction(btn))
.map(this::getAction)
.collect(Collectors.toList());
log.info("Keyboard: {}", buttons);
builder.row(buttons.toArray(new Action[buttons.size()]));
builder.row(buttons.toArray(new Action[0]));
}
keyboard = builder.build();
+3 -3
View File
@@ -4,7 +4,7 @@
<parent>
<artifactId>stbf-parent</artifactId>
<groupId>ru.penkrat.stbf</groupId>
<version>0.0.2-SNAPSHOT</version>
<version>0.0.3</version>
</parent>
<artifactId>stbf-test</artifactId>
@@ -14,12 +14,12 @@
<dependency>
<groupId>ru.penkrat.stbf</groupId>
<artifactId>stbf-api</artifactId>
<version>0.0.2-SNAPSHOT</version>
<version>0.0.3</version>
</dependency>
<dependency>
<groupId>ru.penkrat.stbf</groupId>
<artifactId>stbf-common</artifactId>
<version>0.0.2-SNAPSHOT</version>
<version>0.0.3</version>
<scope>test</scope>
</dependency>
<dependency>