mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2025-12-06 06:15:49 +00:00
small improvements in long polling and behaviour buildr
This commit is contained in:
@@ -14,6 +14,7 @@ import dev.inmo.tgbotapi.types.update.*
|
||||
import dev.inmo.tgbotapi.types.update.abstracts.BaseSentMessageUpdate
|
||||
import dev.inmo.tgbotapi.types.update.abstracts.Update
|
||||
import dev.inmo.tgbotapi.updateshandlers.*
|
||||
import dev.inmo.tgbotapi.utils.DefaultKTgBotAPIKSLog
|
||||
import io.ktor.client.plugins.HttpRequestTimeoutException
|
||||
import io.ktor.utils.io.CancellationException
|
||||
import kotlinx.coroutines.*
|
||||
@@ -33,20 +34,20 @@ fun TelegramBot.longPollingFlow(
|
||||
mediaGroupsDebounceTimeMillis: Long? = 1000L,
|
||||
): Flow<Update> = channelFlow {
|
||||
if (autoDisableWebhooks) {
|
||||
runCatching {
|
||||
runCatchingLogging(logger = Log) {
|
||||
execute(DeleteWebhook())
|
||||
}
|
||||
}
|
||||
|
||||
val contextSafelyExceptionHandler = coroutineContext[ContextSafelyExceptionHandlerKey]
|
||||
val contextToWork = if (contextSafelyExceptionHandler == null || !autoSkipTimeoutExceptions) {
|
||||
val contextSafelyExceptionHandler = coroutineContext[ContextSafelyExceptionHandlerKey] ?.handler ?: defaultSafelyExceptionHandler
|
||||
val contextToWork = if (!autoSkipTimeoutExceptions) {
|
||||
coroutineContext
|
||||
} else {
|
||||
coroutineContext + ContextSafelyExceptionHandler { e ->
|
||||
if (e is HttpRequestTimeoutException || (e is CommonBotException && e.cause is HttpRequestTimeoutException)) {
|
||||
return@ContextSafelyExceptionHandler
|
||||
} else {
|
||||
contextSafelyExceptionHandler.handler(e)
|
||||
contextSafelyExceptionHandler(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -61,7 +62,8 @@ fun TelegramBot.longPollingFlow(
|
||||
send(it)
|
||||
}
|
||||
},
|
||||
mediaGroupsDebounceTimeMillis
|
||||
mediaGroupsDebounceTimeMillis,
|
||||
logger = Log
|
||||
);
|
||||
{ originalUpdates: List<Update> ->
|
||||
originalUpdates.forEach {
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
package dev.inmo.tgbotapi.extensions.utils.updates.retrieving
|
||||
|
||||
import dev.inmo.kslog.common.KSLog
|
||||
import dev.inmo.micro_utils.coroutines.launchLoggingDropExceptions
|
||||
import dev.inmo.micro_utils.coroutines.launchSafelyWithoutExceptions
|
||||
import dev.inmo.tgbotapi.extensions.utils.updates.convertWithMediaGroupUpdates
|
||||
import dev.inmo.tgbotapi.types.message.abstracts.PossiblyMediaGroupMessage
|
||||
import dev.inmo.tgbotapi.types.update.abstracts.BaseMessageUpdate
|
||||
import dev.inmo.tgbotapi.types.update.abstracts.Update
|
||||
import dev.inmo.tgbotapi.updateshandlers.UpdateReceiver
|
||||
import dev.inmo.tgbotapi.utils.DefaultKTgBotAPIKSLog
|
||||
import dev.inmo.tgbotapi.utils.extensions.accumulateByKey
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.channels.Channel
|
||||
@@ -19,7 +22,8 @@ import kotlinx.coroutines.launch
|
||||
*/
|
||||
fun CoroutineScope.updateHandlerWithMediaGroupsAdaptation(
|
||||
output: UpdateReceiver<Update>,
|
||||
debounceTimeMillis: Long = 1000L
|
||||
debounceTimeMillis: Long = 1000L,
|
||||
logger: KSLog = DefaultKTgBotAPIKSLog
|
||||
): UpdateReceiver<Update> {
|
||||
val updatesChannel = Channel<Update>(Channel.UNLIMITED)
|
||||
val mediaGroupChannel = Channel<Pair<String, BaseMessageUpdate>>(Channel.UNLIMITED)
|
||||
@@ -29,7 +33,7 @@ fun CoroutineScope.updateHandlerWithMediaGroupsAdaptation(
|
||||
)
|
||||
|
||||
launch {
|
||||
launchSafelyWithoutExceptions {
|
||||
launchLoggingDropExceptions(logger = logger) {
|
||||
for (update in updatesChannel) {
|
||||
val data = update.data
|
||||
when {
|
||||
@@ -40,7 +44,7 @@ fun CoroutineScope.updateHandlerWithMediaGroupsAdaptation(
|
||||
}
|
||||
}
|
||||
}
|
||||
launchSafelyWithoutExceptions {
|
||||
launchLoggingDropExceptions(logger = logger) {
|
||||
for ((_, mediaGroup) in mediaGroupAccumulatedChannel) {
|
||||
mediaGroup.convertWithMediaGroupUpdates().forEach {
|
||||
output(it)
|
||||
@@ -59,5 +63,6 @@ fun CoroutineScope.updateHandlerWithMediaGroupsAdaptation(
|
||||
* @see UpdateReceiver
|
||||
*/
|
||||
fun CoroutineScope.updateHandlerWithMediaGroupsAdaptation(
|
||||
logger: KSLog = DefaultKTgBotAPIKSLog,
|
||||
output: UpdateReceiver<Update>
|
||||
) = updateHandlerWithMediaGroupsAdaptation(output, 1000L)
|
||||
) = updateHandlerWithMediaGroupsAdaptation(output, 1000L, logger = logger)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package dev.inmo.tgbotapi.extensions.utils.updates.retrieving
|
||||
|
||||
import dev.inmo.kslog.common.KSLog
|
||||
import dev.inmo.micro_utils.coroutines.ExceptionHandler
|
||||
import dev.inmo.micro_utils.coroutines.runCatchingSafely
|
||||
import dev.inmo.tgbotapi.bot.RequestsExecutor
|
||||
@@ -12,6 +13,7 @@ import dev.inmo.tgbotapi.updateshandlers.FlowsUpdatesFilter
|
||||
import dev.inmo.tgbotapi.updateshandlers.UpdateReceiver
|
||||
import dev.inmo.tgbotapi.updateshandlers.UpdatesFilter
|
||||
import dev.inmo.tgbotapi.updateshandlers.webhook.WebhookPrivateKeyConfig
|
||||
import dev.inmo.tgbotapi.utils.DefaultKTgBotAPIKSLog
|
||||
import io.ktor.http.*
|
||||
import io.ktor.server.engine.*
|
||||
import io.ktor.server.request.*
|
||||
@@ -40,9 +42,10 @@ fun Route.includeWebhookHandlingInRoute(
|
||||
scope: CoroutineScope,
|
||||
exceptionsHandler: ExceptionHandler<Unit>? = null,
|
||||
mediaGroupsDebounceTimeMillis: Long = 1000L,
|
||||
logger: KSLog = DefaultKTgBotAPIKSLog,
|
||||
block: UpdateReceiver<Update>,
|
||||
) {
|
||||
val transformer = scope.updateHandlerWithMediaGroupsAdaptation(block, mediaGroupsDebounceTimeMillis)
|
||||
val transformer = scope.updateHandlerWithMediaGroupsAdaptation(block, mediaGroupsDebounceTimeMillis, logger = logger)
|
||||
post {
|
||||
try {
|
||||
runCatchingSafely {
|
||||
@@ -71,11 +74,13 @@ fun Route.includeWebhookHandlingInRouteWithFlows(
|
||||
scope: CoroutineScope,
|
||||
exceptionsHandler: ExceptionHandler<Unit>? = null,
|
||||
mediaGroupsDebounceTimeMillis: Long = 1000L,
|
||||
logger: KSLog = DefaultKTgBotAPIKSLog,
|
||||
block: FlowsUpdatesFilter.() -> Unit,
|
||||
) = includeWebhookHandlingInRoute(
|
||||
scope,
|
||||
exceptionsHandler,
|
||||
mediaGroupsDebounceTimeMillis,
|
||||
logger,
|
||||
flowsUpdatesFilter(block = block).asUpdateReceiver
|
||||
)
|
||||
|
||||
@@ -105,6 +110,7 @@ fun <TEngine : ApplicationEngine, TConfiguration : ApplicationEngine.Configurati
|
||||
mediaGroupsDebounceTimeMillis: Long = 1000L,
|
||||
additionalApplicationEnvironmentConfigurator: ApplicationEnvironmentBuilder.() -> Unit = {},
|
||||
additionalEngineConfigurator: TConfiguration.() -> Unit = {},
|
||||
logger: KSLog = DefaultKTgBotAPIKSLog,
|
||||
block: UpdateReceiver<Update>,
|
||||
): EmbeddedServer<TEngine, TConfiguration> =
|
||||
embeddedServer(
|
||||
@@ -134,12 +140,19 @@ fun <TEngine : ApplicationEngine, TConfiguration : ApplicationEngine.Configurati
|
||||
routing {
|
||||
listenRoute?.also {
|
||||
createRouteFromPath(it).includeWebhookHandlingInRoute(
|
||||
scope,
|
||||
exceptionsHandler,
|
||||
mediaGroupsDebounceTimeMillis,
|
||||
block
|
||||
scope = scope,
|
||||
exceptionsHandler = exceptionsHandler,
|
||||
mediaGroupsDebounceTimeMillis = mediaGroupsDebounceTimeMillis,
|
||||
logger = logger,
|
||||
block = block
|
||||
)
|
||||
} ?: includeWebhookHandlingInRoute(scope, exceptionsHandler, mediaGroupsDebounceTimeMillis, block)
|
||||
} ?: includeWebhookHandlingInRoute(
|
||||
scope = scope,
|
||||
exceptionsHandler = exceptionsHandler,
|
||||
mediaGroupsDebounceTimeMillis = mediaGroupsDebounceTimeMillis,
|
||||
logger = logger,
|
||||
block = block
|
||||
)
|
||||
}
|
||||
}
|
||||
).also {
|
||||
@@ -173,21 +186,23 @@ suspend fun <TEngine : ApplicationEngine, TConfiguration : ApplicationEngine.Con
|
||||
mediaGroupsDebounceTimeMillis: Long = 1000L,
|
||||
additionalApplicationEnvironmentConfigurator: ApplicationEnvironmentBuilder.() -> Unit = {},
|
||||
additionalEngineConfigurator: TConfiguration.() -> Unit = {},
|
||||
logger: KSLog = Log,
|
||||
block: UpdateReceiver<Update>,
|
||||
): EmbeddedServer<TEngine, TConfiguration> = try {
|
||||
execute(setWebhookRequest)
|
||||
startListenWebhooks(
|
||||
listenPort,
|
||||
engineFactory,
|
||||
exceptionsHandler,
|
||||
listenHost,
|
||||
listenRoute,
|
||||
privateKeyConfig,
|
||||
scope,
|
||||
mediaGroupsDebounceTimeMillis,
|
||||
additionalApplicationEnvironmentConfigurator,
|
||||
additionalEngineConfigurator,
|
||||
block
|
||||
listenPort = listenPort,
|
||||
engineFactory = engineFactory,
|
||||
exceptionsHandler = exceptionsHandler,
|
||||
listenHost = listenHost,
|
||||
listenRoute = listenRoute,
|
||||
privateKeyConfig = privateKeyConfig,
|
||||
scope = scope,
|
||||
mediaGroupsDebounceTimeMillis = mediaGroupsDebounceTimeMillis,
|
||||
additionalApplicationEnvironmentConfigurator = additionalApplicationEnvironmentConfigurator,
|
||||
additionalEngineConfigurator = additionalEngineConfigurator,
|
||||
logger = logger,
|
||||
block = block
|
||||
)
|
||||
} catch (e: Exception) {
|
||||
throw e
|
||||
|
||||
Reference in New Issue
Block a user