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

telegrambotsextensions NPE #713

Closed xTheDoctah closed 4 years ago

xTheDoctah commented 4 years ago

When i type the command /start on the bot i got this error. Actually the classes are like this.

@Component
public class RegisterBot extends TelegramLongPollingCommandBot {
    @Value("${telegram.regsterbot.token}")
    private String botToken;

    @Autowired
    public RegisterBot(Start start) {
        register(start);
    }

    @Override
    public void processNonCommandUpdate(Update update) {
        System.out.println(update.getMessage().getText());
    }

    @Override
    public String getBotToken() {
        return botToken;
    }
}

and

@Component
public class Start extends BotCommand {

    public Start() {
        super("start", "Run the bot like a boss");
    }

    @Override
    public void execute(AbsSender absSender, User user, Chat chat, String[] strings) {
        try {
            absSender.execute(new SendMessage().setChatId(chat.getId()).setText("BabboNatale"));
        } catch (TelegramApiException e) {
            e.printStackTrace();
        }
    }
}

Pom:

org.telegram telegrambots 4.5
    <dependency>
        <groupId>org.telegram</groupId>
        <artifactId>telegrambotsextensions</artifactId>
        <version>4.5</version>
    </dependency>

Stacktrace: java.lang.NullPointerException: null at java.util.regex.Pattern.quote(Pattern.java:1291) ~[na:1.8.0_181] at org.telegram.telegrambots.extensions.bots.commandbot.commands.CommandRegistry.removeUsernameFromCommandIfNeeded(CommandRegistry.java:126) ~[telegrambotsextensions-4.5.jar:na] at org.telegram.telegrambots.extensions.bots.commandbot.commands.CommandRegistry.executeCommand(CommandRegistry.java:103) ~[telegrambotsextensions-4.5.jar:na] at org.telegram.telegrambots.extensions.bots.commandbot.TelegramLongPollingCommandBot.onUpdateReceived(TelegramLongPollingCommandBot.java:78) ~[telegrambotsextensions-4.5.jar:na] at java.util.ArrayList.forEach(ArrayList.java:1257) ~[na:1.8.0_181] at org.telegram.telegrambots.meta.generics.LongPollingBot.onUpdatesReceived(LongPollingBot.java:27) ~[telegrambots-meta-4.5.jar:na] at org.telegram.telegrambots.updatesreceivers.DefaultBotSession$HandlerThread.run(DefaultBotSession.java:312) ~[telegrambots-4.5.jar:na]

xTheDoctah commented 4 years ago

Solved, i forgot to implement the constructor of the telegramLongPollingCommandBot.(basically the botname misses)

But there's no way to set it up. Theres a constructor but its also deprecated.

Final solution was add super(new DefaultBotOptions(), false); to the constructor of the bot