2020-05-12 12:52:37 +00:00
|
|
|
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
|
2020-05-14 18:29:17 +00:00
|
|
|
import io.ktor.client.engine.HttpClientEngine
|
|
|
|
import io.ktor.client.engine.ProxyConfig
|
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 ktorClientEngine: HttpClientEngine? = null,
|
|
|
|
var ktorClientConfig: (HttpClientConfig<*>.() -> Unit) ? = null
|
|
|
|
) {
|
|
|
|
internal fun createHttpClient(): HttpClient = ktorClientEngine ?.let { engine ->
|
|
|
|
HttpClient(engine) {
|
|
|
|
ktorClientConfig ?.let { it() }
|
|
|
|
engine {
|
|
|
|
proxy = this@BotBuilder.proxy ?: proxy
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} ?: HttpClient {
|
|
|
|
ktorClientConfig ?.let { it() }
|
|
|
|
engine {
|
|
|
|
proxy = this@BotBuilder.proxy ?: proxy
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-05-15 13:14:09 +00:00
|
|
|
* @return Created by [telegramBotWithCustomClientConfig] function [RequestsExecutor]. This executor will be preconfigured using [token] and
|
2020-05-12 12:52:37 +00:00
|
|
|
* [block]
|
|
|
|
*/
|
|
|
|
fun telegramBot(
|
|
|
|
token: String,
|
|
|
|
block: BotBuilder.() -> Unit
|
|
|
|
): RequestsExecutor = telegramBot(
|
|
|
|
TelegramAPIUrlsKeeper(token),
|
|
|
|
BotBuilder().apply(block).createHttpClient()
|
|
|
|
)
|