2020-10-04 10:57:39 +00:00
|
|
|
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,
|
2020-08-18 07:45:15 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-04 07:18:24 +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()
|
|
|
|
)
|