tdlight-team / tdlight-java

Complete Bot and Userbot Telegram library based on TDLib
https://t.me/TDLight
GNU Lesser General Public License v3.0
260 stars 43 forks source link

Integrate User Bot in Spring Boot application with @Scheduled functions #204

Closed Aleksandr505 closed 8 months ago

Aleksandr505 commented 8 months ago

Hello, I try to configure user bot in my spring app

Bot is working great, but functions annotated as @Scheduled doesn't start. It looks like user bot is blocking this functional.

Configuration bot class:

@Configuration
public class TelegramUserBotConfig {

    @Bean
    @SneakyThrows
    public UserBot userBot(
            @Value("${userbot.api-id}") Integer apiId,
            @Value("${userbot.api-hash}") String apiHash,
            @Value("${userbot.phone-number}") String phoneNumber,
            @Value("${userbot.admin-id}") Long adminId
    ) {
        Init.init();
        Log.setLogMessageHandler(1, new Slf4JLogMessageHandler());

        try (SimpleTelegramClientFactory clientFactory = new SimpleTelegramClientFactory()) {
            APIToken apiToken = new APIToken(apiId, apiHash);

            TDLibSettings settings = TDLibSettings.create(apiToken);

            Path sessionPath = Paths.get("example-tdlight-session");
            settings.setDatabaseDirectoryPath(sessionPath.resolve("data"));
            settings.setDownloadedFilesDirectoryPath(sessionPath.resolve("downloads"));

            SimpleTelegramClientBuilder clientBuilder = clientFactory.builder(settings);

            SimpleAuthenticationSupplier<?> authenticationData = AuthenticationSupplier.user(phoneNumber);

            return new UserBot(clientBuilder, authenticationData, adminId);
        }
    }
}

UserBot class looks like ExampleApp class here: Example.java

This is configuration class for scheduling:

@ConditionalOnProperty(
        value = "app.scheduling.enable", havingValue = "true", matchIfMissing = true
)
@Configuration
@EnableScheduling
public class SchedulingConfiguration {
}

Scheduled functions which doesn't work:

@Scheduled(initialDelayString = "PT5S", fixedDelayString = "PT10M")
public void scheduled() {
    // some scheduled code here
}
cavallium commented 8 months ago

This issue is not related to tdlight, sorry