mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2024-11-26 03:58:44 +00:00
add telegramBot function
This commit is contained in:
parent
bb9c9e22a2
commit
671faabef9
@ -55,14 +55,22 @@ compile "com.github.insanusmokrassar:TelegramBotAPI-extensions-api:$telegrambota
|
|||||||
## Example of usage and comparison with `TelegramBotAPI`
|
## Example of usage and comparison with `TelegramBotAPI`
|
||||||
|
|
||||||
Here presented review table for comparison of api from original [TelegramBotAPI](../TelegramBotAPI/README.md#Requests)
|
Here presented review table for comparison of api from original [TelegramBotAPI](../TelegramBotAPI/README.md#Requests)
|
||||||
and extensions-api library:
|
and extensions-api library. First of all, this library allow to create bot instance in a new way:
|
||||||
|
|
||||||
In all examples supposed that you have created bot with next approximate lines:
|
|
||||||
|
|
||||||
```kotlin
|
```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 |
|
| TelegramBotAPI | TelegramBotAPI-extensions-api |
|
||||||
|----------------|-------------------------------|
|
|----------------|-------------------------------|
|
||||||
| bot.execute(GetMe) | bot.getMe() |
|
| bot.execute(GetMe) | bot.getMe() |
|
||||||
|
@ -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()
|
||||||
|
)
|
@ -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)
|
Loading…
Reference in New Issue
Block a user