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

Resolve bot username automatically

This commit is contained in:
aNNiMON 2024-09-04 19:33:55 +03:00
parent 5b3e9e31fb
commit f915f79137
5 changed files with 19 additions and 10 deletions

View File

@ -42,7 +42,6 @@ Note: FFmpeg binary might be installed with limited number of filters and codecs
docker build --tag 'ffmpegbot' .
docker run -d -t -i \
-e BOT_TOKEN='...' \
-e BOT_USERNAME='...' \
-e APP_ID='...' \
-e APP_HASH='...' \
-e SUPERUSERS='12345' \
@ -53,7 +52,6 @@ docker run -d -t -i \
#### Environment variables
- `BOT_TOKEN` — Telegram bot token
- `BOT_USERNAME` — Telegram bot username
- `APP_ID` — Telegram API app_id (see https://core.telegram.org/api/obtaining_api_id)
- `APP_HASH` — Telegram API app_hash
- `SUPERUSERS` — Comma-separated list of superusers. Superuser can execute /run command

View File

@ -1,6 +1,5 @@
# Telegram bot token and bot username
# Telegram bot token
botToken: 1234567890:AAAABBBBCCCCDDDDEEEEFF-GGGGHHHHIIII
botUsername: yourbotname
# Telegram API app_id / app_hash (see https://core.telegram.org/api/obtaining_api_id)
appId: 12345
appHash: abc123def456

View File

@ -1,12 +1,14 @@
package com.annimon.ffmpegbot;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import java.util.Arrays;
import java.util.Collections;
import java.util.Set;
import java.util.stream.Collectors;
@JsonIgnoreProperties(ignoreUnknown = true)
public record BotConfig(String appId, String appHash,
String botToken, String botUsername,
String botToken,
String downloaderScript,
String superUsers, String allowedUsers) {

View File

@ -6,6 +6,10 @@ import com.annimon.tgbotsmodule.Runner;
import com.annimon.tgbotsmodule.beans.Config;
import com.annimon.tgbotsmodule.services.YamlConfigLoaderService;
import org.jetbrains.annotations.NotNull;
import org.telegram.telegrambots.client.okhttp.OkHttpTelegramClient;
import org.telegram.telegrambots.meta.api.methods.GetMe;
import org.telegram.telegrambots.meta.api.objects.User;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import java.util.List;
@ -19,7 +23,13 @@ public class Main implements BotModule {
public @NotNull BotHandler botHandler(@NotNull Config config) {
final var configLoader = new YamlConfigLoaderService();
final var configFile = configLoader.configFile("ffmpegbot", config.getProfile());
final var wordlyConfig = configLoader.loadFile(configFile, BotConfig.class);
return new MainBotHandler(wordlyConfig);
final var botConfig = configLoader.loadFile(configFile, BotConfig.class);
try {
final var telegramClient = new OkHttpTelegramClient(botConfig.botToken());
final User bot = telegramClient.execute(new GetMe());
return new MainBotHandler(botConfig, bot.getUserName());
} catch (TelegramApiException e) {
throw new IllegalStateException("Unable to get information about bot", e);
}
}
}

View File

@ -24,17 +24,17 @@ public class MainBotHandler extends BotHandler {
private final CommandRegistry<For> commands;
private final MediaProcessingBundle mediaProcessingBundle;
public MainBotHandler(BotConfig botConfig) {
public MainBotHandler(BotConfig botConfig, String botUserName) {
super(BotModuleOptions.createDefault(botConfig.botToken()));
permissions = new Permissions(botConfig.superUserIds(), botConfig.allowedUserIds());
commands = new CommandRegistry<>(botConfig.botUsername(), permissions);
commands = new CommandRegistry<>(botUserName, permissions);
final var sessions = new Sessions();
final var fallbackFileDownloader = new FallbackFileDownloader(
new TelegramFileDownloader(),
new TelegramClientFileDownloader(
botConfig.downloaderScript(),
botConfig.appId(), botConfig.appHash(),
botConfig.botToken(), botConfig.botUsername())
botConfig.botToken(), botUserName)
);
mediaProcessingBundle = new MediaProcessingBundle(sessions, fallbackFileDownloader);
commands.registerBundle(mediaProcessingBundle);