1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-29 13:38:43 +00:00

Merge pull request #207 from InsanusMokrassar/0.30.2

0.30.2
This commit is contained in:
InsanusMokrassar 2020-11-10 21:52:37 +06:00 committed by GitHub
commit 96e00f6e31
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 46 additions and 80 deletions

View File

@ -1,5 +1,16 @@
# TelegramBotAPI changelog # TelegramBotAPI changelog
## 0.30.2
* `Common`:
* `Version`:
* `Ktor`: `1.4.1` -> `1.4.2`
* `Core`:
* New sealed class `SetWebhookRequest` which can be used in `SetWebhook` requests
* `Utils`:
* Extensions `setWebhookInfoAndStartListenWebhooks` has been united in one extension with `SetWebhookRequest`
incoming parameter
## 0.30.1 ## 0.30.1
* `Common`: * `Common`:

View File

@ -10,14 +10,14 @@ kotlin_coroutines_version=1.4.1
kotlin_serialisation_runtime_version=1.0.1 kotlin_serialisation_runtime_version=1.0.1
klock_version=1.12.1 klock_version=1.12.1
uuid_version=0.2.2 uuid_version=0.2.2
ktor_version=1.4.1 ktor_version=1.4.2
micro_utils_version=0.3.0 micro_utils_version=0.3.0
javax_activation_version=1.1.1 javax_activation_version=1.1.1
library_group=dev.inmo library_group=dev.inmo
library_version=0.30.1 library_version=0.30.2
gradle_bintray_plugin_version=1.8.5 gradle_bintray_plugin_version=1.8.5
github_release_plugin_version=2.2.12 github_release_plugin_version=2.2.12

View File

