add additional signatures for setWebhookInfoAndStartListenWebhooks

This commit is contained in:
InsanusMokrassar 2020-05-15 18:35:16 +06:00
parent 735ed9fd86
commit efc2681da8
3 changed files with 92 additions and 26 deletions

View File

@ -55,6 +55,7 @@
* `UpdateDeserializationStrategy` is publicly available now * `UpdateDeserializationStrategy` is publicly available now
* All `setWebhook` extensions was marked as deprecated, renamed 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`
* `SetWebhook` factories signatures was changed (backward compatibility was not broken)
* `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`
* `TelegramBotAPI-extensions-utils`: * `TelegramBotAPI-extensions-utils`:

View File

@ -2,6 +2,9 @@ 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.MultipartFile
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.Request
import com.github.insanusmokrassar.TelegramBotAPI.requests.send.media.base.MultipartRequestImpl
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
@ -106,6 +109,29 @@ fun startListenWebhooks(
return engine return engine
} }
internal suspend fun RequestsExecutor.internalSetWebhookInfoAndStartListenWebhooks(
listenPort: Int,
engineFactory: ApplicationEngineFactory<*, *>,
setWebhookRequest: Request<Boolean>,
exceptionsHandler: ExceptionHandler<Unit> = {},
listenHost: String = "0.0.0.0",
listenRoute: String = "/",
privateKeyConfig: WebhookPrivateKeyConfig? = null,
scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()),
block: UpdateReceiver<Update>
): Job {
return try {
execute(setWebhookRequest)
val engine = startListenWebhooks(listenPort, engineFactory, exceptionsHandler, listenHost, listenRoute, privateKeyConfig, scope, block)
scope.launch {
engine.environment.parentCoroutineContext[Job] ?.join()
engine.stop(1000, 5000)
}
} catch (e: Exception) {
throw e
}
}
/** /**
* Setting up ktor server, set webhook info via [SetWebhook] request. * Setting up ktor server, set webhook info via [SetWebhook] request.
* *
@ -128,15 +154,48 @@ suspend fun RequestsExecutor.setWebhookInfoAndStartListenWebhooks(
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>
): Job { ): Job = internalSetWebhookInfoAndStartListenWebhooks(
return try { listenPort,
execute(setWebhookRequest) engineFactory,
val engine = startListenWebhooks(listenPort, engineFactory, exceptionsHandler, listenHost, listenRoute, privateKeyConfig, scope, block) setWebhookRequest as Request<Boolean>,
scope.launch { exceptionsHandler,
engine.environment.parentCoroutineContext[Job] ?.join() listenHost,
engine.stop(1000, 5000) listenRoute,
} privateKeyConfig,
} catch (e: Exception) { scope,
throw e 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 com.github.insanusmokrassar.TelegramBotAPI.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 = "/",
privateKeyConfig: WebhookPrivateKeyConfig? = null,
scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()),
block: UpdateReceiver<Update>
): Job = internalSetWebhookInfoAndStartListenWebhooks(
listenPort,
engineFactory,
setWebhookRequest as Request<Boolean>,
exceptionsHandler,
listenHost,
listenRoute,
privateKeyConfig,
scope,
block
)

View File

@ -9,30 +9,36 @@ import kotlinx.serialization.builtins.serializer
fun SetWebhook( fun SetWebhook(
url: String, url: String,
certificate: InputFile, certificate: MultipartFile,
maxAllowedConnections: Int? = null, maxAllowedConnections: Int? = null,
allowedUpdates: List<String>? = null allowedUpdates: List<String>? = null
) : Request<Boolean> { ): MultipartRequestImpl<SetWebhook, Map<String, MultipartFile>, Boolean> = MultipartRequestImpl(
val data = SetWebhook( SetWebhook(
url, url,
(certificate as? FileId) ?.fileId, null,
maxAllowedConnections, maxAllowedConnections,
allowedUpdates allowedUpdates
) ),
return when (certificate) { mapOf(certificateField to certificate)
is FileId -> data )
is MultipartFile -> MultipartRequestImpl(
data, fun SetWebhook(
mapOf(certificateField to certificate) url: String,
) certificate: FileId,
} maxAllowedConnections: Int? = null,
} allowedUpdates: List<String>? = null
): SetWebhook = SetWebhook(
url,
certificate.fileId,
maxAllowedConnections,
allowedUpdates
)
fun SetWebhook( fun SetWebhook(
url: String, url: String,
maxAllowedConnections: Int? = null, maxAllowedConnections: Int? = null,
allowedUpdates: List<String>? = null allowedUpdates: List<String>? = null
) : Request<Boolean> = SetWebhook( ) = SetWebhook(
url, url,
null, null,
maxAllowedConnections, maxAllowedConnections,