1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-06-03 00:15:27 +00:00
tgbotapi/tgbotapi.extensions.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/BotBuilder.kt

62 lines
1.9 KiB
Kotlin
Raw Normal View History

package dev.inmo.tgbotapi.extensions.api
2020-05-12 12:52:37 +00:00
2020-10-04 11:06:30 +00:00
import dev.inmo.tgbotapi.bot.TelegramBot
import dev.inmo.tgbotapi.utils.TelegramAPIUrlsKeeper
2020-12-04 10:29:50 +00:00
import dev.inmo.tgbotapi.utils.telegramBotAPIDefaultUrl
2020-05-12 12:52:37 +00:00
import io.ktor.client.HttpClient
import io.ktor.client.HttpClientConfig
2020-06-02 15:18:49 +00:00
import io.ktor.client.engine.*
2020-05-12 12:52:37 +00:00
/**
* @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<HttpClientEngineConfig>? = null,
2020-05-12 12:52:37 +00:00
var ktorClientConfig: (HttpClientConfig<*>.() -> Unit) ? = null
) {
2020-06-02 15:18:49 +00:00
internal fun createHttpClient(): HttpClient = ktorClientEngineFactory ?.let {
HttpClient(
it.create {
this@create.proxy = this@BotBuilder.proxy ?: this@create.proxy
}
) {
ktorClientConfig ?.let { it() }
}
2020-05-12 12:52:37 +00:00
} ?: HttpClient {
ktorClientConfig ?.let { it() }
engine {
2020-06-02 15:18:49 +00:00
this@engine.proxy = this@BotBuilder.proxy ?: this@engine.proxy
2020-05-12 12:52:37 +00:00
}
}
}
/**
* @return Created by [telegramBotWithCustomClientConfig] function [TelegramBot]. This executor will be preconfigured using [token] and
2020-05-12 12:52:37 +00:00
* [block]
*/
2020-12-04 10:29:50 +00:00
fun buildBot(
2020-05-12 12:52:37 +00:00
token: String,
2020-12-04 10:29:50 +00:00
apiUrl: String = telegramBotAPIDefaultUrl,
2020-05-12 12:52:37 +00:00
block: BotBuilder.() -> Unit
2020-12-04 10:29:50 +00:00
) = telegramBot(
TelegramAPIUrlsKeeper(token, apiUrl),
2020-05-12 12:52:37 +00:00
BotBuilder().apply(block).createHttpClient()
)
2020-12-04 10:29:50 +00:00
/**
* @return Created by [telegramBotWithCustomClientConfig] function [TelegramBot]. This executor will be preconfigured using [token] and
* [block]
*/
@Deprecated("Renamed", ReplaceWith("buildBot", "dev.inmo.tgbotapi.extensions.api.buildBot"))
fun telegramBot(
token: String,
block: BotBuilder.() -> Unit
): TelegramBot = buildBot(
token,
telegramBotAPIDefaultUrl,
block
)