@ -1,8 +1,7 @@
package dev.inmo.tgbotapi.requests.webhook package dev.inmo.tgbotapi.requests.webhook
import dev.inmo.tgbotapi.requests.abstracts.* import dev.inmo.tgbotapi.requests.abstracts.*
import dev.inmo.tgbotapi.requests.send.media.base.DataRequest import dev.inmo.tgbotapi.requests.send.media.base.*
import dev.inmo.tgbotapi.requests.send.media.base.MultipartRequestImpl
import dev.inmo.tgbotapi.types.* import dev.inmo.tgbotapi.types.*
import kotlinx.serialization.* import kotlinx.serialization.*
import kotlinx.serialization.builtins.serializer import kotlinx.serialization.builtins.serializer
@ -13,14 +12,15 @@ private fun correctWebhookUrl(sourceUrl: String) = if (sourceUrl.contains("://")
"https://$sourceUrl" "https://$sourceUrl"
} }
fun SetWebhook( sealed class SetWebhookRequest : Request<Boolean>
class MultipartSetWebhookRequest(
url: String, url: String,
certificate: MultipartFile, certificate: MultipartFile,
ipAddress: String? = null, ipAddress: String? = null,
maxAllowedConnections: Int? = null, maxAllowedConnections: Int? = null,
allowedUpdates: List<String>? = null, allowedUpdates: List<String>? = null,
dropPendingUpdates: Boolean? = null dropPendingUpdates: Boolean? = null
): MultipartRequestImpl<SetWebhook, Map<String, MultipartFile>, Boolean> = MultipartRequestImpl( ) : SetWebhookRequest(), MultipartRequest<Boolean> by MultipartRequestImpl(
SetWebhook( SetWebhook(
correctWebhookUrl(url), correctWebhookUrl(url),
null as String?, null as String?,
@ -32,6 +32,22 @@ fun SetWebhook(
mapOf(certificateField to certificate) mapOf(certificateField to certificate)
) )
fun SetWebhook(
url: String,
certificate: MultipartFile,
ipAddress: String? = null,
maxAllowedConnections: Int? = null,
allowedUpdates: List<String>? = null,
dropPendingUpdates: Boolean? = null
): MultipartSetWebhookRequest = MultipartSetWebhookRequest(
correctWebhookUrl(url),
certificate,
ipAddress,
maxAllowedConnections,
allowedUpdates,
dropPendingUpdates
)
fun SetWebhook( fun SetWebhook(
url: String, url: String,
certificate: FileId, certificate: FileId,
@ -63,7 +79,7 @@ fun SetWebhook(
maxAllowedConnections: Int? = null, maxAllowedConnections: Int? = null,
allowedUpdates: List<String>? = null, allowedUpdates: List<String>? = null,
dropPendingUpdates: Boolean? = null dropPendingUpdates: Boolean? = null
): Request<Boolean> = when (certificate) { ) = when (certificate) {
is MultipartFile -> SetWebhook(correctWebhookUrl(url), certificate as MultipartFile, ipAddress, maxAllowedConnections, allowedUpdates, dropPendingUpdates) is MultipartFile -> SetWebhook(correctWebhookUrl(url), certificate as MultipartFile, ipAddress, maxAllowedConnections, allowedUpdates, dropPendingUpdates)
is FileId -> SetWebhook(correctWebhookUrl(url), certificate as FileId, ipAddress, maxAllowedConnections, allowedUpdates, dropPendingUpdates) is FileId -> SetWebhook(correctWebhookUrl(url), certificate as FileId, ipAddress, maxAllowedConnections, allowedUpdates, dropPendingUpdates)
} }
@ -82,7 +98,7 @@ fun SetWebhook(
maxAllowedConnections: Int? = null, maxAllowedConnections: Int? = null,
allowedUpdates: List<String>? = null, allowedUpdates: List<String>? = null,
dropPendingUpdates: Boolean? = null dropPendingUpdates: Boolean? = null
): Request<Boolean> = SetWebhook( ) = SetWebhook(
correctWebhookUrl(url), correctWebhookUrl(url),
null, null,
ipAddress, ipAddress,
@ -112,7 +128,7 @@ data class SetWebhook internal constructor(
val allowedUpdates: List<String>? = null, val allowedUpdates: List<String>? = null,
@SerialName(dropPendingUpdatesField) @SerialName(dropPendingUpdatesField)
val dropPendingUpdates: Boolean? = null val dropPendingUpdates: Boolean? = null
) : DataRequest<Boolean> { ) : SetWebhookRequest(), DataRequest<Boolean> {
override fun method(): String = "setWebhook" override fun method(): String = "setWebhook"
override val resultDeserializer: DeserializationStrategy<Boolean> override val resultDeserializer: DeserializationStrategy<Boolean>
get() = Boolean.serializer() get() = Boolean.serializer()

View File

@ -5,10 +5,7 @@ import dev.inmo.micro_utils.coroutines.safely
import dev.inmo.tgbotapi.bot.RequestsExecutor import dev.inmo.tgbotapi.bot.RequestsExecutor
import dev.inmo.tgbotapi.extensions.utils.nonstrictJsonFormat import dev.inmo.tgbotapi.extensions.utils.nonstrictJsonFormat
import dev.inmo.tgbotapi.extensions.utils.updates.flowsUpdatesFilter import dev.inmo.tgbotapi.extensions.utils.updates.flowsUpdatesFilter
import dev.inmo.tgbotapi.requests.abstracts.MultipartFile import dev.inmo.tgbotapi.requests.webhook.SetWebhookRequest
import dev.inmo.tgbotapi.requests.abstracts.Request
import dev.inmo.tgbotapi.requests.send.media.base.MultipartRequestImpl
import dev.inmo.tgbotapi.requests.webhook.SetWebhook
import dev.inmo.tgbotapi.types.update.abstracts.Update import dev.inmo.tgbotapi.types.update.abstracts.Update
import dev.inmo.tgbotapi.types.update.abstracts.UpdateDeserializationStrategy import dev.inmo.tgbotapi.types.update.abstracts.UpdateDeserializationStrategy
import dev.inmo.tgbotapi.updateshandlers.* import dev.inmo.tgbotapi.updateshandlers.*
@ -67,7 +64,7 @@ fun Route.includeWebhookHandlingInRouteWithFlows(
) )
/** /**
* Setting up ktor server, set webhook info via [SetWebhook] request. * Setting up ktor server
* *
* @param listenPort port which will be listen by bot * @param listenPort port which will be listen by bot
* @param listenRoute address to listen by bot. If null - will be set up in root of host * @param listenRoute address to listen by bot. If null - will be set up in root of host
@ -119,27 +116,8 @@ fun startListenWebhooks(
} }
} }
private suspend fun RequestsExecutor.internalSetWebhookInfoAndStartListenWebhooks(
listenPort: Int,
engineFactory: ApplicationEngineFactory<*, *>,
setWebhookRequest: Request<Boolean>,
exceptionsHandler: ExceptionHandler<Unit> = {},
listenHost: String = "0.0.0.0",
listenRoute: String? = null,
privateKeyConfig: WebhookPrivateKeyConfig? = null,
scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()),
block: UpdateReceiver<Update>
): ApplicationEngine {
return try {
execute(setWebhookRequest)
startListenWebhooks(listenPort, engineFactory, exceptionsHandler, listenHost, listenRoute, privateKeyConfig, scope, block)
} catch (e: Exception) {
throw e
}
}
/** /**
* Setting up ktor server, set webhook info via [SetWebhook] request. * Setting up ktor server, set webhook info via [SetWebhookRequest] request.
* *
* @param listenPort port which will be listen by bot * @param listenPort port which will be listen by bot
* @param listenRoute address to listen by bot * @param listenRoute address to listen by bot
@ -153,55 +131,16 @@ private suspend fun RequestsExecutor.internalSetWebhookInfoAndStartListenWebhook
suspend fun RequestsExecutor.setWebhookInfoAndStartListenWebhooks( suspend fun RequestsExecutor.setWebhookInfoAndStartListenWebhooks(
listenPort: Int, listenPort: Int,
engineFactory: ApplicationEngineFactory<*, *>, engineFactory: ApplicationEngineFactory<*, *>,
setWebhookRequest: SetWebhook, setWebhookRequest: SetWebhookRequest,
exceptionsHandler: ExceptionHandler<Unit> = {}, exceptionsHandler: ExceptionHandler<Unit> = {},
listenHost: String = "0.0.0.0", listenHost: String = "0.0.0.0",
listenRoute: String = "/", listenRoute: String = "/",
privateKeyConfig: WebhookPrivateKeyConfig? = null, privateKeyConfig: WebhookPrivateKeyConfig? = null,
scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()), scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()),
block: UpdateReceiver<Update> block: UpdateReceiver<Update>
): ApplicationEngine = internalSetWebhookInfoAndStartListenWebhooks( ): ApplicationEngine = try {
listenPort, execute(setWebhookRequest)
engineFactory, startListenWebhooks(listenPort, engineFactory, exceptionsHandler, listenHost, listenRoute, privateKeyConfig, scope, block)
setWebhookRequest as Request<Boolean>, } catch (e: Exception) {
exceptionsHandler, throw e
listenHost, }
listenRoute,
privateKeyConfig,
scope,
block
)
/**
* Setting up ktor server, set webhook info via [SetWebhook] request.
*
* @param listenPort port which will be listen by bot
* @param listenRoute address to listen by bot
* @param scope Scope which will be used for
*
* @see dev.inmo.tgbotapi.updateshandlers.FlowsUpdatesFilter
* @see UpdatesFilter
* @see UpdatesFilter.asUpdateReceiver
*/
@Suppress("unused")
suspend fun RequestsExecutor.setWebhookInfoAndStartListenWebhooks(
listenPort: Int,
engineFactory: ApplicationEngineFactory<*, *>,
setWebhookRequest: MultipartRequestImpl<SetWebhook, Map<String, MultipartFile>, Boolean>,
exceptionsHandler: ExceptionHandler<Unit> = {},
listenHost: String = "0.0.0.0",
listenRoute: String? = null,
privateKeyConfig: WebhookPrivateKeyConfig? = null,
scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()),
block: UpdateReceiver<Update>
): ApplicationEngine = internalSetWebhookInfoAndStartListenWebhooks(
listenPort,
engineFactory,
setWebhookRequest as Request<Boolean>,
exceptionsHandler,
listenHost,
listenRoute,
privateKeyConfig,
scope,
block
)