All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.com.github.insanusmokrassar.TelegramBotAPI.extensions.api.BotBuilder.kt Maven / Gradle / Ivy

Go to download

API extensions which provide work with RequestsExecutor of TelegramBotAPI almost like it is described in original Telegram Bot API reference

The newest version!
package com.github.insanusmokrassar.TelegramBotAPI.extensions.api

import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor
import com.github.insanusmokrassar.TelegramBotAPI.utils.TelegramAPIUrlsKeeper
import io.ktor.client.HttpClient
import io.ktor.client.HttpClientConfig
import io.ktor.client.engine.*

/**
 * @param proxy Standard ktor [ProxyConfig]
 * @param ktorClientEngine Engine like [io.ktor.client.engine.cio.CIO]
 * @param ktorClientConfig Config block for preconfiguring of bot [HttpClient]
 */
data class BotBuilder internal constructor(
    var proxy: ProxyConfig? = null,
    var ktorClientEngineFactory: HttpClientEngineFactory? = null,
    var ktorClientConfig: (HttpClientConfig<*>.() -> Unit) ? = null
) {
    internal fun createHttpClient(): HttpClient = ktorClientEngineFactory ?.let {
        HttpClient(
            it.create {
                [email protected] = [email protected] ?: [email protected]
            }
        ) {
            ktorClientConfig ?.let { it() }
        }
    } ?: HttpClient {
        ktorClientConfig ?.let { it() }
        engine {
            [email protected] = [email protected] ?: [email protected]
        }
    }
}

/**
 * @return Created by [telegramBotWithCustomClientConfig] function [RequestsExecutor]. This executor will be preconfigured using [token] and
 * [block]
 */
fun telegramBot(
    token: String,
    block: BotBuilder.() -> Unit
): RequestsExecutor = telegramBot(
    TelegramAPIUrlsKeeper(token),
    BotBuilder().apply(block).createHttpClient()
)




© 2015 - 2025 Weber Informatics LLC | Privacy Policy