1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2025-09-02 14:49:47 +00:00

improvements in data filling

This commit is contained in:
2025-08-10 22:47:52 +06:00
parent 0fd8c1e2f3
commit 9a00193e59
5 changed files with 99 additions and 39 deletions

View File

@@ -7,7 +7,6 @@ import dev.inmo.micro_utils.fsm.common.managers.InMemoryDefaultStatesManagerRepo
import dev.inmo.micro_utils.fsm.common.utils.StateHandlingErrorHandler
import dev.inmo.micro_utils.fsm.common.utils.defaultStateHandlingErrorHandler
import dev.inmo.tgbotapi.bot.TelegramBot
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.optionallyWithDefaultReceiver
import dev.inmo.tgbotapi.extensions.utils.updates.retrieving.longPolling
import dev.inmo.tgbotapi.extensions.utils.updates.retrieving.updateHandlerWithMediaGroupsAdaptation
import dev.inmo.tgbotapi.types.Seconds
@@ -15,6 +14,7 @@ import dev.inmo.tgbotapi.types.update.abstracts.Update
import dev.inmo.tgbotapi.updateshandlers.FlowsUpdatesFilter
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.emptyFlow
/**
* Creates [BehaviourContextWithFSM] via creating of [DefaultBehaviourContext] with [this] as [TelegramBot],
@@ -39,11 +39,12 @@ suspend fun <T : State> TelegramBot.buildBehaviourWithFSM(
useDefaultSubcontextInitialAction: Boolean = true,
block: CustomBehaviourContextReceiver<DefaultBehaviourContextWithFSM<T>, Unit>
): DefaultBehaviourContextWithFSM<T> = BehaviourContextWithFSM(
behaviourContext = DefaultBehaviourContext(
behaviourContext = BehaviourContext(
bot = this,
scope = defaultExceptionsHandler ?.let { scope + ContextSafelyExceptionHandler(it) } ?: scope,
upstreamUpdatesFlow = upstreamUpdatesFlow,
subcontextInitialAction = subcontextInitialAction.optionallyWithDefaultReceiver(useDefaultSubcontextInitialAction)
upstreamUpdatesFlow = upstreamUpdatesFlow ?: emptyFlow(),
useDefaultSubcontextInitialAction = useDefaultSubcontextInitialAction,
subcontextInitialAction = subcontextInitialAction
),
handlers = presetHandlers,
statesManager = statesManager,
@@ -127,11 +128,12 @@ suspend fun <T : State> TelegramBot.buildBehaviourWithFSM(
useDefaultSubcontextInitialAction: Boolean = true,
block: CustomBehaviourContextReceiver<DefaultBehaviourContextWithFSM<T>, Unit>
): DefaultBehaviourContextWithFSM<T> = BehaviourContextWithFSM(
DefaultBehaviourContext(
BehaviourContext(
this,
defaultExceptionsHandler ?.let { scope + ContextSafelyExceptionHandler(it) } ?: scope,
upstreamUpdatesFlow = flowUpdatesFilter.allUpdatesFlow,
subcontextInitialAction = subcontextInitialAction.optionallyWithDefaultReceiver(useDefaultSubcontextInitialAction)
subcontextInitialAction = subcontextInitialAction,
useDefaultSubcontextInitialAction = useDefaultSubcontextInitialAction
),
presetHandlers,
statesManager,

View File

@@ -66,8 +66,12 @@ public final class dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourConte
public final class dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContextKt {
public static final fun BehaviourContext (Ldev/inmo/tgbotapi/bot/RequestsExecutor;Lkotlinx/coroutines/CoroutineScope;Ldev/inmo/tgbotapi/updateshandlers/FlowsUpdatesFilter;Ldev/inmo/tgbotapi/extensions/behaviour_builder/utils/handlers_registrar/TriggersHolder;Lkotlin/jvm/functions/Function3;ZLkotlin/jvm/functions/Function1;)Ljava/lang/Object;
public static final fun BehaviourContext (Ldev/inmo/tgbotapi/bot/RequestsExecutor;Lkotlinx/coroutines/CoroutineScope;Ldev/inmo/tgbotapi/updateshandlers/FlowsUpdatesFilter;Ldev/inmo/tgbotapi/extensions/behaviour_builder/utils/handlers_registrar/TriggersHolder;ZLkotlin/jvm/functions/Function3;)Ldev/inmo/tgbotapi/extensions/behaviour_builder/DefaultBehaviourContext;
public static final fun BehaviourContext (Ldev/inmo/tgbotapi/bot/RequestsExecutor;Lkotlinx/coroutines/CoroutineScope;Lkotlinx/coroutines/flow/Flow;Ldev/inmo/tgbotapi/extensions/behaviour_builder/utils/handlers_registrar/TriggersHolder;Lkotlin/jvm/functions/Function3;ZLkotlin/jvm/functions/Function1;)Ljava/lang/Object;
public static final fun BehaviourContext (Ldev/inmo/tgbotapi/bot/RequestsExecutor;Lkotlinx/coroutines/CoroutineScope;Lkotlinx/coroutines/flow/Flow;Lkotlin/jvm/functions/Function3;Ldev/inmo/tgbotapi/extensions/behaviour_builder/utils/handlers_registrar/TriggersHolder;Z)Ldev/inmo/tgbotapi/extensions/behaviour_builder/DefaultBehaviourContext;
public static synthetic fun BehaviourContext$default (Ldev/inmo/tgbotapi/bot/RequestsExecutor;Lkotlinx/coroutines/CoroutineScope;Ldev/inmo/tgbotapi/updateshandlers/FlowsUpdatesFilter;Ldev/inmo/tgbotapi/extensions/behaviour_builder/utils/handlers_registrar/TriggersHolder;Lkotlin/jvm/functions/Function3;ZLkotlin/jvm/functions/Function1;ILjava/lang/Object;)Ljava/lang/Object;
public static synthetic fun BehaviourContext$default (Ldev/inmo/tgbotapi/bot/RequestsExecutor;Lkotlinx/coroutines/CoroutineScope;Ldev/inmo/tgbotapi/updateshandlers/FlowsUpdatesFilter;Ldev/inmo/tgbotapi/extensions/behaviour_builder/utils/handlers_registrar/TriggersHolder;ZLkotlin/jvm/functions/Function3;ILjava/lang/Object;)Ldev/inmo/tgbotapi/extensions/behaviour_builder/DefaultBehaviourContext;
public static synthetic fun BehaviourContext$default (Ldev/inmo/tgbotapi/bot/RequestsExecutor;Lkotlinx/coroutines/CoroutineScope;Lkotlinx/coroutines/flow/Flow;Ldev/inmo/tgbotapi/extensions/behaviour_builder/utils/handlers_registrar/TriggersHolder;Lkotlin/jvm/functions/Function3;ZLkotlin/jvm/functions/Function1;ILjava/lang/Object;)Ljava/lang/Object;
public static synthetic fun BehaviourContext$default (Ldev/inmo/tgbotapi/bot/RequestsExecutor;Lkotlinx/coroutines/CoroutineScope;Lkotlinx/coroutines/flow/Flow;Lkotlin/jvm/functions/Function3;Ldev/inmo/tgbotapi/extensions/behaviour_builder/utils/handlers_registrar/TriggersHolder;ZILjava/lang/Object;)Ldev/inmo/tgbotapi/extensions/behaviour_builder/DefaultBehaviourContext;
public static final fun BehaviourContextAndTwoTypesReceiver (Lkotlin/jvm/functions/Function4;)Lkotlin/jvm/functions/Function4;
public static final fun BehaviourContextAndTypeReceiver (Lkotlin/jvm/functions/Function3;)Lkotlin/jvm/functions/Function3;
public static final fun BehaviourContextReceiver (Lkotlin/jvm/functions/Function2;)Lkotlin/jvm/functions/Function2;
@@ -111,8 +115,8 @@ public final class dev/inmo/tgbotapi/extensions/behaviour_builder/CombinedSubcon
}
public final class dev/inmo/tgbotapi/extensions/behaviour_builder/DefaultBehaviourContext : dev/inmo/tgbotapi/updateshandlers/AbstractFlowsUpdatesFilter, dev/inmo/tgbotapi/bot/RequestsExecutor, dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContext, kotlinx/coroutines/CoroutineScope {
public fun <init> (Ldev/inmo/tgbotapi/bot/RequestsExecutor;Lkotlinx/coroutines/CoroutineScope;ILkotlinx/coroutines/channels/BufferOverflow;Lkotlinx/coroutines/flow/Flow;Ldev/inmo/tgbotapi/extensions/behaviour_builder/utils/handlers_registrar/TriggersHolder;Lkotlin/jvm/functions/Function3;)V
public synthetic fun <init> (Ldev/inmo/tgbotapi/bot/RequestsExecutor;Lkotlinx/coroutines/CoroutineScope;ILkotlinx/coroutines/channels/BufferOverflow;Lkotlinx/coroutines/flow/Flow;Ldev/inmo/tgbotapi/extensions/behaviour_builder/utils/handlers_registrar/TriggersHolder;Lkotlin/jvm/functions/Function3;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun <init> (Ldev/inmo/tgbotapi/bot/RequestsExecutor;Lkotlinx/coroutines/CoroutineScope;ILkotlinx/coroutines/channels/BufferOverflow;Lkotlinx/coroutines/flow/Flow;Ldev/inmo/tgbotapi/extensions/behaviour_builder/utils/handlers_registrar/TriggersHolder;Ldev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContextData;Lkotlin/jvm/functions/Function3;)V
public synthetic fun <init> (Ldev/inmo/tgbotapi/bot/RequestsExecutor;Lkotlinx/coroutines/CoroutineScope;ILkotlinx/coroutines/channels/BufferOverflow;Lkotlinx/coroutines/flow/Flow;Ldev/inmo/tgbotapi/extensions/behaviour_builder/utils/handlers_registrar/TriggersHolder;Ldev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContextData;Lkotlin/jvm/functions/Function3;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun close ()V
public synthetic fun copy (Ldev/inmo/tgbotapi/bot/RequestsExecutor;Lkotlinx/coroutines/CoroutineScope;ILkotlinx/coroutines/channels/BufferOverflow;Lkotlinx/coroutines/flow/Flow;Ldev/inmo/tgbotapi/extensions/behaviour_builder/utils/handlers_registrar/TriggersHolder;Lkotlin/jvm/functions/Function3;)Ldev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContext;
public fun copy (Ldev/inmo/tgbotapi/bot/RequestsExecutor;Lkotlinx/coroutines/CoroutineScope;ILkotlinx/coroutines/channels/BufferOverflow;Lkotlinx/coroutines/flow/Flow;Ldev/inmo/tgbotapi/extensions/behaviour_builder/utils/handlers_registrar/TriggersHolder;Lkotlin/jvm/functions/Function3;)Ldev/inmo/tgbotapi/extensions/behaviour_builder/DefaultBehaviourContext;
@@ -1593,6 +1597,7 @@ public final class dev/inmo/tgbotapi/extensions/behaviour_builder/utils/DefaultC
public static final field Companion Ldev/inmo/tgbotapi/extensions/behaviour_builder/utils/DefaultCustomBehaviourContextAndTypeReceiver$Companion;
public fun <init> (Lkotlin/jvm/functions/Function3;)V
public fun invoke (Ldev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContext;Ldev/inmo/tgbotapi/types/update/abstracts/Update;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public final fun invoke (Ldev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContextData;)V
public synthetic fun invoke (Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
}
@@ -1605,8 +1610,8 @@ public abstract interface class dev/inmo/tgbotapi/extensions/behaviour_builder/u
public final class dev/inmo/tgbotapi/extensions/behaviour_builder/utils/DefaultCustomBehaviourContextAndTypeReceiverKt {
public static final fun botInfo (Ldev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContext;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static final fun optionallyWithDefaultReceiver (Lkotlin/jvm/functions/Function3;Z)Lkotlin/jvm/functions/Function3;
public static final fun withDefaultReceiver (Lkotlin/jvm/functions/Function3;)Ldev/inmo/tgbotapi/extensions/behaviour_builder/utils/DefaultCustomBehaviourContextAndTypeReceiver;
public static final fun optionallyWithDefaultReceiver (Lkotlin/jvm/functions/Function3;ZLdev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContextData;)Lkotlin/jvm/functions/Function3;
public static final fun withDefaultReceiver (Lkotlin/jvm/functions/Function3;Ldev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContextData;)Ldev/inmo/tgbotapi/extensions/behaviour_builder/utils/DefaultCustomBehaviourContextAndTypeReceiver;
}
public final class dev/inmo/tgbotapi/extensions/behaviour_builder/utils/LiveLocationKt {

View File

@@ -4,6 +4,7 @@ import dev.inmo.micro_utils.coroutines.ContextSafelyExceptionHandler
import dev.inmo.micro_utils.coroutines.ExceptionHandler
import dev.inmo.tgbotapi.bot.TelegramBot
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.DefaultCustomBehaviourContextAndTypeReceiver
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.optionallyWithDefaultReceiver
import dev.inmo.tgbotapi.extensions.utils.updates.retrieving.longPolling
import dev.inmo.tgbotapi.extensions.utils.updates.retrieving.startGettingOfUpdatesByLongPolling
import dev.inmo.tgbotapi.extensions.utils.updates.retrieving.updateHandlerWithMediaGroupsAdaptation
@@ -44,11 +45,8 @@ suspend fun TelegramBot.buildBehaviour(
}
},
flowsUpdatesFilter = flowUpdatesFilter,
subcontextInitialAction = if (useDefaultSubcontextInitialAction) {
DefaultCustomBehaviourContextAndTypeReceiver(subcontextInitialAction)
} else {
subcontextInitialAction
}
useDefaultSubcontextInitialAction = useDefaultSubcontextInitialAction,
subcontextInitialAction = subcontextInitialAction
).apply {
block()
}

View File

@@ -2,7 +2,6 @@
package dev.inmo.tgbotapi.extensions.behaviour_builder
import dev.inmo.kslog.common.KSLog
import dev.inmo.micro_utils.coroutines.*
import dev.inmo.tgbotapi.bot.TelegramBot
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.handlers_registrar.TriggersHolder
@@ -84,6 +83,7 @@ class DefaultBehaviourContext(
onBufferOverflow: BufferOverflow = BufferOverflow.SUSPEND,
private val upstreamUpdatesFlow: Flow<Update>? = null,
override val triggersHolder: TriggersHolder = TriggersHolder(),
override val data: BehaviourContextData = BehaviourContextData(),
override val subcontextInitialAction: CustomBehaviourContextAndTypeReceiver<BehaviourContext, Unit, Update> = {}
) : AbstractFlowsUpdatesFilter(), TelegramBot by bot, CoroutineScope by scope, BehaviourContext {
@@ -108,8 +108,6 @@ class DefaultBehaviourContext(
}.accumulatorFlow(WeakScope(scope))
override val asUpdateReceiver: UpdateReceiver<Update> = additionalUpdatesSharedFlow::emit
override val data: BehaviourContextData = BehaviourContextData()
override fun copy(
bot: TelegramBot,
scope: CoroutineScope,
@@ -138,13 +136,17 @@ fun BehaviourContext(
triggersHolder: TriggersHolder = TriggersHolder(),
useDefaultSubcontextInitialAction: Boolean = true,
subcontextInitialAction: CustomBehaviourContextAndTypeReceiver<BehaviourContext, Unit, Update> = {}
) = DefaultBehaviourContext(
bot = bot,
scope = scope,
upstreamUpdatesFlow = flowsUpdatesFilter.allUpdatesFlow,
triggersHolder = triggersHolder,
subcontextInitialAction = subcontextInitialAction.optionallyWithDefaultReceiver(useDefaultSubcontextInitialAction)
)
): DefaultBehaviourContext {
val data = BehaviourContextData()
return DefaultBehaviourContext(
bot = bot,
scope = scope,
upstreamUpdatesFlow = flowsUpdatesFilter.allUpdatesFlow,
triggersHolder = triggersHolder,
data = data,
subcontextInitialAction = subcontextInitialAction.optionallyWithDefaultReceiver(useDefaultSubcontextInitialAction, data)
)
}
inline fun <T> BehaviourContext(
bot: TelegramBot,
@@ -154,13 +156,56 @@ inline fun <T> BehaviourContext(
noinline subcontextInitialAction: CustomBehaviourContextAndTypeReceiver<BehaviourContext, Unit, Update> = {},
useDefaultSubcontextInitialAction: Boolean = true,
crossinline block: BehaviourContext.() -> T
) = DefaultBehaviourContext(
bot = bot,
scope = scope,
upstreamUpdatesFlow = flowsUpdatesFilter.allUpdatesFlow,
triggersHolder = triggersHolder,
subcontextInitialAction = subcontextInitialAction.optionallyWithDefaultReceiver(useDefaultSubcontextInitialAction)
).run(block)
): T {
val data = BehaviourContextData()
return DefaultBehaviourContext(
bot = bot,
scope = scope,
upstreamUpdatesFlow = flowsUpdatesFilter.allUpdatesFlow,
triggersHolder = triggersHolder,
data = data,
subcontextInitialAction = subcontextInitialAction.optionallyWithDefaultReceiver(useDefaultSubcontextInitialAction, data)
).run(block)
}
inline fun <T> BehaviourContext(
bot: TelegramBot,
scope: CoroutineScope,
upstreamUpdatesFlow: Flow<Update>,
triggersHolder: TriggersHolder = TriggersHolder(),
noinline subcontextInitialAction: CustomBehaviourContextAndTypeReceiver<BehaviourContext, Unit, Update> = {},
useDefaultSubcontextInitialAction: Boolean = true,
crossinline block: BehaviourContext.() -> T
): T {
val data = BehaviourContextData()
return DefaultBehaviourContext(
bot = bot,
scope = scope,
upstreamUpdatesFlow = upstreamUpdatesFlow,
triggersHolder = triggersHolder,
data = data,
subcontextInitialAction = subcontextInitialAction.optionallyWithDefaultReceiver(useDefaultSubcontextInitialAction, data)
).run(block)
}
fun BehaviourContext(
bot: TelegramBot,
scope: CoroutineScope,
upstreamUpdatesFlow: Flow<Update>,
subcontextInitialAction: CustomBehaviourContextAndTypeReceiver<BehaviourContext, Unit, Update>,
triggersHolder: TriggersHolder = TriggersHolder(),
useDefaultSubcontextInitialAction: Boolean = true
): DefaultBehaviourContext {
val data = BehaviourContextData()
return DefaultBehaviourContext(
bot = bot,
scope = scope,
upstreamUpdatesFlow = upstreamUpdatesFlow,
triggersHolder = triggersHolder,
data = data,
subcontextInitialAction = subcontextInitialAction.optionallyWithDefaultReceiver(useDefaultSubcontextInitialAction, data)
)
}
/**
* Creates new [BehaviourContext] using its [BehaviourContext.copy] method
@@ -183,7 +228,7 @@ fun <BC : BehaviourContext> BC.createSubContext(
) as BC
/**
* Launch [behaviourContextReceiver] in context of [this] as [BehaviourContext] and as [kotlin.coroutines.CoroutineContext]
* Launch [behaviourContextReceiver] in context of [this] as [BehaviourContext] and as [CoroutineContext]
*
* [this] [BehaviourContext] will **NOT** be closed automatically
*/
@@ -196,7 +241,7 @@ suspend fun <T, BC : BehaviourContext> BC.doInContext(
}
/**
* Launch [behaviourContextReceiver] in context of [this] as [BehaviourContext] and as [kotlin.coroutines.CoroutineContext]
* Launch [behaviourContextReceiver] in context of [this] as [BehaviourContext] and as [CoroutineContext]
*
* [this] [BehaviourContext] will **NOT** be closed automatically
*/
@@ -218,7 +263,7 @@ suspend fun <T, BC : BehaviourContext> BC.doInNewSubContext(
}
/**
* Launch [behaviourContextReceiver] in context of [this] as [BehaviourContext] and as [kotlin.coroutines.CoroutineContext]
* Launch [behaviourContextReceiver] in context of [this] as [BehaviourContext] and as [CoroutineContext]
*
* [this] [BehaviourContext] will **NOT** be closed automatically
*/

