mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2025-09-03 23:29:33 +00:00
small improvements in long polling and behaviour buildr
This commit is contained in:
@@ -10,7 +10,8 @@
|
|||||||
* `Ktor`: `3.1.1` -> `3.1.3`
|
* `Ktor`: `3.1.1` -> `3.1.3`
|
||||||
* `MicroUtils`: `0.25.3` -> `0.25.7`
|
* `MicroUtils`: `0.25.3` -> `0.25.7`
|
||||||
* `Core`:
|
* `Core`:
|
||||||
* **BREAKING CHANGE** `RequestsExecutor` got property `RequestsExecutor.Log: KSLog?`
|
* **POTENTIALLY BREAKING CHANGE** Long polling has been reworked a bit
|
||||||
|
* **BREAKING CHANGE** `RequestsExecutor` got property `RequestsExecutor.Log: KSLog`
|
||||||
* `BehaviourContext`:
|
* `BehaviourContext`:
|
||||||
* **BREAKING CHANGE** All triggers and waiters become non-suspend functions
|
* **BREAKING CHANGE** All triggers and waiters become non-suspend functions
|
||||||
* **BREAKING CHANGE** Behaviour of counted extensions (commands, data callback queries, etc.) has been changed a bit: now each one will
|
* **BREAKING CHANGE** Behaviour of counted extensions (commands, data callback queries, etc.) has been changed a bit: now each one will
|
||||||
|
@@ -38,9 +38,9 @@ if ((project.hasProperty('SONATYPE_USER') || System.getenv('SONATYPE_USER') != n
|
|||||||
// temporal crutch until legacy tests will be stabled or legacy target will be removed
|
// temporal crutch until legacy tests will be stabled or legacy target will be removed
|
||||||
allprojects {
|
allprojects {
|
||||||
repositories {
|
repositories {
|
||||||
maven { url "https://nexus.inmo.dev/repository/maven-releases/" }
|
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
google()
|
google()
|
||||||
|
// maven { url "https://nexus.inmo.dev/repository/maven-releases/" }
|
||||||
mavenLocal()
|
mavenLocal()
|
||||||
}
|
}
|
||||||
if (it != rootProject.findProject("docs")) {
|
if (it != rootProject.findProject("docs")) {
|
||||||
|
@@ -217,7 +217,7 @@ fun <T, BC : BehaviourContext> BC.launchInNewSubContext(
|
|||||||
updatesUpstreamFlow = updatesUpstreamFlow,
|
updatesUpstreamFlow = updatesUpstreamFlow,
|
||||||
subcontextInitialAction = subcontextInitialAction
|
subcontextInitialAction = subcontextInitialAction
|
||||||
).apply {
|
).apply {
|
||||||
this@apply.launchLoggingDropExceptions(logger = Log ?: KSLog) {
|
this@apply.launchLoggingDropExceptions(logger = Log) {
|
||||||
behaviourContextReceiver()
|
behaviourContextReceiver()
|
||||||
}
|
}
|
||||||
}.coroutineContext.job
|
}.coroutineContext.job
|
||||||
|
@@ -40,7 +40,7 @@ class CombinedSubcontextInitialAction(
|
|||||||
runCatching {
|
runCatching {
|
||||||
invoke(update)
|
invoke(update)
|
||||||
}.onFailure {
|
}.onFailure {
|
||||||
(Log ?: logger).error(it) {
|
Log.error(it) {
|
||||||
"Unable to execute $subaction for update $update. Will try on next round"
|
"Unable to execute $subaction for update $update. Will try on next round"
|
||||||
}
|
}
|
||||||
}.onSuccess {
|
}.onSuccess {
|
||||||
@@ -50,7 +50,7 @@ class CombinedSubcontextInitialAction(
|
|||||||
}
|
}
|
||||||
leftSubActions.removeAll(successSubActions)
|
leftSubActions.removeAll(successSubActions)
|
||||||
if (successSubActions.isEmpty()) {
|
if (successSubActions.isEmpty()) {
|
||||||
(Log ?: logger).error {
|
Log.error {
|
||||||
"Some SubActions have been unable to complete successfully:${leftSubActions.joinToString("\n") { it.toString() }}"
|
"Some SubActions have been unable to complete successfully:${leftSubActions.joinToString("\n") { it.toString() }}"
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
|
@@ -2,6 +2,7 @@ package dev.inmo.tgbotapi.bot
|
|||||||
|
|
||||||
import dev.inmo.kslog.common.KSLog
|
import dev.inmo.kslog.common.KSLog
|
||||||
import dev.inmo.tgbotapi.requests.abstracts.Request
|
import dev.inmo.tgbotapi.requests.abstracts.Request
|
||||||
|
import dev.inmo.tgbotapi.utils.DefaultKTgBotAPIKSLog
|
||||||
import io.ktor.utils.io.core.Closeable
|
import io.ktor.utils.io.core.Closeable
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -12,8 +13,8 @@ import io.ktor.utils.io.core.Closeable
|
|||||||
* @see dev.inmo.tgbotapi.bot.Ktor.KtorRequestsExecutor
|
* @see dev.inmo.tgbotapi.bot.Ktor.KtorRequestsExecutor
|
||||||
*/
|
*/
|
||||||
interface RequestsExecutor : Closeable {
|
interface RequestsExecutor : Closeable {
|
||||||
val Log: KSLog?
|
val Log: KSLog
|
||||||
get() = null
|
get() = DefaultKTgBotAPIKSLog
|
||||||
/**
|
/**
|
||||||
* Unsafe execution of incoming [request]. Can throw almost any exception. So, it is better to use
|
* Unsafe execution of incoming [request]. Can throw almost any exception. So, it is better to use
|
||||||
* something like [dev.inmo.tgbotapi.extensions.utils.shortcuts.executeAsync] or
|
* something like [dev.inmo.tgbotapi.extensions.utils.shortcuts.executeAsync] or
|
||||||
|
@@ -29,7 +29,7 @@ expect class KtorRequestsExecutor internal constructor(
|
|||||||
logger: KSLog,
|
logger: KSLog,
|
||||||
diff: Unit // just a diff property to know where constructor and where calling function with defaults
|
diff: Unit // just a diff property to know where constructor and where calling function with defaults
|
||||||
) : BaseRequestsExecutor {
|
) : BaseRequestsExecutor {
|
||||||
override val Log: KSLog?
|
override val Log: KSLog
|
||||||
override suspend fun <T : Any> execute(request: Request<T>): T
|
override suspend fun <T : Any> execute(request: Request<T>): T
|
||||||
override fun close()
|
override fun close()
|
||||||
}
|
}
|
||||||
|
@@ -30,7 +30,7 @@ class DefaultKtorRequestsExecutor internal constructor(
|
|||||||
private val logger: KSLog,
|
private val logger: KSLog,
|
||||||
diff: Unit
|
diff: Unit
|
||||||
) : BaseRequestsExecutor(telegramAPIUrlsKeeper) {
|
) : BaseRequestsExecutor(telegramAPIUrlsKeeper) {
|
||||||
override val Log: KSLog? = logger
|
override val Log: KSLog = logger
|
||||||
private val callsFactories: List<KtorCallFactory> = callsFactories.run {
|
private val callsFactories: List<KtorCallFactory> = callsFactories.run {
|
||||||
if (!excludeDefaultFactories) {
|
if (!excludeDefaultFactories) {
|
||||||
this@DefaultKtorRequestsExecutor.logger.v { "Installing default factories" }
|
this@DefaultKtorRequestsExecutor.logger.v { "Installing default factories" }
|
||||||
|
@@ -49,7 +49,7 @@ class MultipleClientKtorRequestsExecutor(
|
|||||||
logger: KSLog,
|
logger: KSLog,
|
||||||
clientFactory: () -> HttpClient,
|
clientFactory: () -> HttpClient,
|
||||||
) : BaseRequestsExecutor(telegramAPIUrlsKeeper) {
|
) : BaseRequestsExecutor(telegramAPIUrlsKeeper) {
|
||||||
override val Log: KSLog? = logger
|
override val Log: KSLog = logger
|
||||||
private val requestExecutors = (0 until requestExecutorsCount).map {
|
private val requestExecutors = (0 until requestExecutorsCount).map {
|
||||||
DefaultKtorRequestsExecutor(
|
DefaultKtorRequestsExecutor(
|
||||||
telegramAPIUrlsKeeper,
|
telegramAPIUrlsKeeper,
|
||||||
|
@@ -6,6 +6,7 @@ import dev.inmo.tgbotapi.bot.ktor.telegramBot
|
|||||||
import dev.inmo.tgbotapi.bot.RequestsExecutor
|
import dev.inmo.tgbotapi.bot.RequestsExecutor
|
||||||
import dev.inmo.tgbotapi.bot.TelegramBot
|
import dev.inmo.tgbotapi.bot.TelegramBot
|
||||||
import dev.inmo.tgbotapi.requests.abstracts.Request
|
import dev.inmo.tgbotapi.requests.abstracts.Request
|
||||||
|
import dev.inmo.tgbotapi.utils.DefaultKTgBotAPIKSLog
|
||||||
import dev.inmo.tgbotapi.utils.TelegramAPIUrlsKeeper
|
import dev.inmo.tgbotapi.utils.TelegramAPIUrlsKeeper
|
||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.*
|
||||||
import kotlin.js.JsName
|
import kotlin.js.JsName
|
||||||
@@ -30,8 +31,8 @@ class SimpleMultiServerRequestsExecutor(
|
|||||||
bots.forEach(TelegramBot::close)
|
bots.forEach(TelegramBot::close)
|
||||||
}
|
}
|
||||||
) : RequestsExecutor {
|
) : RequestsExecutor {
|
||||||
override val Log: KSLog?
|
override val Log: KSLog
|
||||||
get() = bots.firstNotNullOfOrNull { it.Log }
|
get() = bots.firstNotNullOfOrNull { it.Log } ?: DefaultKTgBotAPIKSLog
|
||||||
override suspend fun <T : Any> execute(request: Request<T>): T {
|
override suspend fun <T : Any> execute(request: Request<T>): T {
|
||||||
var currentBot = bots.botSelector(-1, null)
|
var currentBot = bots.botSelector(-1, null)
|
||||||
while (currentCoroutineContext().isActive) {
|
while (currentCoroutineContext().isActive) {
|
||||||
|
@@ -3609,20 +3609,21 @@ public final class dev/inmo/tgbotapi/extensions/utils/updates/retrieving/LongPol
|
|||||||
}
|
}
|
||||||
|
|
||||||
public final class dev/inmo/tgbotapi/extensions/utils/updates/retrieving/MediaGroupsIncluderKt {
|
public final class dev/inmo/tgbotapi/extensions/utils/updates/retrieving/MediaGroupsIncluderKt {
|
||||||
public static final fun updateHandlerWithMediaGroupsAdaptation (Lkotlinx/coroutines/CoroutineScope;Lkotlin/jvm/functions/Function2;)Lkotlin/jvm/functions/Function2;
|
public static final fun updateHandlerWithMediaGroupsAdaptation (Lkotlinx/coroutines/CoroutineScope;Ldev/inmo/kslog/common/KSLog;Lkotlin/jvm/functions/Function2;)Lkotlin/jvm/functions/Function2;
|
||||||
public static final fun updateHandlerWithMediaGroupsAdaptation (Lkotlinx/coroutines/CoroutineScope;Lkotlin/jvm/functions/Function2;J)Lkotlin/jvm/functions/Function2;
|
public static final fun updateHandlerWithMediaGroupsAdaptation (Lkotlinx/coroutines/CoroutineScope;Lkotlin/jvm/functions/Function2;JLdev/inmo/kslog/common/KSLog;)Lkotlin/jvm/functions/Function2;
|
||||||
public static synthetic fun updateHandlerWithMediaGroupsAdaptation$default (Lkotlinx/coroutines/CoroutineScope;Lkotlin/jvm/functions/Function2;JILjava/lang/Object;)Lkotlin/jvm/functions/Function2;
|
public static synthetic fun updateHandlerWithMediaGroupsAdaptation$default (Lkotlinx/coroutines/CoroutineScope;Ldev/inmo/kslog/common/KSLog;Lkotlin/jvm/functions/Function2;ILjava/lang/Object;)Lkotlin/jvm/functions/Function2;
|
||||||
|
public static synthetic fun updateHandlerWithMediaGroupsAdaptation$default (Lkotlinx/coroutines/CoroutineScope;Lkotlin/jvm/functions/Function2;JLdev/inmo/kslog/common/KSLog;ILjava/lang/Object;)Lkotlin/jvm/functions/Function2;
|
||||||
}
|
}
|
||||||
|
|
||||||
public final class dev/inmo/tgbotapi/extensions/utils/updates/retrieving/WebhookKt {
|
public final class dev/inmo/tgbotapi/extensions/utils/updates/retrieving/WebhookKt {
|
||||||
public static final fun includeWebhookHandlingInRoute (Lio/ktor/server/routing/Route;Lkotlinx/coroutines/CoroutineScope;Lkotlin/jvm/functions/Function2;JLkotlin/jvm/functions/Function2;)V
|
public static final fun includeWebhookHandlingInRoute (Lio/ktor/server/routing/Route;Lkotlinx/coroutines/CoroutineScope;Lkotlin/jvm/functions/Function2;JLdev/inmo/kslog/common/KSLog;Lkotlin/jvm/functions/Function2;)V
|
||||||
public static synthetic fun includeWebhookHandlingInRoute$default (Lio/ktor/server/routing/Route;Lkotlinx/coroutines/CoroutineScope;Lkotlin/jvm/functions/Function2;JLkotlin/jvm/functions/Function2;ILjava/lang/Object;)V
|
public static synthetic fun includeWebhookHandlingInRoute$default (Lio/ktor/server/routing/Route;Lkotlinx/coroutines/CoroutineScope;Lkotlin/jvm/functions/Function2;JLdev/inmo/kslog/common/KSLog;Lkotlin/jvm/functions/Function2;ILjava/lang/Object;)V
|
||||||
public static final fun includeWebhookHandlingInRouteWithFlows (Lio/ktor/server/routing/Route;Lkotlinx/coroutines/CoroutineScope;Lkotlin/jvm/functions/Function2;JLkotlin/jvm/functions/Function1;)V
|
public static final fun includeWebhookHandlingInRouteWithFlows (Lio/ktor/server/routing/Route;Lkotlinx/coroutines/CoroutineScope;Lkotlin/jvm/functions/Function2;JLdev/inmo/kslog/common/KSLog;Lkotlin/jvm/functions/Function1;)V
|
||||||
public static synthetic fun includeWebhookHandlingInRouteWithFlows$default (Lio/ktor/server/routing/Route;Lkotlinx/coroutines/CoroutineScope;Lkotlin/jvm/functions/Function2;JLkotlin/jvm/functions/Function1;ILjava/lang/Object;)V
|
public static synthetic fun includeWebhookHandlingInRouteWithFlows$default (Lio/ktor/server/routing/Route;Lkotlinx/coroutines/CoroutineScope;Lkotlin/jvm/functions/Function2;JLdev/inmo/kslog/common/KSLog;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)V
|
||||||
public static final fun setWebhookInfoAndStartListenWebhooks (Ldev/inmo/tgbotapi/bot/RequestsExecutor;ILio/ktor/server/engine/ApplicationEngineFactory;Ldev/inmo/tgbotapi/requests/webhook/SetWebhookRequest;Lkotlin/jvm/functions/Function2;Ljava/lang/String;Ljava/lang/String;Ldev/inmo/tgbotapi/updateshandlers/webhook/WebhookPrivateKeyConfig;Lkotlinx/coroutines/CoroutineScope;JLkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
|
public static final fun setWebhookInfoAndStartListenWebhooks (Ldev/inmo/tgbotapi/bot/RequestsExecutor;ILio/ktor/server/engine/ApplicationEngineFactory;Ldev/inmo/tgbotapi/requests/webhook/SetWebhookRequest;Lkotlin/jvm/functions/Function2;Ljava/lang/String;Ljava/lang/String;Ldev/inmo/tgbotapi/updateshandlers/webhook/WebhookPrivateKeyConfig;Lkotlinx/coroutines/CoroutineScope;JLkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Ldev/inmo/kslog/common/KSLog;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
|
||||||
public static synthetic fun setWebhookInfoAndStartListenWebhooks$default (Ldev/inmo/tgbotapi/bot/RequestsExecutor;ILio/ktor/server/engine/ApplicationEngineFactory;Ldev/inmo/tgbotapi/requests/webhook/SetWebhookRequest;Lkotlin/jvm/functions/Function2;Ljava/lang/String;Ljava/lang/String;Ldev/inmo/tgbotapi/updateshandlers/webhook/WebhookPrivateKeyConfig;Lkotlinx/coroutines/CoroutineScope;JLkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
|
public static synthetic fun setWebhookInfoAndStartListenWebhooks$default (Ldev/inmo/tgbotapi/bot/RequestsExecutor;ILio/ktor/server/engine/ApplicationEngineFactory;Ldev/inmo/tgbotapi/requests/webhook/SetWebhookRequest;Lkotlin/jvm/functions/Function2;Ljava/lang/String;Ljava/lang/String;Ldev/inmo/tgbotapi/updateshandlers/webhook/WebhookPrivateKeyConfig;Lkotlinx/coroutines/CoroutineScope;JLkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Ldev/inmo/kslog/common/KSLog;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
|
||||||
public static final fun startListenWebhooks (ILio/ktor/server/engine/ApplicationEngineFactory;Lkotlin/jvm/functions/Function2;Ljava/lang/String;Ljava/lang/String;Ldev/inmo/tgbotapi/updateshandlers/webhook/WebhookPrivateKeyConfig;Lkotlinx/coroutines/CoroutineScope;JLkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Lio/ktor/server/engine/EmbeddedServer;
|
public static final fun startListenWebhooks (ILio/ktor/server/engine/ApplicationEngineFactory;Lkotlin/jvm/functions/Function2;Ljava/lang/String;Ljava/lang/String;Ldev/inmo/tgbotapi/updateshandlers/webhook/WebhookPrivateKeyConfig;Lkotlinx/coroutines/CoroutineScope;JLkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Ldev/inmo/kslog/common/KSLog;Lkotlin/jvm/functions/Function2;)Lio/ktor/server/engine/EmbeddedServer;
|
||||||
public static synthetic fun startListenWebhooks$default (ILio/ktor/server/engine/ApplicationEngineFactory;Lkotlin/jvm/functions/Function2;Ljava/lang/String;Ljava/lang/String;Ldev/inmo/tgbotapi/updateshandlers/webhook/WebhookPrivateKeyConfig;Lkotlinx/coroutines/CoroutineScope;JLkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;ILjava/lang/Object;)Lio/ktor/server/engine/EmbeddedServer;
|
public static synthetic fun startListenWebhooks$default (ILio/ktor/server/engine/ApplicationEngineFactory;Lkotlin/jvm/functions/Function2;Ljava/lang/String;Ljava/lang/String;Ldev/inmo/tgbotapi/updateshandlers/webhook/WebhookPrivateKeyConfig;Lkotlinx/coroutines/CoroutineScope;JLkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Ldev/inmo/kslog/common/KSLog;Lkotlin/jvm/functions/Function2;ILjava/lang/Object;)Lio/ktor/server/engine/EmbeddedServer;
|
||||||
}
|
}
|
||||||
|
|
||||||
public final class dev/inmo/tgbotapi/types/files/PathedFileAsStreamKt {
|
public final class dev/inmo/tgbotapi/types/files/PathedFileAsStreamKt {
|
||||||
|
@@ -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.BaseSentMessageUpdate
|
||||||
import dev.inmo.tgbotapi.types.update.abstracts.Update
|
import dev.inmo.tgbotapi.types.update.abstracts.Update
|
||||||
import dev.inmo.tgbotapi.updateshandlers.*
|
import dev.inmo.tgbotapi.updateshandlers.*
|
||||||
|
import dev.inmo.tgbotapi.utils.DefaultKTgBotAPIKSLog
|
||||||
import io.ktor.client.plugins.HttpRequestTimeoutException
|
import io.ktor.client.plugins.HttpRequestTimeoutException
|
||||||
import io.ktor.utils.io.CancellationException
|
import io.ktor.utils.io.CancellationException
|
||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.*
|
||||||
@@ -33,20 +34,20 @@ fun TelegramBot.longPollingFlow(
|
|||||||
mediaGroupsDebounceTimeMillis: Long? = 1000L,
|
mediaGroupsDebounceTimeMillis: Long? = 1000L,
|
||||||
): Flow<Update> = channelFlow {
|
): Flow<Update> = channelFlow {
|
||||||
if (autoDisableWebhooks) {
|
if (autoDisableWebhooks) {
|
||||||
runCatching {
|
runCatchingLogging(logger = Log) {
|
||||||
execute(DeleteWebhook())
|
execute(DeleteWebhook())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val contextSafelyExceptionHandler = coroutineContext[ContextSafelyExceptionHandlerKey]
|
val contextSafelyExceptionHandler = coroutineContext[ContextSafelyExceptionHandlerKey] ?.handler ?: defaultSafelyExceptionHandler
|
||||||
val contextToWork = if (contextSafelyExceptionHandler == null || !autoSkipTimeoutExceptions) {
|
val contextToWork = if (!autoSkipTimeoutExceptions) {
|
||||||
coroutineContext
|
coroutineContext
|
||||||
} else {
|
} else {
|
||||||
coroutineContext + ContextSafelyExceptionHandler { e ->
|
coroutineContext + ContextSafelyExceptionHandler { e ->
|
||||||
if (e is HttpRequestTimeoutException || (e is CommonBotException && e.cause is HttpRequestTimeoutException)) {
|
if (e is HttpRequestTimeoutException || (e is CommonBotException && e.cause is HttpRequestTimeoutException)) {
|
||||||
return@ContextSafelyExceptionHandler
|
return@ContextSafelyExceptionHandler
|
||||||
} else {
|
} else {
|
||||||
contextSafelyExceptionHandler.handler(e)
|
contextSafelyExceptionHandler(e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -61,7 +62,8 @@ fun TelegramBot.longPollingFlow(
|
|||||||
send(it)
|
send(it)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
mediaGroupsDebounceTimeMillis
|
mediaGroupsDebounceTimeMillis,
|
||||||
|
logger = Log
|
||||||
);
|
);
|
||||||
{ originalUpdates: List<Update> ->
|
{ originalUpdates: List<Update> ->
|
||||||
originalUpdates.forEach {
|
originalUpdates.forEach {
|
||||||
|
@@ -1,11 +1,14 @@
|
|||||||
package dev.inmo.tgbotapi.extensions.utils.updates.retrieving
|
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.micro_utils.coroutines.launchSafelyWithoutExceptions
|
||||||
import dev.inmo.tgbotapi.extensions.utils.updates.convertWithMediaGroupUpdates
|
import dev.inmo.tgbotapi.extensions.utils.updates.convertWithMediaGroupUpdates
|
||||||
import dev.inmo.tgbotapi.types.message.abstracts.PossiblyMediaGroupMessage
|
import dev.inmo.tgbotapi.types.message.abstracts.PossiblyMediaGroupMessage
|
||||||
import dev.inmo.tgbotapi.types.update.abstracts.BaseMessageUpdate
|
import dev.inmo.tgbotapi.types.update.abstracts.BaseMessageUpdate
|
||||||
import dev.inmo.tgbotapi.types.update.abstracts.Update
|
import dev.inmo.tgbotapi.types.update.abstracts.Update
|
||||||
import dev.inmo.tgbotapi.updateshandlers.UpdateReceiver
|
import dev.inmo.tgbotapi.updateshandlers.UpdateReceiver
|
||||||
|
import dev.inmo.tgbotapi.utils.DefaultKTgBotAPIKSLog
|
||||||
import dev.inmo.tgbotapi.utils.extensions.accumulateByKey
|
import dev.inmo.tgbotapi.utils.extensions.accumulateByKey
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.channels.Channel
|
import kotlinx.coroutines.channels.Channel
|
||||||
@@ -19,7 +22,8 @@ import kotlinx.coroutines.launch
|
|||||||
*/
|
*/
|
||||||
fun CoroutineScope.updateHandlerWithMediaGroupsAdaptation(
|
fun CoroutineScope.updateHandlerWithMediaGroupsAdaptation(
|
||||||
output: UpdateReceiver<Update>,
|
output: UpdateReceiver<Update>,
|
||||||
debounceTimeMillis: Long = 1000L
|
debounceTimeMillis: Long = 1000L,
|
||||||
|
logger: KSLog = DefaultKTgBotAPIKSLog
|
||||||
): UpdateReceiver<Update> {
|
): UpdateReceiver<Update> {
|
||||||
val updatesChannel = Channel<Update>(Channel.UNLIMITED)
|
val updatesChannel = Channel<Update>(Channel.UNLIMITED)
|
||||||
val mediaGroupChannel = Channel<Pair<String, BaseMessageUpdate>>(Channel.UNLIMITED)
|
val mediaGroupChannel = Channel<Pair<String, BaseMessageUpdate>>(Channel.UNLIMITED)
|
||||||
@@ -29,7 +33,7 @@ fun CoroutineScope.updateHandlerWithMediaGroupsAdaptation(
|
|||||||
)
|
)
|
||||||
|
|
||||||
launch {
|
launch {
|
||||||
launchSafelyWithoutExceptions {
|
launchLoggingDropExceptions(logger = logger) {
|
||||||
for (update in updatesChannel) {
|
for (update in updatesChannel) {
|
||||||
val data = update.data
|
val data = update.data
|
||||||
when {
|
when {
|
||||||
@@ -40,7 +44,7 @@ fun CoroutineScope.updateHandlerWithMediaGroupsAdaptation(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
launchSafelyWithoutExceptions {
|
launchLoggingDropExceptions(logger = logger) {
|
||||||
for ((_, mediaGroup) in mediaGroupAccumulatedChannel) {
|
for ((_, mediaGroup) in mediaGroupAccumulatedChannel) {
|
||||||
mediaGroup.convertWithMediaGroupUpdates().forEach {
|
mediaGroup.convertWithMediaGroupUpdates().forEach {
|
||||||
output(it)
|
output(it)
|
||||||
@@ -59,5 +63,6 @@ fun CoroutineScope.updateHandlerWithMediaGroupsAdaptation(
|
|||||||
* @see UpdateReceiver
|
* @see UpdateReceiver
|
||||||
*/
|
*/
|
||||||
fun CoroutineScope.updateHandlerWithMediaGroupsAdaptation(
|
fun CoroutineScope.updateHandlerWithMediaGroupsAdaptation(
|
||||||
|
logger: KSLog = DefaultKTgBotAPIKSLog,
|
||||||
output: UpdateReceiver<Update>
|
output: UpdateReceiver<Update>
|
||||||
) = updateHandlerWithMediaGroupsAdaptation(output, 1000L)
|
) = updateHandlerWithMediaGroupsAdaptation(output, 1000L, logger = logger)
|
||||||
|
@@ -1,5 +1,6 @@
|
|||||||
package dev.inmo.tgbotapi.extensions.utils.updates.retrieving
|
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.ExceptionHandler
|
||||||
import dev.inmo.micro_utils.coroutines.runCatchingSafely
|
import dev.inmo.micro_utils.coroutines.runCatchingSafely
|
||||||
import dev.inmo.tgbotapi.bot.RequestsExecutor
|
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.UpdateReceiver
|
||||||
import dev.inmo.tgbotapi.updateshandlers.UpdatesFilter
|
import dev.inmo.tgbotapi.updateshandlers.UpdatesFilter
|
||||||
import dev.inmo.tgbotapi.updateshandlers.webhook.WebhookPrivateKeyConfig
|
import dev.inmo.tgbotapi.updateshandlers.webhook.WebhookPrivateKeyConfig
|
||||||
|
import dev.inmo.tgbotapi.utils.DefaultKTgBotAPIKSLog
|
||||||
import io.ktor.http.*
|
import io.ktor.http.*
|
||||||
import io.ktor.server.engine.*
|
import io.ktor.server.engine.*
|
||||||
import io.ktor.server.request.*
|
import io.ktor.server.request.*
|
||||||
@@ -40,9 +42,10 @@ fun Route.includeWebhookHandlingInRoute(
|
|||||||
scope: CoroutineScope,
|
scope: CoroutineScope,
|
||||||
exceptionsHandler: ExceptionHandler<Unit>? = null,
|
exceptionsHandler: ExceptionHandler<Unit>? = null,
|
||||||
mediaGroupsDebounceTimeMillis: Long = 1000L,
|
mediaGroupsDebounceTimeMillis: Long = 1000L,
|
||||||
|
logger: KSLog = DefaultKTgBotAPIKSLog,
|
||||||
block: UpdateReceiver<Update>,
|
block: UpdateReceiver<Update>,
|
||||||
) {
|
) {
|
||||||
val transformer = scope.updateHandlerWithMediaGroupsAdaptation(block, mediaGroupsDebounceTimeMillis)
|
val transformer = scope.updateHandlerWithMediaGroupsAdaptation(block, mediaGroupsDebounceTimeMillis, logger = logger)
|
||||||
post {
|
post {
|
||||||
try {
|
try {
|
||||||
runCatchingSafely {
|
runCatchingSafely {
|
||||||
@@ -71,11 +74,13 @@ fun Route.includeWebhookHandlingInRouteWithFlows(
|
|||||||
scope: CoroutineScope,
|
scope: CoroutineScope,
|
||||||
exceptionsHandler: ExceptionHandler<Unit>? = null,
|
exceptionsHandler: ExceptionHandler<Unit>? = null,
|
||||||
mediaGroupsDebounceTimeMillis: Long = 1000L,
|
mediaGroupsDebounceTimeMillis: Long = 1000L,
|
||||||
|
logger: KSLog = DefaultKTgBotAPIKSLog,
|
||||||
block: FlowsUpdatesFilter.() -> Unit,
|
block: FlowsUpdatesFilter.() -> Unit,
|
||||||
) = includeWebhookHandlingInRoute(
|
) = includeWebhookHandlingInRoute(
|
||||||
scope,
|
scope,
|
||||||
exceptionsHandler,
|
exceptionsHandler,
|
||||||
mediaGroupsDebounceTimeMillis,
|
mediaGroupsDebounceTimeMillis,
|
||||||
|
logger,
|
||||||
flowsUpdatesFilter(block = block).asUpdateReceiver
|
flowsUpdatesFilter(block = block).asUpdateReceiver
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -105,6 +110,7 @@ fun <TEngine : ApplicationEngine, TConfiguration : ApplicationEngine.Configurati
|
|||||||
mediaGroupsDebounceTimeMillis: Long = 1000L,
|
mediaGroupsDebounceTimeMillis: Long = 1000L,
|
||||||
additionalApplicationEnvironmentConfigurator: ApplicationEnvironmentBuilder.() -> Unit = {},
|
additionalApplicationEnvironmentConfigurator: ApplicationEnvironmentBuilder.() -> Unit = {},
|
||||||
additionalEngineConfigurator: TConfiguration.() -> Unit = {},
|
additionalEngineConfigurator: TConfiguration.() -> Unit = {},
|
||||||
|
logger: KSLog = DefaultKTgBotAPIKSLog,
|
||||||
block: UpdateReceiver<Update>,
|
block: UpdateReceiver<Update>,
|
||||||
): EmbeddedServer<TEngine, TConfiguration> =
|
): EmbeddedServer<TEngine, TConfiguration> =
|
||||||
embeddedServer(
|
embeddedServer(
|
||||||
@@ -134,12 +140,19 @@ fun <TEngine : ApplicationEngine, TConfiguration : ApplicationEngine.Configurati
|
|||||||
routing {
|
routing {
|
||||||
listenRoute?.also {
|
listenRoute?.also {
|
||||||
createRouteFromPath(it).includeWebhookHandlingInRoute(
|
createRouteFromPath(it).includeWebhookHandlingInRoute(
|
||||||
scope,
|
scope = scope,
|
||||||
exceptionsHandler,
|
exceptionsHandler = exceptionsHandler,
|
||||||
mediaGroupsDebounceTimeMillis,
|
mediaGroupsDebounceTimeMillis = mediaGroupsDebounceTimeMillis,
|
||||||
block
|
logger = logger,
|
||||||
|
block = block
|
||||||
)
|
)
|
||||||
} ?: includeWebhookHandlingInRoute(scope, exceptionsHandler, mediaGroupsDebounceTimeMillis, block)
|
} ?: includeWebhookHandlingInRoute(
|
||||||
|
scope = scope,
|
||||||
|
exceptionsHandler = exceptionsHandler,
|
||||||
|
mediaGroupsDebounceTimeMillis = mediaGroupsDebounceTimeMillis,
|
||||||
|
logger = logger,
|
||||||
|
block = block
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
).also {
|
).also {
|
||||||
@@ -173,21 +186,23 @@ suspend fun <TEngine : ApplicationEngine, TConfiguration : ApplicationEngine.Con
|
|||||||
mediaGroupsDebounceTimeMillis: Long = 1000L,
|
mediaGroupsDebounceTimeMillis: Long = 1000L,
|
||||||
additionalApplicationEnvironmentConfigurator: ApplicationEnvironmentBuilder.() -> Unit = {},
|
additionalApplicationEnvironmentConfigurator: ApplicationEnvironmentBuilder.() -> Unit = {},
|
||||||
additionalEngineConfigurator: TConfiguration.() -> Unit = {},
|
additionalEngineConfigurator: TConfiguration.() -> Unit = {},
|
||||||
|
logger: KSLog = Log,
|
||||||
block: UpdateReceiver<Update>,
|
block: UpdateReceiver<Update>,
|
||||||
): EmbeddedServer<TEngine, TConfiguration> = try {
|
): EmbeddedServer<TEngine, TConfiguration> = try {
|
||||||
execute(setWebhookRequest)
|
execute(setWebhookRequest)
|
||||||
startListenWebhooks(
|
startListenWebhooks(
|
||||||
listenPort,
|
listenPort = listenPort,
|
||||||
engineFactory,
|
engineFactory = engineFactory,
|
||||||
exceptionsHandler,
|
exceptionsHandler = exceptionsHandler,
|
||||||
listenHost,
|
listenHost = listenHost,
|
||||||
listenRoute,
|
listenRoute = listenRoute,
|
||||||
privateKeyConfig,
|
privateKeyConfig = privateKeyConfig,
|
||||||
scope,
|
scope = scope,
|
||||||
mediaGroupsDebounceTimeMillis,
|
mediaGroupsDebounceTimeMillis = mediaGroupsDebounceTimeMillis,
|
||||||
additionalApplicationEnvironmentConfigurator,
|
additionalApplicationEnvironmentConfigurator = additionalApplicationEnvironmentConfigurator,
|
||||||
additionalEngineConfigurator,
|
additionalEngineConfigurator = additionalEngineConfigurator,
|
||||||
block
|
logger = logger,
|
||||||
|
block = block
|
||||||
)
|
)
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
throw e
|
throw e
|
||||||
|
Reference in New Issue
Block a user