mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2024-11-22 08:13:47 +00:00
commonly of webhooks and other requests-related code
This commit is contained in:
parent
8b92a678fa
commit
6d8190e5aa
@ -2,6 +2,11 @@
|
|||||||
|
|
||||||
## 0.14.0
|
## 0.14.0
|
||||||
|
|
||||||
|
* Now library have no default engine for both webhooks and requests executor. It is required for clients to set
|
||||||
|
some default library
|
||||||
|
* All proxy help methods was removed . They are will be replaced in separated project
|
||||||
|
* `Ktor` version `1.1.3` -> `1.1.4`
|
||||||
|
|
||||||
## 0.13.0 Telegram Polls
|
## 0.13.0 Telegram Polls
|
||||||
|
|
||||||
* Type `PollOption` and `AnonymousPollOption` added
|
* Type `PollOption` and `AnonymousPollOption` added
|
||||||
|
11
build.gradle
11
build.gradle
@ -26,7 +26,6 @@ repositories {
|
|||||||
jcenter()
|
jcenter()
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
maven { url "https://kotlin.bintray.com/kotlinx" }
|
maven { url "https://kotlin.bintray.com/kotlinx" }
|
||||||
maven { url "https://dl.bintray.com/kotlin/ktor" }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
@ -35,14 +34,10 @@ dependencies {
|
|||||||
implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime:$kotlin_serialisation_runtime_version"
|
implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime:$kotlin_serialisation_runtime_version"
|
||||||
implementation "joda-time:joda-time:$joda_time_version"
|
implementation "joda-time:joda-time:$joda_time_version"
|
||||||
|
|
||||||
implementation "io.ktor:ktor-client-core:$ktor_version"
|
implementation "io.ktor:ktor-client:$ktor_version"
|
||||||
implementation "io.ktor:ktor-client-okhttp:$ktor_version"
|
|
||||||
|
|
||||||
implementation "io.ktor:ktor-server-core:$ktor_version"
|
implementation "io.ktor:ktor-server:$ktor_version"
|
||||||
implementation "io.ktor:ktor-server-netty:$ktor_version"
|
implementation "io.ktor:ktor-server-host-common:$ktor_version"
|
||||||
|
|
||||||
// Use JUnit test framework
|
|
||||||
testImplementation 'junit:junit:4.12'
|
|
||||||
}
|
}
|
||||||
|
|
||||||
compileKotlin {
|
compileKotlin {
|
||||||
|
@ -3,7 +3,7 @@ kotlin_version=1.3.30
|
|||||||
kotlin_coroutines_version=1.2.0
|
kotlin_coroutines_version=1.2.0
|
||||||
kotlin_serialisation_runtime_version=0.11.0
|
kotlin_serialisation_runtime_version=0.11.0
|
||||||
joda_time_version=2.10.1
|
joda_time_version=2.10.1
|
||||||
ktor_version=1.1.3
|
ktor_version=1.1.4
|
||||||
|
|
||||||
gradle_bintray_plugin_version=1.8.4
|
gradle_bintray_plugin_version=1.8.4
|
||||||
|
|
||||||
|
@ -1,16 +0,0 @@
|
|||||||
package com.github.insanusmokrassar.TelegramBotAPI
|
|
||||||
|
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.bot.Ktor.KtorRequestsExecutor
|
|
||||||
import io.ktor.client.engine.okhttp.OkHttp
|
|
||||||
import kotlinx.coroutines.runBlocking
|
|
||||||
|
|
||||||
fun main(args: Array<String>) {
|
|
||||||
runBlocking {
|
|
||||||
KtorRequestsExecutor(
|
|
||||||
args[0],
|
|
||||||
OkHttp.create()
|
|
||||||
).apply {
|
|
||||||
// It is just template of creating requests executor
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -12,7 +12,6 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.RetryAfterError
|
|||||||
import io.ktor.client.HttpClient
|
import io.ktor.client.HttpClient
|
||||||
import io.ktor.client.call.HttpClientCall
|
import io.ktor.client.call.HttpClientCall
|
||||||
import io.ktor.client.engine.HttpClientEngine
|
import io.ktor.client.engine.HttpClientEngine
|
||||||
import io.ktor.client.engine.okhttp.OkHttp
|
|
||||||
import io.ktor.util.cio.toByteArray
|
import io.ktor.util.cio.toByteArray
|
||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.delay
|
||||||
import kotlinx.io.charsets.Charset
|
import kotlinx.io.charsets.Charset
|
||||||
@ -20,7 +19,7 @@ import kotlinx.serialization.json.Json
|
|||||||
|
|
||||||
class KtorRequestsExecutor(
|
class KtorRequestsExecutor(
|
||||||
token: String,
|
token: String,
|
||||||
private val client: HttpClient = HttpClient(OkHttp),
|
private val client: HttpClient = HttpClient(),
|
||||||
hostUrl: String = "https://api.telegram.org",
|
hostUrl: String = "https://api.telegram.org",
|
||||||
callsFactories: List<KtorCallFactory> = emptyList(),
|
callsFactories: List<KtorCallFactory> = emptyList(),
|
||||||
excludeDefaultFactories: Boolean = false,
|
excludeDefaultFactories: Boolean = false,
|
||||||
@ -29,11 +28,11 @@ class KtorRequestsExecutor(
|
|||||||
) : BaseRequestsExecutor(token, hostUrl) {
|
) : BaseRequestsExecutor(token, hostUrl) {
|
||||||
constructor(
|
constructor(
|
||||||
token: String,
|
token: String,
|
||||||
engine: HttpClientEngine = OkHttp.create(),
|
engine: HttpClientEngine? = null,
|
||||||
hostUrl: String = "https://api.telegram.org"
|
hostUrl: String = "https://api.telegram.org"
|
||||||
) : this(
|
) : this(
|
||||||
token,
|
token,
|
||||||
HttpClient(engine),
|
engine ?.let { HttpClient(engine) } ?: HttpClient(),
|
||||||
hostUrl
|
hostUrl
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -1,32 +0,0 @@
|
|||||||
package com.github.insanusmokrassar.TelegramBotAPI.bot.Ktor
|
|
||||||
|
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.bot.settings.ProxySettings
|
|
||||||
import io.ktor.http.HttpHeaders
|
|
||||||
import okhttp3.Credentials
|
|
||||||
import okhttp3.OkHttpClient
|
|
||||||
import java.net.InetSocketAddress
|
|
||||||
import java.net.Proxy
|
|
||||||
|
|
||||||
fun OkHttpClient.Builder.useWith(proxySettings: ProxySettings) {
|
|
||||||
proxy(
|
|
||||||
Proxy(
|
|
||||||
Proxy.Type.SOCKS,
|
|
||||||
InetSocketAddress(
|
|
||||||
proxySettings.host,
|
|
||||||
proxySettings.port
|
|
||||||
)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
proxySettings.password ?.let {
|
|
||||||
password ->
|
|
||||||
proxyAuthenticator {
|
|
||||||
_, response ->
|
|
||||||
response.request().newBuilder().apply {
|
|
||||||
addHeader(
|
|
||||||
HttpHeaders.ProxyAuthorization,
|
|
||||||
Credentials.basic(proxySettings.username ?: "", password)
|
|
||||||
)
|
|
||||||
}.build()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,18 +0,0 @@
|
|||||||
package com.github.insanusmokrassar.TelegramBotAPI.bot
|
|
||||||
|
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.bot.Ktor.useWith
|
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.bot.settings.ProxySettings
|
|
||||||
import okhttp3.OkHttpClient
|
|
||||||
|
|
||||||
@Deprecated(
|
|
||||||
"Replaced in settings package",
|
|
||||||
ReplaceWith("ProxySettings", "com.github.insanusmokrassar.TelegramBotAPI.bot.settings.ProxySettings")
|
|
||||||
)
|
|
||||||
typealias ProxySettings = com.github.insanusmokrassar.TelegramBotAPI.bot.settings.ProxySettings
|
|
||||||
|
|
||||||
|
|
||||||
@Deprecated(
|
|
||||||
"Replaced in Ktor package",
|
|
||||||
ReplaceWith("useWith", "com.github.insanusmokrassar.TelegramBotAPI.bot.Ktor.useWith")
|
|
||||||
)
|
|
||||||
fun OkHttpClient.Builder.useWith(proxySettings: ProxySettings) = useWith(proxySettings)
|
|
@ -16,7 +16,6 @@ import io.ktor.response.respond
|
|||||||
import io.ktor.routing.post
|
import io.ktor.routing.post
|
||||||
import io.ktor.routing.routing
|
import io.ktor.routing.routing
|
||||||
import io.ktor.server.engine.*
|
import io.ktor.server.engine.*
|
||||||
import io.ktor.server.netty.Netty
|
|
||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.*
|
||||||
import kotlinx.coroutines.channels.Channel
|
import kotlinx.coroutines.channels.Channel
|
||||||
import kotlinx.serialization.json.Json
|
import kotlinx.serialization.json.Json
|
||||||
@ -36,12 +35,12 @@ import java.util.concurrent.TimeUnit
|
|||||||
suspend fun RequestsExecutor.setWebhook(
|
suspend fun RequestsExecutor.setWebhook(
|
||||||
url: String,
|
url: String,
|
||||||
port: Int,
|
port: Int,
|
||||||
|
engineFactory: ApplicationEngineFactory<*, *>,
|
||||||
certificate: InputFile? = null,
|
certificate: InputFile? = null,
|
||||||
privateKeyConfig: WebhookPrivateKeyConfig? = null,
|
privateKeyConfig: WebhookPrivateKeyConfig? = null,
|
||||||
scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()),
|
scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()),
|
||||||
allowedUpdates: List<String>? = null,
|
allowedUpdates: List<String>? = null,
|
||||||
maxAllowedConnections: Int? = null,
|
maxAllowedConnections: Int? = null,
|
||||||
engineFactory: ApplicationEngineFactory<*, *> = Netty,
|
|
||||||
block: UpdateReceiver<Update>
|
block: UpdateReceiver<Update>
|
||||||
): Job {
|
): Job {
|
||||||
val executeDeferred = certificate ?.let {
|
val executeDeferred = certificate ?.let {
|
||||||
@ -69,7 +68,6 @@ suspend fun RequestsExecutor.setWebhook(
|
|||||||
val env = applicationEngineEnvironment {
|
val env = applicationEngineEnvironment {
|
||||||
|
|
||||||
module {
|
module {
|
||||||
fun Application.main() {
|
|
||||||
routing {
|
routing {
|
||||||
post {
|
post {
|
||||||
val deserialized = call.receiveText()
|
val deserialized = call.receiveText()
|
||||||
@ -82,8 +80,6 @@ suspend fun RequestsExecutor.setWebhook(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
main()
|
|
||||||
}
|
|
||||||
privateKeyConfig ?.let {
|
privateKeyConfig ?.let {
|
||||||
sslConnector(
|
sslConnector(
|
||||||
privateKeyConfig.keyStore,
|
privateKeyConfig.keyStore,
|
||||||
@ -140,19 +136,19 @@ suspend fun RequestsExecutor.setWebhook(
|
|||||||
url: String,
|
url: String,
|
||||||
port: Int,
|
port: Int,
|
||||||
filter: UpdatesFilter,
|
filter: UpdatesFilter,
|
||||||
|
engineFactory: ApplicationEngineFactory<*, *>,
|
||||||
certificate: InputFile? = null,
|
certificate: InputFile? = null,
|
||||||
privateKeyConfig: WebhookPrivateKeyConfig? = null,
|
privateKeyConfig: WebhookPrivateKeyConfig? = null,
|
||||||
scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()),
|
scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()),
|
||||||
maxAllowedConnections: Int? = null,
|
maxAllowedConnections: Int? = null
|
||||||
engineFactory: ApplicationEngineFactory<*, *> = Netty
|
|
||||||
): Job = setWebhook(
|
): Job = setWebhook(
|
||||||
url,
|
url,
|
||||||
port,
|
port,
|
||||||
|
engineFactory,
|
||||||
certificate,
|
certificate,
|
||||||
privateKeyConfig,
|
privateKeyConfig,
|
||||||
scope,
|
scope,
|
||||||
filter.allowedUpdates,
|
filter.allowedUpdates,
|
||||||
maxAllowedConnections,
|
maxAllowedConnections,
|
||||||
engineFactory,
|
|
||||||
filter.asUpdateReceiver
|
filter.asUpdateReceiver
|
||||||
)
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user