Spring starter

The Spring Starter module for library is an auto-configuration module that integrates Telegram bot functionalities into Spring Boot applications. It leverages the power of Spring Boot's dependency injection and configuration properties to automatically configure Telegram bots based on the provided configuration. This library is particularly useful for developers looking to build Telegram bots using Kotlin and Spring Boot, offering a streamlined approach to bot development and management.

Key Features

Getting Started

To use the Spring Starter Library for Telegram Bots, you need to include it as a dependency in your Spring Boot project. The library is designed to work with Spring Boot applications and requires the Spring Boot framework to function.

Dependency

Add the following dependency to your build.gradle or pom.xml file:

dependencies {
    implementation 'eu.vendeli:spring-starter:<version>'
}

Replace <version> with the latest version of the library.

Configuration

The library uses Spring Boot's @ConfigurationProperties to bind configuration properties. You can define your bot configurations in the application.properties or application.yml file of your Spring Boot application.

ktgram:
 autoStartPolling: true
 shareHttpClient: true
 bot:
    - token: YOUR_BOT_TOKEN
      pckg: com.example.bot
      identifier: MyBot

Usage

Once the library is included and configured, it automatically creates and configures Telegram bot instances based on the provided configuration.

It also supports multiple bot instances, to initialize several ones just declare it as new entry in bot section:

ktgram:
 bot:
    - token: YOUR_BOT_TOKEN
    - token: SECOND_BOT_TOKEN

Advanced Configuration

For more advanced configurations, such as customizing bot behavior or integrating with other Spring components, you can extend the BotConfiguration class and change bot configuration through its applyCfg method, you can see example there.

[!TIP] To configure each initialized instance with a custom configuration, distinguish them by their identifier (the BotConfiguration class also has an identifier).

Ktor

The module is designed to facilitate the creation of a webhook server for Telegram bots. It allows developers to configure the server, including SSL/TLS settings, and declare multiple Telegram bots with custom configurations. The setup process is flexible, enabling developers to tailor the server to their specific needs.

Installation

To install ktor starter add additional to main dependency:

dependencies {
    implementation("eu.vendeli:ktor-starter:x.y.z") // there
    // change x.y.z to current library version
}

Key Components

serveWebhook Function

The serveWebhook function is the core of the library. It sets up and starts the webhook server for Telegram bots. It accepts two parameters:

Configuration

Server Setup

The library provides wide range of configurable parameters for the server, including host, port, SSL settings, and more. There are two concrete options for its configuring:

There's list of parameters that can be set:

[!TIP] If pem certificates are present, the module itself will create a jks storage from them at the specified path.

Bot Configuration:

To configure bot call declareBot {} which have such parameters:

Example Usage

To use this module, call serveWebhook function, configure it with your desired settings, declare your bots. Here's a simplified example:

fun main() = runBlocking {
    serveWebhook {
        server {
            HOST = "0.0.0.0"
            PORT = 8080
            SSL_PORT = 8443

            PEM_PRIVATE_KEY_PATH = "/etc/letsencrypt/live/example.com/privkey.pem"
            PEM_CHAIN_PATH = "/etc/letsencrypt/live/example.com/fullchain.pem"
            PEM_PRIVATE_KEY = "pem_changeit".toCharArray()

            KEYSTORE_PATH = "/etc/ssl/certs/java/cacerts/bot_keystore.jks"
            KEYSTORE_PASSWORD = "changeit".toCharArray()
            // Set other configuration parameters as needed
        }
        declareBot {
            token = "YOUR_BOT_TOKEN"
            // Configure other bot settings
        }
        // Add more bots or set other parameters if needed
    }
}

[!CAUTION] Don't forget to set webhook to make everything work. 😃

By default module will serve webhook listenening endpoints as host/BOT_TOKEN