1
0
mirror of https://github.com/aNNiMON/ffmpegbot synced 2024-09-19 22:54:20 +03:00

Migrate to API 7.6

This commit is contained in:
aNNiMON 2024-07-07 20:28:25 +03:00
parent 0e17d85180
commit cb01dce7ab
7 changed files with 29 additions and 32 deletions

View File

@ -21,7 +21,7 @@ repositories {
}
dependencies {
implementation 'com.annimon:tgbots-module:7.1.0'
implementation 'com.annimon:tgbots-module:7.6.0'
implementation 'org.slf4j:slf4j-simple:2.0.13'
}

View File

@ -11,22 +11,21 @@ import com.annimon.ffmpegbot.file.TelegramClientFileDownloader;
import com.annimon.ffmpegbot.file.TelegramFileDownloader;
import com.annimon.ffmpegbot.session.Sessions;
import com.annimon.tgbotsmodule.BotHandler;
import com.annimon.tgbotsmodule.BotModuleOptions;
import com.annimon.tgbotsmodule.commands.CommandRegistry;
import com.annimon.tgbotsmodule.commands.authority.For;
import org.jetbrains.annotations.NotNull;
import org.telegram.telegrambots.meta.api.methods.BotApiMethod;
import org.telegram.telegrambots.meta.api.methods.botapimethods.BotApiMethod;
import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
public class MainBotHandler extends BotHandler {
private final BotConfig botConfig;
private final Permissions permissions;
private final CommandRegistry<For> commands;
private final MediaProcessingBundle mediaProcessingBundle;
public MainBotHandler(BotConfig botConfig) {
super(botConfig.botToken());
this.botConfig = botConfig;
super(BotModuleOptions.createDefault(botConfig.botToken()));
permissions = new Permissions(botConfig.superUsers(), botConfig.allowedUsers());
commands = new CommandRegistry<>(botConfig.botUsername(), permissions);
final var sessions = new Sessions();
@ -57,11 +56,6 @@ public class MainBotHandler extends BotHandler {
return null;
}
@Override
public String getBotUsername() {
return botConfig.botUsername();
}
@Override
public void handleTelegramApiException(TelegramApiException ex) {
throw new TelegramRuntimeException(ex);

View File

@ -17,7 +17,7 @@ import com.annimon.tgbotsmodule.commands.authority.For;
import com.annimon.tgbotsmodule.commands.context.CallbackQueryContext;
import com.annimon.tgbotsmodule.services.CommonAbsSender;
import org.jetbrains.annotations.NotNull;
import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.api.objects.message.Message;
import java.util.concurrent.CompletableFuture;
import java.util.function.BiConsumer;

View File

@ -6,10 +6,10 @@ import com.annimon.ffmpegbot.session.MediaSession;
import com.annimon.ffmpegbot.session.YtDlpSession;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.InlineKeyboardMarkup;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.InlineKeyboardButton;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.InlineKeyboardRow;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
@ -18,6 +18,8 @@ import static com.annimon.ffmpegbot.commands.ffmpeg.CallbackQueryCommands.*;
public class MediaProcessingKeyboard {
private static final int BUTTON_COLUMNS = 2;
private MediaProcessingKeyboard() { }
public static InlineKeyboardMarkup createKeyboard(MediaSession session) {
final var selectedParam = session.getSelectedParam();
if (selectedParam != null) {
@ -28,10 +30,10 @@ public class MediaProcessingKeyboard {
}
private static InlineKeyboardMarkup createParamsListKeyboard(Parameters params) {
final var keyboard = new ArrayList<List<InlineKeyboardButton>>();
final var keyboard = new ArrayList<InlineKeyboardRow>();
final var it = params.iterator();
while (it.hasNext()) {
final var row = new ArrayList<InlineKeyboardButton>();
final var row = new InlineKeyboardRow();
for (int i = 0; i < BUTTON_COLUMNS; i++) {
if (it.hasNext()) {
final var param = it.next();
@ -41,18 +43,18 @@ public class MediaProcessingKeyboard {
}
keyboard.add(row);
}
keyboard.add(List.of(inlineKeyboardButton("Process", callbackData(PROCESS))));
keyboard.add(new InlineKeyboardRow(inlineKeyboardButton("Process", callbackData(PROCESS))));
return new InlineKeyboardMarkup(keyboard);
}
private static InlineKeyboardMarkup createParamKeyboard(Parameter<?> param) {
final var keyboard = new ArrayList<List<InlineKeyboardButton>>();
final var keyboard = new ArrayList<InlineKeyboardRow>();
final String paramId = param.getId();
final int maxSize = param.getPossibleValuesSize();
int index = 0;
final int columnsCount = param.defaultColumnsCount();
while (index < maxSize) {
final var row = new ArrayList<InlineKeyboardButton>();
final var row = new InlineKeyboardRow();
for (int i = 0; i < columnsCount; i++) {
if (index < maxSize) {
String value = param.describeValueByIndex(index);
@ -62,22 +64,21 @@ public class MediaProcessingKeyboard {
}
keyboard.add(row);
}
keyboard.add(List.of(inlineKeyboardButton("Back", callbackData(PARAMETER))));
keyboard.add(new InlineKeyboardRow(inlineKeyboardButton("Back", callbackData(PARAMETER))));
return new InlineKeyboardMarkup(keyboard);
}
public static InlineKeyboardMarkup createKeyboard(YtDlpSession session) {
final var keyboard = new ArrayList<List<InlineKeyboardButton>>();
final var keyboard = new ArrayList<InlineKeyboardRow>();
if (!session.hasAdditionalInfo()) {
keyboard.add(List.of(inlineKeyboardButton("Get info", callbackData(YTDLP_INFO))));
keyboard.add(new InlineKeyboardRow(inlineKeyboardButton("Get info", callbackData(YTDLP_INFO))));
}
keyboard.add(List.of(inlineKeyboardButton("Start", callbackData(YTDLP_START))));
keyboard.add(new InlineKeyboardRow(inlineKeyboardButton("Start", callbackData(YTDLP_START))));
return new InlineKeyboardMarkup(keyboard);
}
private static InlineKeyboardButton inlineKeyboardButton(String text, String callbackData) {
final var button = new InlineKeyboardButton();
button.setText(text);
final var button = new InlineKeyboardButton(text);
button.setCallbackData(callbackData);
return button;
}

View File

@ -16,7 +16,9 @@ public class TelegramFileDownloader implements FileDownloader {
try {
final var tgFile = Methods.getFile(fileId).call(sender);
final var extension = FilenameUtils.getExtension(tgFile.getFilePath());
return sender.downloadFile(tgFile, File.createTempFile("tmp", "file." + extension));
File defaultFile = sender.downloadFile(tgFile);
File dest = File.createTempFile("tmp", "file." + extension);
return defaultFile.renameTo(dest) ? dest : defaultFile;
} catch (IOException | TelegramApiException | TelegramRuntimeException e) {
throw new FileDownloadException(e.getMessage(), e);
}

View File

@ -5,8 +5,6 @@ import com.annimon.ffmpegbot.commands.ffmpeg.Visitor;
import java.util.List;
import java.util.Objects;
import static com.google.common.base.Preconditions.checkArgument;
public abstract class Parameter<T> {
protected final String id;
protected final String displayName;
@ -20,8 +18,10 @@ public abstract class Parameter<T> {
this.possibleValues = values;
this.value = value;
this.enabled = true;
checkArgument(!values.isEmpty(), "possible values cannot be empty");
checkArgument(values.contains(value), "possible values must contain a value");
if (values.isEmpty())
throw new IllegalArgumentException("Possible values cannot be empty");
if (!values.contains(value))
throw new IllegalArgumentException("Possible values " + values + " must contain " + value);
}
public String getId() {

View File

@ -5,7 +5,7 @@ import com.annimon.tgbotsmodule.api.methods.interfaces.MediaMessageMethod;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.telegram.telegrambots.meta.api.methods.ActionType;
import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.api.objects.message.Message;
public class Resolver {
public static @Nullable FileInfo resolveFileInfo(@NotNull Message message) {
@ -65,9 +65,9 @@ public class Resolver {
public static ActionType resolveAction(@NotNull FileType fileType) {
return switch (fileType) {
case VIDEO -> ActionType.UPLOADVIDEO;
case VIDEO_NOTE -> ActionType.UPLOADVIDEONOTE;
case VOICE -> ActionType.UPLOADVOICE;
case VIDEO -> ActionType.UPLOAD_VIDEO;
case VIDEO_NOTE -> ActionType.UPLOAD_VIDEO_NOTE;
case VOICE -> ActionType.UPLOAD_VOICE;
default -> ActionType.TYPING;
};
}