mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2024-11-22 16:23:48 +00:00
webhook fixes
This commit is contained in:
parent
7376eb5b10
commit
9c91980d5d
@ -2,6 +2,13 @@
|
|||||||
|
|
||||||
## 0.30.2
|
## 0.30.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`:
|
||||||
|
@ -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()
|
||||||
|
@ -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
|
|
||||||
)
|
|
||||||
|
Loading…
Reference in New Issue
Block a user