From 671faabef92651da068d6366c40f6b3baa140e6e Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 12 May 2020 18:52:37 +0600 Subject: [PATCH] add telegramBot function --- TelegramBotAPI-extensions-api/README.md | 16 +++- .../extensions/api/BotBuilder.kt | 44 +++++++++++ .../extensions/api/BotExtensions.kt | 76 +++++++++++++++++++ 3 files changed, 132 insertions(+), 4 deletions(-) create mode 100644 TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/BotBuilder.kt create mode 100644 TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/BotExtensions.kt diff --git a/TelegramBotAPI-extensions-api/README.md b/TelegramBotAPI-extensions-api/README.md index 05ef1d3fda..eeac67ba38 100644 --- a/TelegramBotAPI-extensions-api/README.md +++ b/TelegramBotAPI-extensions-api/README.md @@ -55,14 +55,22 @@ compile "com.github.insanusmokrassar:TelegramBotAPI-extensions-api:$telegrambota ## Example of usage and comparison with `TelegramBotAPI` Here presented review table for comparison of api from original [TelegramBotAPI](../TelegramBotAPI/README.md#Requests) -and extensions-api library: - -In all examples supposed that you have created bot with next approximate lines: +and extensions-api library. First of all, this library allow to create bot instance in a new way: ```kotlin -val bot: RequestsExecutor = ... +val bot = telegramBot("IT IS YOUR TOKEN") ``` +There are a lot of signature for this. For example, you can create bot with next code: + +```kotlin +val bot = telegramBot("IT IS YOUR TOKEN") { + proxy = ProxyBuilder.socks("127.0.0.1", 1080) +} +``` + +In all examples supposed that you have created bot. + | TelegramBotAPI | TelegramBotAPI-extensions-api | |----------------|-------------------------------| | bot.execute(GetMe) | bot.getMe() | diff --git a/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/BotBuilder.kt b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/BotBuilder.kt new file mode 100644 index 0000000000..414f2a1d8d --- /dev/null +++ b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/BotBuilder.kt @@ -0,0 +1,44 @@ +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 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 + } + } +} + +/** + * @return Created by [telegramBot] 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() +) diff --git a/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/BotExtensions.kt b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/BotExtensions.kt new file mode 100644 index 0000000000..44514ddeae --- /dev/null +++ b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/BotExtensions.kt @@ -0,0 +1,76 @@ +package com.github.insanusmokrassar.TelegramBotAPI.extensions.api + +import com.github.insanusmokrassar.TelegramBotAPI.bot.Ktor.KtorRequestsExecutor +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.HttpClientEngine + +/** + * Allows to create bot using bot [urlsKeeper] and already prepared [client] + */ +fun telegramBot( + urlsKeeper: TelegramAPIUrlsKeeper, + client: HttpClient +): RequestsExecutor = KtorRequestsExecutor( + urlsKeeper, + client +) + +/** + * Allows to create bot using bot [urlsKeeper] and specify [HttpClientEngine] by passing [clientEngine] param and optionally + * configure [HttpClient] using [clientConfig] + */ +fun telegramBot( + urlsKeeper: TelegramAPIUrlsKeeper, + clientEngine: HttpClientEngine, + clientConfig: HttpClientConfig<*>.() -> Unit = {} +): RequestsExecutor = telegramBot( + urlsKeeper, + HttpClient(clientEngine, clientConfig) +) + +/** + * Allows to create bot using bot [urlsKeeper] and optionally configure [HttpClient] using [clientConfig] + */ +fun telegramBot( + urlsKeeper: TelegramAPIUrlsKeeper, + clientConfig: HttpClientConfig<*>.() -> Unit = {} +): RequestsExecutor = telegramBot( + urlsKeeper, + HttpClient(clientConfig) +) + +/** + * Allows to create bot using bot [token] + */ +fun telegramBot( + token: String +): RequestsExecutor = telegramBot(TelegramAPIUrlsKeeper(token)) + +/** + * Allows to create bot using bot [token] and already prepared [client] + */ +fun telegramBot( + token: String, + client: HttpClient +): RequestsExecutor = telegramBot(TelegramAPIUrlsKeeper(token), client) + +/** + * Allows to create bot using bot [token] and configure [HttpClient] using [clientConfig] + */ +fun telegramBot( + token: String, + clientConfig: HttpClientConfig<*>.() -> Unit +): RequestsExecutor = telegramBot(TelegramAPIUrlsKeeper(token), clientConfig) + +/** + * Allows to create bot using bot [token] and specify [HttpClientEngine] by passing [clientEngine] param and optionally + * configure [HttpClient] using [clientConfig] + */ +fun telegramBot( + token: String, + clientEngine: HttpClientEngine, + clientConfig: HttpClientConfig<*>.() -> Unit = {} +): RequestsExecutor = telegramBot(TelegramAPIUrlsKeeper(token), clientEngine, clientConfig)