mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2024-12-22 16:47:13 +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
|
||||
|
||||
* 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
|
||||
|
||||
* Type `PollOption` and `AnonymousPollOption` added
|
||||
|
11
build.gradle
11
build.gradle
@ -26,7 +26,6 @@ repositories {
|
||||
jcenter()
|
||||
mavenCentral()
|
||||
maven { url "https://kotlin.bintray.com/kotlinx" }
|
||||
maven { url "https://dl.bintray.com/kotlin/ktor" }
|
||||
}
|
||||
|
||||
dependencies {
|
||||
@ -35,14 +34,10 @@ dependencies {
|
||||
implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime:$kotlin_serialisation_runtime_version"
|
||||
implementation "joda-time:joda-time:$joda_time_version"
|
||||
|
||||
implementation "io.ktor:ktor-client-core:$ktor_version"
|
||||
implementation "io.ktor:ktor-client-okhttp:$ktor_version"
|
||||
implementation "io.ktor:ktor-client:$ktor_version"
|
||||
|
||||
implementation "io.ktor:ktor-server-core:$ktor_version"
|
||||
implementation "io.ktor:ktor-server-netty:$ktor_version"
|
||||
|
||||
// Use JUnit test framework
|
||||
testImplementation 'junit:junit:4.12'
|
||||
implementation "io.ktor:ktor-server:$ktor_version"
|
||||
implementation "io.ktor:ktor-server-host-common:$ktor_version"
|
||||
}
|
||||
|
||||
compileKotlin {
|
||||
|
@ -3,7 +3,7 @@ kotlin_version=1.3.30
|
||||
kotlin_coroutines_version=1.2.0
|
||||
kotlin_serialisation_runtime_version=0.11.0
|
||||
joda_time_version=2.10.1
|
||||
ktor_version=1.1.3
|
||||
ktor_version=1.1.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.call.HttpClientCall
|
||||
import io.ktor.client.engine.HttpClientEngine
|
||||
import io.ktor.client.engine.okhttp.OkHttp
|
||||
import io.ktor.util.cio.toByteArray
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.io.charsets.Charset
|
||||
@ -20,7 +19,7 @@ import kotlinx.serialization.json.Json
|
||||
|
||||
class KtorRequestsExecutor(
|
||||
token: String,
|
||||
private val client: HttpClient = HttpClient(OkHttp),
|
||||
private val client: HttpClient = HttpClient(),
|
||||
hostUrl: String = "https://api.telegram.org",
|
||||
callsFactories: List<KtorCallFactory> = emptyList(),
|
||||
excludeDefaultFactories: Boolean = false,
|
||||
@ -29,11 +28,11 @@ class KtorRequestsExecutor(
|
||||
) : BaseRequestsExecutor(token, hostUrl) {
|
||||
constructor(
|
||||
token: String,
|
||||
engine: HttpClientEngine = OkHttp.create(),
|
||||
engine: HttpClientEngine? = null,
|
||||
hostUrl: String = "https://api.telegram.org"
|
||||
) : this(
|
||||
token,
|
||||
HttpClient(engine),
|
||||
engine ?.let { HttpClient(engine) } ?: HttpClient(),
|
||||
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.routing
|
||||
import io.ktor.server.engine.*
|
||||
import io.ktor.server.netty.Netty
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.channels.Channel
|
||||
import kotlinx.serialization.json.Json
|
||||
@ -36,12 +35,12 @@ import java.util.concurrent.TimeUnit
|
||||
suspend fun RequestsExecutor.setWebhook(
|
||||
url: String,
|
||||
port: Int,
|
||||
engineFactory: ApplicationEngineFactory<*, *>,
|
||||
certificate: InputFile? = null,
|
||||
privateKeyConfig: WebhookPrivateKeyConfig? = null,
|
||||
scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()),
|
||||
allowedUpdates: List<String>? = null,
|
||||
maxAllowedConnections: Int? = null,
|
||||
engineFactory: ApplicationEngineFactory<*, *> = Netty,
|
||||
block: UpdateReceiver<Update>
|
||||
): Job {
|
||||
val executeDeferred = certificate ?.let {
|
||||
@ -69,20 +68,17 @@ suspend fun RequestsExecutor.setWebhook(
|
||||
val env = applicationEngineEnvironment {
|
||||
|
||||
module {
|
||||
fun Application.main() {
|
||||
routing {
|
||||
post {
|
||||
val deserialized = call.receiveText()
|
||||
val update = Json.nonstrict.parse(
|
||||
RawUpdate.serializer(),
|
||||
deserialized
|
||||
)
|
||||
updatesChannel.send(update.asUpdate)
|
||||
call.respond("Ok")
|
||||
}
|
||||
routing {
|
||||
post {
|
||||
val deserialized = call.receiveText()
|
||||
val update = Json.nonstrict.parse(
|
||||
RawUpdate.serializer(),
|
||||
deserialized
|
||||
)
|
||||
updatesChannel.send(update.asUpdate)
|
||||
call.respond("Ok")
|
||||
}
|
||||
}
|
||||
main()
|
||||
}
|
||||
privateKeyConfig ?.let {
|
||||
sslConnector(
|
||||
@ -140,19 +136,19 @@ suspend fun RequestsExecutor.setWebhook(
|
||||
url: String,
|
||||
port: Int,
|
||||
filter: UpdatesFilter,
|
||||
engineFactory: ApplicationEngineFactory<*, *>,
|
||||
certificate: InputFile? = null,
|
||||
privateKeyConfig: WebhookPrivateKeyConfig? = null,
|
||||
scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()),
|
||||
maxAllowedConnections: Int? = null,
|
||||
engineFactory: ApplicationEngineFactory<*, *> = Netty
|
||||
maxAllowedConnections: Int? = null
|
||||
): Job = setWebhook(
|
||||
url,
|
||||
port,
|
||||
engineFactory,
|
||||
certificate,
|
||||
privateKeyConfig,
|
||||
scope,
|
||||
filter.allowedUpdates,
|
||||
maxAllowedConnections,
|
||||
engineFactory,
|
||||
filter.asUpdateReceiver
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user