rubenlagus / TelegramBots

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

After upgrade to 7.2.x AbilityBot.onRegister must be called manually or it will fail to accept messages #1354

Open chpasha opened 5 months ago

chpasha commented 5 months ago

Describe the bug After upgrade to 7.2.x if I don't call AbilityBot.onRegister() method manually after bot creation, it will fail to receive any messages from API with following exception

Exception in thread "pool-2-thread-1" java.lang.NullPointerException: Cannot invoke "java.util.List.stream()" because "this.replies" is null
    at org.telegram.telegrambots.abilitybots.api.bot.BaseAbilityBot.filterReply(BaseAbilityBot.java:621)
    at java.base/java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:178)
    at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:197)
    at java.base/java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:179)
    at java.base/java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:179)
    at java.base/java.util.stream.Streams$StreamBuilderImpl.forEachRemaining(Streams.java:411)
    at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:509)
    at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499)
    at java.base/java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150)
    at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173)
    at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    at java.base/java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:596)
    at org.telegram.telegrambots.abilitybots.api.bot.BaseAbilityBot.consume(BaseAbilityBot.java:201)
    at org.telegram.telegrambots.abilitybots.api.bot.AbilityBot.consume(AbilityBot.java:37)

this.replies is indeed null until onRegister is called. So my question is, am I doing something wrong? Documentation says nothing about it and yet everything works fine only if I call this method manually. If it is an intention? If so, why it is not mentioned anywhere. And if it is an intention, it looks like a bad design choice, because some magic method on bot must be called additionally to its creation and registration in TelegramBotApplication

To Reproduce Steps to reproduce the behavior:

implementation 'org.telegram:telegrambots-springboot-longpolling-starter:7.2.1'
implementation 'org.telegram:telegrambots-abilities:7.2.1'
implementation 'org.telegram:telegrambots-client:7.2.1'

class MyBot extends AbilityBot implements SpringLongPollingBot  {}

Expected behavior Ideally, onRegister would be called automatically OR at least mentioned in the documentation and examples provided

rubenlagus commented 2 months ago

@chpasha have you try adding this to your bot?

    @AfterBotRegistration
    @Override
    public void onRegister() {
        super.onRegister();
    }

That should ensure that, after registering the bot in spring, the onRegister method is autocalled.

chpasha commented 2 months ago

Hi, thanks. I used the @PostConstruct annotation which might be not the 100% right one, since post construction doesn't mean, the bot is already registered, but because the default implementation creates some stuff and doesn't seem to depend on being really registered, that did the trick. But like I wrote before, I found such design decision questionable, that some method has to be called before bot is usable. Is there any reason why you can't call onRegister on public method of the bot, when registerBot is called?

rubenlagus commented 2 months ago

I don't think abilities was built with spring boot in mind. Hence it requires some extra workarounds to make it work.