rubenlagus / TelegramBots

Java library to create bots using Telegram Bots API
https://telegram.me/JavaBotsApi
MIT License
4.77k stars 1.22k forks source link

Error initializing application. Error!!! #319

Closed ttashmatov closed 6 years ago

ttashmatov commented 7 years ago

I need to start config bot with my application When I start my application then i have this error

Error initializing application com.google.inject.ConfigurationException: Guice configuration errors:

1) No implementation for org.telegram.telegrambots.generics.BotSession was bound. while locating org.telegram.telegrambots.generics.BotSession

I add
ApiContextInitializer.init(); in static method, but the problem did not disappear

Clevero commented 7 years ago

Where do you added ApiContextInitializer.init();?

https://github.com/rubenlagus/TelegramBots/wiki/How-To-Update#to-version-242

Duplicate of #161, #277, #167 an #234

ttashmatov commented 7 years ago
public static void main(String[] args) throws SQLException {
        ApiContextInitializer.init();
        TelegramBotsApi telegramBotsApi = new TelegramBotsApi();
......

Creating project with Gradle

charmstead commented 7 years ago

I am currently experiencing this issue too, i use springboot as well. I tried all the work around in the comments but no luck. has anyone solved this.

maistrovyi commented 7 years ago

@charmstead I can share my implementation with Spring.

charmstead commented 7 years ago

@maystrovyy please do share your implementation so that others can learn from it.

ronindev commented 6 years ago

I realized the reason for that issue. The problem is that you create Bot instance before ApiContextInitializer.init(); The INJECTOR in org.telegram.telegrambots.ApiContext register beans only once while initialization. In my case I have a configuration like this:

    @Bean
    FilaBot bot() {
        return new FilaBot() {
            @Override
            public String getBotToken() {
                return token;
            }

            @Override
            public String getBotUsername() {
                return username;
            }
        };
    }

    @Bean(initMethod = "init", destroyMethod = "dispose")
    BotService botService() {
        ApiContextInitializer.init();
        return new BotService(new TelegramBotsApi(), bot());
    }

Spring construct bot bean first, and INJECTOR initialize without any beans. And org.telegram.telegrambots.ApiContext simply do nothing instead of adding beans to internal map. The solution is calling ApiContextInitializer.init(); before all initiations (bots and api). In my case I do workaround moving bot bean definition after botService bean. Like this:

    @Bean(initMethod = "init", destroyMethod = "dispose")
    BotService botService() {
        ApiContextInitializer.init();
        return new BotService(new TelegramBotsApi(), bot());
    }

    @Bean
    FilaBot bot() {
        return new FilaBot() {
            @Override
            public String getBotToken() {
                return token;
            }

            @Override
            public String getBotUsername() {
                return username;
            }
        };
    }

Hope developers will fix that issue in a proper way

ronindev commented 6 years ago

So documentation https://github.com/rubenlagus/TelegramBots/wiki/How-To-Update#to-version-242 also have an issue: 2. At the beginning of your program (before creating your TelegramBotsApi instance, add the following line: should be

  1. At the beginning of your program (before creating your TelegramBotsApi and Bot instances, add the following line: