Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No implementation for org.telegram.telegrambots.generics.BotSession was bound #161

Closed
mentiflectax opened this issue Nov 17, 2016 · 19 comments

Comments

@mentiflectax
Copy link

How can I fix the following problem?

Exception in thread "main" 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

1 error
	at com.google.inject.internal.InjectorImpl.getProvider(InjectorImpl.java:1045)
	at com.google.inject.internal.InjectorImpl.getProvider(InjectorImpl.java:1004)
	at com.google.inject.internal.InjectorImpl.getInstance(InjectorImpl.java:1054)
	at org.telegram.telegrambots.ApiContext.getInstance(ApiContext.java:25)
	at org.telegram.telegrambots.TelegramBotsApi.registerBot(TelegramBotsApi.java:83)
@rubenlagus
Copy link
Owner

Please follow the steps as explained: https://github.com/rubenlagus/TelegramBots/wiki/How-To-Update#to-version-242

@mentiflectax
Copy link
Author

Thanks!

@segator
Copy link

segator commented Dec 4, 2016

same problem, How to update doesn't work in my case
I'm using spring boot and Maybe is causing conflicts.

@SpringBootApplication
public class AutoDownloaderApplication implements CommandLineRunner {

    @Autowired
    private MovieGrabber movieGrabber;

    @Override
    public void run(String... args) throws Exception {
        ApiContextInitializer.init();
        TelegramBotsApi botsApi = new TelegramBotsApi();
        botsApi.registerBot(movieGrabber);

    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(AutoDownloaderApplication.class, args);
    }
}

@drunckoder
Copy link

Try moving ApiContextInitializer.init(); to a static {} block. Or put it somewhere to run earlier.

@drunckoder drunckoder mentioned this issue Dec 4, 2016
@segator
Copy link

segator commented Dec 4, 2016

Thank you, it works :)

@sslavian812
Copy link

@drunckoder can you please explain, why ApiContextInitializer.init(); should be moved to static block?
I used the following code in PostConstruct-annotated method of my bot-Bean class in SpringBoot.

ApiContextInitializer.init();
TelegramBotsApi botsApi = new TelegramBotsApi();
botsApi.registerBot(this);

I got the same error and your advice did help me too. Why?)

@v1as
Copy link

v1as commented Aug 2, 2017

@sslavian812 This is because it is necessary to init ApiContextInitializer before constructor of your bot. In your case you calling it in PostConstruct block, which executing after constructor executing.

@charmstead
Copy link

I solved this by removing the annotation @component or @service from myBots class

@lawrence615
Copy link

@charmstead I also solved the same problem by removing the annotations; but what if you would want to autowire something like a repository in myBots class. How would you go about it?

@the-elf
Copy link

the-elf commented Mar 4, 2018

@charmstead I solved this problem by putting ApiContextInitializer.init() before SpringApplication.run

@IgorVolg1n
Copy link

@charmstead any idea, why does it work?
and are there some hidden pitfalls in this solution?
thanks in advance

@pengchenxue
Copy link

@log4j2
@service
public class TelegramBotService extends TelegramLongPollingBot {
@Autowired
private TelegrambotConfig telegrambotConfig;
@Autowired
private TelegramInvoker telegramInvoker;

@PostConstruct
public void registerBot(){
    TelegramBotsApi botsApi = new TelegramBotsApi();
    try {
        botsApi.registerBot(this);
        log.info("==================================TelegramBotService.afterPropertiesSet:registerBot finish");
    } catch (TelegramApiException e) {
        e.printStackTrace();
    }
}


@Override
public void onUpdateReceived(Update update) {
    
    if (update.hasMessage() && update.getMessage().hasText()) {
        telegramInvoker.action(update);
    }
}

@Override
public String getBotUsername() {
    return telegrambotConfig.getName();
}

@Override
public String getBotToken() {
    return telegrambotConfig.getToken();
}

}

@SpringCloudApplication
public class Application {

public static void main(String[] args) {
    ApiContextInitializer.init();
    SpringApplication.run(Application.class, args);
}

}

In this way ,I find the sloution

@alexIslander
Copy link

In Springboot Telegram starter version 4.1, you don't need to register your bot! It is done automatically!

@mhkarimi
Copy link

I had a same problem, just add this to your main springboot class :


@PostConstruct
    public void init(){
        ApiContextInitializer.init();
    }

@AppLoidx
Copy link

Suddenly someone will help. I had such an error when there was such a piece of code:

DefaultBotOptions botOptions = ApiContext.getInstance(DefaultBotOptions.class);

Maybe you, too, have lost something like this or something similar.

What is the coolest thing is that I called it up to the bot constructor 😫

@2fortunately
Copy link

2fortunately commented Sep 12, 2019

i have same problem, but i fix by add /lib directory with dependencies, my maven build:

<dependencies>
        <dependency>
            <groupId>org.telegram</groupId>
            <artifactId>telegrambots</artifactId>
            <version>4.4.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.4</version>
        </dependency>
</dependencies>
<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>myMainClass</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
</build>

@coarsehorse
Copy link

As @sslavian812 mentioned, the problem is in that Bot(TelegramLongPollingBot in my case) constructor was called before the ApiContextInitializer.init();.
Bot constructor expects that context is already initialized.
In my case constructor was called because of Spring DI.
So, if you want to use @Service, @Component, etc. annotations inside the bot class, put

static {
    ApiContextInitializer.init();
}

into bot class to initialize context API context at static initialization stage.
Or like in @pengchenxue example, run this code inside main() before SpringApplication.run(Application.class, args);.

@JimyDeveloper
Copy link

Try moving ApiContextInitializer.init(); to a static {} block. Or put it somewhere to run earlier.

thak you. it worked

@torvicv
Copy link

torvicv commented Apr 1, 2021

I solved removing Autowired of TelegramBot class.

`/@Autowired
private TelegramBot telegramBot;
/

TelegramBotsApi botsApi = new TelegramBotsApi();
botsApi.registerBot(new TelegramBot());`

And inserting in registerBot (class) directly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests