1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-25 19:48:43 +00:00

clean up in webhooks

This commit is contained in:
InsanusMokrassar 2020-05-15 00:28:16 +06:00
parent ea224fd765
commit a1788e35b2
3 changed files with 25 additions and 127 deletions

View File

@ -53,7 +53,7 @@
* `TelegramBotAPI`: * `TelegramBotAPI`:
* `UpdateDeserializationStrategy` is publicly available now * `UpdateDeserializationStrategy` is publicly available now
* All `setWebhook` extensions was marked as deprecated and replaced into `TelegramBotAPI-extensions-utils` * All `setWebhook` extensions was marked as deprecated, renamed and replaced into `TelegramBotAPI-extensions-utils`
* Typealias `ExceptionHandler` was added - it will be used for `handleSafely` * Typealias `ExceptionHandler` was added - it will be used for `handleSafely`
* `TelegramBotAPI-extensions-api`: * `TelegramBotAPI-extensions-api`:
* Long Polling extensions now are deprecated in this project. It was replaced into `TelegramBotAPI-extensions-utils` * Long Polling extensions now are deprecated in this project. It was replaced into `TelegramBotAPI-extensions-utils`
@ -61,10 +61,10 @@
* Extension `toTelegramUpdate` was added * Extension `toTelegramUpdate` was added
* Long Polling extensions were added * Long Polling extensions were added
* Updates utils were added * Updates utils were added
* New extensions `setWebhook` and `includeWebhookInRoute` was added * New extensions `startListenWebhooks`, `setWebhookInfoAndStartListenWebhooks` and `includeWebhookHandlingInRoute` was added
* New extension `CoroutineScope#updateHandlerWithMediaGroupsAdaptation` was added * New extension `CoroutineScope#updateHandlerWithMediaGroupsAdaptation` was added
* `TelegramBotAPI-all`: * `TelegramBotAPI-all`:
* Project is created * Project was created
### 0.27.2 ### 0.27.2

View File