View File

@@ -2,6 +2,7 @@ package dev.inmo.tgbotapi.extensions.behaviour_builder.utils
import dev.inmo.micro_utils.common.Warning
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContextData
import dev.inmo.tgbotapi.extensions.behaviour_builder.CustomBehaviourContextAndTypeReceiver
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.DefaultCustomBehaviourContextAndTypeReceiver.Companion.BOT_INFO_RECEIVER
import dev.inmo.tgbotapi.requests.bot.GetMe
@@ -28,12 +29,17 @@ suspend fun BehaviourContext.botInfo(): ExtendedBot? {
}
@Warning("It is internal API and can be changed without notes")
fun <BC : BehaviourContext, R, U : Update> CustomBehaviourContextAndTypeReceiver<BC, R, U>.withDefaultReceiver() = DefaultCustomBehaviourContextAndTypeReceiver(this)
fun <BC : BehaviourContext, R, U : Update> CustomBehaviourContextAndTypeReceiver<BC, R, U>.withDefaultReceiver(
data: BehaviourContextData
) = DefaultCustomBehaviourContextAndTypeReceiver(this).also {
it(data)
}
@Warning("It is internal API and can be changed without notes")
fun <BC : BehaviourContext, R, U : Update> CustomBehaviourContextAndTypeReceiver<BC, R, U>.optionallyWithDefaultReceiver(
include: Boolean
include: Boolean,
data: BehaviourContextData
) = if (include) {
withDefaultReceiver()
withDefaultReceiver(data)
} else {
this
}
@@ -81,6 +87,10 @@ class DefaultCustomBehaviourContextAndTypeReceiver<BC : BehaviourContext, R, U :
}
}
operator fun invoke(data: BehaviourContextData) {
data[BOT_INFO_RECEIVER] = internalReceiver
}
/**
* Registers the internal bot info provider in [BehaviourContext.data] and then delegates to [wrapperReceiver].
*
@@ -88,7 +98,7 @@ class DefaultCustomBehaviourContextAndTypeReceiver<BC : BehaviourContext, R, U :
* the wrapped behaviour.
*/
override suspend fun invoke(p1: BC, p2: U): R {
p1.data[BOT_INFO_RECEIVER] = internalReceiver
invoke(p1.data)
return wrapperReceiver(p1, p2)
}