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

How to use the ability library with spring boot? #1041

Open LexProfi opened 2 years ago

LexProfi commented 2 years ago

Tell me - how can I run the ability library using spring boot?

4o4-Not-Found commented 2 years ago

https://github.com/rubenlagus/TelegramBots/tree/master/telegrambots-spring-boot-starter

LexProfi commented 2 years ago

https://github.com/rubenlagus/TelegramBots/tree/master/telegrambots-spring-boot-starter

This is not entirely suitable, because in telegrambots-spring-boot-starter it is proposed to create a class heir to TelegramLongPollingBot

import org.telegram.telegrambots.bots.TelegramLongPollingBot;

While the ability library implements LongPollingBot

import org.telegram.telegrambots.meta.generics.LongPollingBot;

The question is how to run telegrambots-abilities in Spring.

webrelab commented 2 years ago

It work for class extending AbilityBot

LdSe commented 7 months ago

Hello! This might be super late, but I just started playing with this library.

Here's what I did to be able to use abilities with Spring:

  1. Make your bot extend AbilityBot and implement SpringLongPollingBot if you use that strategy.
  2. Implement the ability you want at the bot level.
  3. Implement the following:

@AfterBotRegistration public void afterRegistration(BotSession botSession) { this.onRegister(); } @Override public LongPollingUpdateConsumer getUpdatesConsumer() { return this; }

With the first method, you're forcing the AbilityBot to get initialized, forcing the scan of the abilities you implement at the class level.

Hope this is useful for someone else.

Cheers!