Simple Java telegram bots runner built on top of the Telegram Bots library
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
gradle/wrapper
module Add group admin role support Nov 11, 2018
testbot-kotlin
testbot
.gitignore
LICENSE
README.md
config.yaml
gradlew
gradlew.bat
settings.gradle
testbot.yaml

README.md

tgbots-module

Simple Java telegram bots runner built on top of the Telegram Bots library.

Why?

  • I want separate projects per bots.
  • I want to easily switch between long polling and webhook methods without recompiling the application.
  • I want to easily disable bots without recompiling the application.
  • I want yaml configs to store bot tokens and other data.
  • I want different profiles for configs.

Usage

  • Add gradle dependency:

    compile 'com.annimon:tgbots-module:0.2'
  • Implement BotModule interface:

    public class TestBot implements BotModule {   
       @Override
       public BotHandler botHandler(Config config) {
           return new TestBotHandler();
       }
    }
  • [Optional] Add main method to run single project:

    public class TestBot implements BotModule {   
       public static void main(String[] args) {
           final var profile = (args.length >= 1 && !args[0].isEmpty()) ? args[0] : "";
           Runner.run(profile, List.of(new TestBot()));
       }
       // ...
    }
  • [Optional] Add yaml configuration support:

    import lombok.Data;
    import com.fasterxml.jackson.annotation.JsonProperty;
    import javax.validation.constraints.NotBlank;
    
    @Data
    public class BotConfig {
    
        @NotBlank
        @JsonProperty(required = true)
        private String token;
    
        @NotBlank
        @JsonProperty(required = true)
        private String username;
    }

    testbot.yaml

    token: 123456789:ABCDEFGHIJKLM_NOPQRSTUVWXYZ01234567
    username: bot
    public class TestBot implements BotModule {   
       // ...
       @Override
       public BotHandler botHandler(Config config) {
           final var configLoader = new YamlConfigLoaderService<BotConfig>();
           final var configFile = configLoader.configFile("testbot", config.getProfile());
           final var botConfig = configLoader.load(configFile, BotConfig.class);
           return new TestBotHandler(botConfig);
       }
    }
  • Fill config.yaml:

    log-level: FINE
    webhook:
      enabled: false
      port: env(PORT:8443)
      externalUrl: https://123.45.67.89:$port
      internalUrl: https://123.45.67.89:$port
      certificatePublicKeyPath: cert/public_cert.pem
      certificateStorePath: cert/keystore.jks
      certificateStorePassword: env(STORE_PASSWORD)
    modules:
      - com.annimon.testbot.TestBot
  • Happy bots developing:

    public class TestBotHandler extends BotHandler {
    
        private final BotConfig botConfig;
    
        public TestBotHandler(BotConfig botConfig) {
            this.botConfig = botConfig;
        }
    
        @Override
        public BotApiMethod onUpdate(Update update) {
            // your code here
            return null;
        }
    
        @Override
        public String getBotUsername() {
            return botConfig.getUsername();
        }
    
        @Override
        public String getBotToken() {
            return botConfig.getToken();
        }
    }

Now you can easily switch between webhook and longpolling methods by changing the webhook: enabled flag in config,yaml.

Or you can create config-test.yaml and run the test profile:

java -cp tgbots-module.jar:testbot.jar:yourfavoritebot.jar com.annimon.telegrambots.Runner test