@ -75,7 +75,7 @@ fun RequestsExecutor.startGettingOfUpdatesByLongPolling(
fun RequestsExecutor.startGettingFlowsUpdatesByLongPolling( fun RequestsExecutor.startGettingFlowsUpdatesByLongPolling(
timeoutSeconds: Seconds = 30, timeoutSeconds: Seconds = 30,
scope: CoroutineScope = CoroutineScope(Dispatchers.Default), scope: CoroutineScope = CoroutineScope(Dispatchers.Default),
exceptionsHandler: (suspend (Exception) -> Unit)? = null, exceptionsHandler: ExceptionHandler<Unit>? = null,
flowsUpdatesFilterUpdatesKeeperCount: Int = 64, flowsUpdatesFilterUpdatesKeeperCount: Int = 64,
flowUpdatesPreset: FlowsUpdatesFilter.() -> Unit = {} flowUpdatesPreset: FlowsUpdatesFilter.() -> Unit = {}
): FlowsUpdatesFilter = FlowsUpdatesFilter(flowsUpdatesFilterUpdatesKeeperCount).apply { ): FlowsUpdatesFilter = FlowsUpdatesFilter(flowsUpdatesFilterUpdatesKeeperCount).apply {
@ -86,7 +86,7 @@ fun RequestsExecutor.startGettingFlowsUpdatesByLongPolling(
fun RequestsExecutor.startGettingOfUpdatesByLongPolling( fun RequestsExecutor.startGettingOfUpdatesByLongPolling(
updatesFilter: UpdatesFilter, updatesFilter: UpdatesFilter,
timeoutSeconds: Seconds = 30, timeoutSeconds: Seconds = 30,
exceptionsHandler: (suspend (Exception) -> Unit)? = null, exceptionsHandler: ExceptionHandler<Unit>? = null,
scope: CoroutineScope = CoroutineScope(Dispatchers.Default) scope: CoroutineScope = CoroutineScope(Dispatchers.Default)
): Job = startGettingOfUpdatesByLongPolling( ): Job = startGettingOfUpdatesByLongPolling(
timeoutSeconds, timeoutSeconds,
@ -113,7 +113,7 @@ fun RequestsExecutor.startGettingOfUpdatesByLongPolling(
pollCallback: UpdateReceiver<PollUpdate>? = null, pollCallback: UpdateReceiver<PollUpdate>? = null,
pollAnswerCallback: UpdateReceiver<PollAnswerUpdate>? = null, pollAnswerCallback: UpdateReceiver<PollAnswerUpdate>? = null,
timeoutSeconds: Seconds = 30, timeoutSeconds: Seconds = 30,
exceptionsHandler: (suspend (Exception) -> Unit)? = null, exceptionsHandler: ExceptionHandler<Unit>? = null,
scope: CoroutineScope = GlobalScope scope: CoroutineScope = GlobalScope
): Job { ): Job {
return startGettingOfUpdatesByLongPolling( return startGettingOfUpdatesByLongPolling(
@ -155,7 +155,7 @@ fun RequestsExecutor.startGettingOfUpdatesByLongPolling(
pollCallback: UpdateReceiver<PollUpdate>? = null, pollCallback: UpdateReceiver<PollUpdate>? = null,
pollAnswerCallback: UpdateReceiver<PollAnswerUpdate>? = null, pollAnswerCallback: UpdateReceiver<PollAnswerUpdate>? = null,
timeoutSeconds: Seconds = 30, timeoutSeconds: Seconds = 30,
exceptionsHandler: (suspend (Exception) -> Unit)? = null, exceptionsHandler: ExceptionHandler<Unit>? = null,
scope: CoroutineScope = CoroutineScope(Dispatchers.Default) scope: CoroutineScope = CoroutineScope(Dispatchers.Default)
): Job = startGettingOfUpdatesByLongPolling( ): Job = startGettingOfUpdatesByLongPolling(
messageCallback = messageCallback, messageCallback = messageCallback,

View File

@ -2,14 +2,13 @@ package com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.updates.retr
import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor
import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.nonstrictJsonFormat import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.nonstrictJsonFormat
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.InputFile
import com.github.insanusmokrassar.TelegramBotAPI.requests.webhook.SetWebhook import com.github.insanusmokrassar.TelegramBotAPI.requests.webhook.SetWebhook
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.UpdateDeserializationStrategy import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.UpdateDeserializationStrategy
import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.UpdateReceiver import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.UpdateReceiver
import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.UpdatesFilter import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.UpdatesFilter
import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.webhook.WebhookPrivateKeyConfig import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.webhook.WebhookPrivateKeyConfig
import com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.executeAsync import com.github.insanusmokrassar.TelegramBotAPI.utils.ExceptionHandler
import com.github.insanusmokrassar.TelegramBotAPI.utils.handleSafely import com.github.insanusmokrassar.TelegramBotAPI.utils.handleSafely
import io.ktor.application.call import io.ktor.application.call
import io.ktor.request.receiveText import io.ktor.request.receiveText
@ -31,9 +30,9 @@ import java.util.concurrent.Executors
* @see UpdatesFilter * @see UpdatesFilter
* @see UpdatesFilter.asUpdateReceiver * @see UpdatesFilter.asUpdateReceiver
*/ */
fun Route.includeWebhookInRoute( fun Route.includeWebhookHandlingInRoute(
scope: CoroutineScope, scope: CoroutineScope,
exceptionsHandler: (suspend (Exception) -> Unit)? = null, exceptionsHandler: ExceptionHandler<Unit>? = null,
block: UpdateReceiver<Update> block: UpdateReceiver<Update>
) { ) {
val transformer = scope.updateHandlerWithMediaGroupsAdaptation(block) val transformer = scope.updateHandlerWithMediaGroupsAdaptation(block)
@ -56,7 +55,7 @@ fun Route.includeWebhookInRoute(
/** /**
* Setting up ktor server, set webhook info via [SetWebhook] request. * Setting up ktor server, set webhook info via [SetWebhook] request.
* *
* @param port 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
* @param scope Scope which will be used for * @param scope Scope which will be used for
* @param privateKeyConfig If configured - server will be created with [sslConnector]. [connector] will be used otherwise * @param privateKeyConfig If configured - server will be created with [sslConnector]. [connector] will be used otherwise
@ -65,14 +64,14 @@ fun Route.includeWebhookInRoute(
* @see UpdatesFilter * @see UpdatesFilter
* @see UpdatesFilter.asUpdateReceiver * @see UpdatesFilter.asUpdateReceiver
*/ */
fun setWebhook( fun startListenWebhooks(
port: Int, listenPort: Int,
engineFactory: ApplicationEngineFactory<*, *>, engineFactory: ApplicationEngineFactory<*, *>,
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()),
exceptionsHandler: (suspend (Exception) -> Unit)? = null,
block: UpdateReceiver<Update> block: UpdateReceiver<Update>
): ApplicationEngine { ): ApplicationEngine {
lateinit var engine: ApplicationEngine lateinit var engine: ApplicationEngine
@ -81,7 +80,7 @@ fun setWebhook(
module { module {
routing { routing {
route(listenRoute) { route(listenRoute) {
includeWebhookInRoute(scope, exceptionsHandler, block) includeWebhookHandlingInRoute(scope, exceptionsHandler, block)
} }
} }
} }
@ -93,11 +92,11 @@ fun setWebhook(
privateKeyConfig::aliasPassword privateKeyConfig::aliasPassword
) { ) {
host = listenHost host = listenHost
this.port = port this.port = listenPort
} }
} ?: connector { } ?: connector {
host = listenHost host = listenHost
this.port = port this.port = listenPort
} }
} }
@ -110,52 +109,29 @@ fun setWebhook(
/** /**
* Setting up ktor server, set webhook info via [SetWebhook] request. * Setting up ktor server, set webhook info via [SetWebhook] request.
* *
* @param url URL of webhook WITHOUT including of [port] * @param listenPort port which will be listen by bot
* @param port port which will be listen by bot
* @param listenRoute address to listen by bot * @param listenRoute address to listen by bot
* @param certificate [com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.MultipartFile] or [com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.FileId]
* which will be used by telegram to send encrypted messages
* @param scope Scope which will be used for * @param scope Scope which will be used for
* *
* @see com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.FlowsUpdatesFilter * @see com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.FlowsUpdatesFilter
* @see UpdatesFilter * @see UpdatesFilter
* @see UpdatesFilter.asUpdateReceiver * @see UpdatesFilter.asUpdateReceiver
*/ */
suspend fun RequestsExecutor.setWebhook( @Suppress("unused")
url: String, suspend fun RequestsExecutor.setWebhookInfoAndStartListenWebhooks(
port: Int, listenPort: Int,
engineFactory: ApplicationEngineFactory<*, *>, engineFactory: ApplicationEngineFactory<*, *>,
setWebhookRequest: SetWebhook,
exceptionsHandler: ExceptionHandler<Unit> = {},
listenHost: String = "0.0.0.0", listenHost: String = "0.0.0.0",
listenRoute: String = "/", listenRoute: String = "/",
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,
maxAllowedConnections: Int? = null,
exceptionsHandler: (suspend (Exception) -> Unit)? = null,
block: UpdateReceiver<Update> block: UpdateReceiver<Update>
): Job { ): Job {
val executeDeferred = certificate ?.let {
executeAsync(
SetWebhook(
url,
certificate,
maxAllowedConnections,
allowedUpdates
)
)
} ?: executeAsync(
SetWebhook(
url,
maxAllowedConnections,
allowedUpdates
)
)
return try { return try {
executeDeferred.await() execute(setWebhookRequest)
val engine = setWebhook(port, engineFactory, listenHost, listenRoute, privateKeyConfig, scope, exceptionsHandler, block) val engine = startListenWebhooks(listenPort, engineFactory, exceptionsHandler, listenHost, listenRoute, privateKeyConfig, scope, block)
scope.launch { scope.launch {
engine.environment.parentCoroutineContext[Job] ?.join() engine.environment.parentCoroutineContext[Job] ?.join()
engine.stop(1000, 5000) engine.stop(1000, 5000)
@ -164,81 +140,3 @@ suspend fun RequestsExecutor.setWebhook(
throw e throw e
} }
} }
suspend fun RequestsExecutor.setWebhook(
url: String,
port: Int,
engineFactory: ApplicationEngineFactory<*, *>,
listenHost: String = "0.0.0.0",
listenRoute: String = "/",
certificate: InputFile? = null,
privateKeyConfig: WebhookPrivateKeyConfig? = null,
scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()),
allowedUpdates: List<String>? = null,
maxAllowedConnections: Int? = null,
block: UpdateReceiver<Update>
) = setWebhook(
url,
port,
engineFactory,
listenHost,
listenRoute,
certificate,
privateKeyConfig,
scope,
allowedUpdates,
maxAllowedConnections,
null,
block
)
@Suppress("unused")
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,
block: UpdateReceiver<Update>
) = setWebhook(
url,
port,
engineFactory,
certificate ?.let { "0.0.0.0" } ?: "localhost",
"/",
certificate,
privateKeyConfig,
scope,
allowedUpdates,
maxAllowedConnections,
block
)
@Suppress("unused")
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,
listenHost: String = certificate ?.let { "0.0.0.0" } ?: "localhost",
listenRoute: String = "/"
): Job = setWebhook(
url,
port,
engineFactory,
listenHost,
listenRoute,
certificate,
privateKeyConfig,
scope,
filter.allowedUpdates,
maxAllowedConnections,
filter.asUpdateReceiver
)