mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2025-12-04 21:35:44 +00:00
Compare commits
4 Commits
d55d8fa000
...
53e8c53fb1
| Author | SHA1 | Date | |
|---|---|---|---|
| 53e8c53fb1 | |||
| 69d36c5c1b | |||
| ad97008f12 | |||
| 3e58114eeb |
@@ -52,6 +52,8 @@ __All the `tgbotapi.extensions.*` packages have been removed__
|
||||
* `SimpleFilter` now is a `fun interface` instead of just callback (fix of [#546](https://github.com/InsanusMokrassar/TelegramBotAPI/issues/546))
|
||||
* New extension `BehaviourContext#createSubContext`. It uses separated `AccumulatorFlow` and will retrieve updates by itself
|
||||
* New extension `BehaviourContext#doInContext`
|
||||
* Extension `BehaviourContext#doInSubContextWithUpdatesFilter` has been renamed to `BehaviourContext#createSubContextAndDoWithUpdatesFilter`
|
||||
* Extension `BehaviourContext#doInSubContext` has been deprecated
|
||||
* `BehaviourContextWithFSM`:
|
||||
* `launchStateHandling` lost its parameter `contextUpdatesFlow: Flow`
|
||||
* `handleState` of `BehaviourContextWithFSM` now will get/create sub context for the state and launch handling in it
|
||||
|
||||
@@ -94,7 +94,7 @@ class DefaultBehaviourContextWithFSM<T : State>(
|
||||
private val additionalHandlers = mutableListOf<BehaviourWithFSMStateHandlerHolder<*, T>>()
|
||||
private var actualHandlersList = additionalHandlers + handlers
|
||||
|
||||
private suspend fun getSubContext(context: Any) = updatesFlows.getOrPut(context) {
|
||||
private fun getSubContext(context: Any) = updatesFlows.getOrPut(context) {
|
||||
createSubContext()
|
||||
}
|
||||
|
||||
|
||||
@@ -34,10 +34,9 @@ class BehaviourWithFSMStateHandlerHolder<I : O, O : State>(
|
||||
handleState(state as I)
|
||||
}
|
||||
|
||||
override suspend fun StatesMachine<in O>.handleState(state: O): O? = if (this is BehaviourContextWithFSM) {
|
||||
handleState(state)
|
||||
} else {
|
||||
null
|
||||
override suspend fun StatesMachine<in O>.handleState(state: O): O? = with(delegateTo) {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
handleState(state as I)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -113,10 +113,10 @@ inline fun <T> BehaviourContext(
|
||||
crossinline block: BehaviourContext.() -> T
|
||||
) = DefaultBehaviourContext(bot, scope, upstreamUpdatesFlow = flowsUpdatesFilter.allUpdatesFlow, triggersHolder = triggersHolder).run(block)
|
||||
|
||||
suspend fun <BC : BehaviourContext> BC.createSubContext(
|
||||
fun <BC : BehaviourContext> BC.createSubContext(
|
||||
scope: CoroutineScope = LinkedSupervisorScope(),
|
||||
triggersHolder: TriggersHolder = this.triggersHolder,
|
||||
updatesUpstreamFlow: Flow<Update> = allUpdatesFlow.accumulatorFlow(scope),
|
||||
updatesUpstreamFlow: Flow<Update> = allUpdatesFlow,
|
||||
updatesFilter: CustomBehaviourContextAndTypeReceiver<BC, Boolean, Update>? = null,
|
||||
) = copy(
|
||||
scope = scope,
|
||||
@@ -132,20 +132,51 @@ suspend fun <BC : BehaviourContext> BC.createSubContext(
|
||||
) as BC
|
||||
|
||||
/**
|
||||
* Creates new one [BehaviourContext], adding subsequent [FlowsUpdatesFilter] in case [updatesFilter] is provided and
|
||||
* [CoroutineScope] as new [BehaviourContext.scope]
|
||||
* Launch [behaviourContextReceiver] in context of [this] as [BehaviourContext] and as [kotlin.coroutines.CoroutineContext]
|
||||
*
|
||||
* @param stopOnCompletion ___FALSE BY DEFAULT___. Will stop [this] in case if passed true
|
||||
*/
|
||||
suspend fun <T, BC : BehaviourContext> BC.doInContext(
|
||||
stopOnCompletion: Boolean = true,
|
||||
stopOnCompletion: Boolean = false,
|
||||
behaviourContextReceiver: CustomBehaviourContextReceiver<BC, T>
|
||||
): T {
|
||||
return behaviourContextReceiver().also { if (stopOnCompletion) stop() }
|
||||
return withContext(coroutineContext) {
|
||||
behaviourContextReceiver().also { if (stopOnCompletion) stop() }
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates new one [BehaviourContext], adding subsequent [FlowsUpdatesFilter] in case [updatesFilter] is provided and
|
||||
* [CoroutineScope] as new [BehaviourContext.scope]
|
||||
* Creates new one [BehaviourContext] using [createSubContext] and launches [behaviourContextReceiver] in a new context
|
||||
* using [doInContext]
|
||||
*
|
||||
* @param stopOnCompletion ___TRUE BY DEFAULT___
|
||||
*/
|
||||
suspend fun <T, BC : BehaviourContext> BC.createSubContextAndDoWithUpdatesFilter(
|
||||
scope: CoroutineScope = LinkedSupervisorScope(),
|
||||
triggersHolder: TriggersHolder = this.triggersHolder,
|
||||
updatesUpstreamFlow: Flow<Update> = allUpdatesFlow,
|
||||
updatesFilter: CustomBehaviourContextAndTypeReceiver<BC, Boolean, Update>? = null,
|
||||
stopOnCompletion: Boolean = true,
|
||||
behaviourContextReceiver: CustomBehaviourContextReceiver<BC, T>
|
||||
): T {
|
||||
return createSubContext(
|
||||
scope,
|
||||
triggersHolder,
|
||||
updatesUpstreamFlow,
|
||||
updatesFilter
|
||||
).doInContext(
|
||||
stopOnCompletion,
|
||||
behaviourContextReceiver
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates new one [BehaviourContext] using [createSubContext] and launches [behaviourContextReceiver] in a new context
|
||||
* using [doInContext]
|
||||
*
|
||||
* @param stopOnCompletion ___TRUE BY DEFAULT___
|
||||
*/
|
||||
@Deprecated("Renamed", ReplaceWith("createSubContextAndDoWithUpdatesFilter", "dev.inmo.tgbotapi.extensions.behaviour_builder.createSubContextAndDoWithUpdatesFilter"))
|
||||
suspend fun <T, BC : BehaviourContext> BC.doInSubContextWithUpdatesFilter(
|
||||
updatesFilter: CustomBehaviourContextAndTypeReceiver<BC, Boolean, Update>?,
|
||||
stopOnCompletion: Boolean = true,
|
||||
@@ -165,13 +196,21 @@ suspend fun <T, BC : BehaviourContext> BC.doInSubContextWithUpdatesFilter(
|
||||
)
|
||||
}
|
||||
|
||||
@Deprecated("Redundant", ReplaceWith("createSubContextAndDoWithUpdatesFilter", "dev.inmo.tgbotapi.extensions.behaviour_builder.createSubContextAndDoWithUpdatesFilter"))
|
||||
suspend fun <T> BehaviourContext.doInSubContext(
|
||||
stopOnCompletion: Boolean = true,
|
||||
updatesUpstreamFlow: Flow<Update> = allUpdatesFlow,
|
||||
scope: CoroutineScope = LinkedSupervisorScope(),
|
||||
triggersHolder: TriggersHolder = this.triggersHolder,
|
||||
behaviourContextReceiver: BehaviourContextReceiver<T>
|
||||
) = doInSubContextWithUpdatesFilter(updatesFilter = null, stopOnCompletion, updatesUpstreamFlow, scope, triggersHolder, behaviourContextReceiver)
|
||||
) = createSubContextAndDoWithUpdatesFilter(
|
||||
scope,
|
||||
triggersHolder,
|
||||
updatesUpstreamFlow,
|
||||
updatesFilter = null,
|
||||
stopOnCompletion,
|
||||
behaviourContextReceiver
|
||||
)
|
||||
|
||||
/**
|
||||
* This method will cancel ALL subsequent contexts, expectations and waiters
|
||||
|
||||
@@ -21,12 +21,9 @@ internal suspend inline fun <BC : BehaviourContext, reified T> BC.on(
|
||||
scope,
|
||||
markerFactory::invoke
|
||||
) { triggerData ->
|
||||
val scope = LinkedSupervisorScope()
|
||||
doInSubContextWithUpdatesFilter(
|
||||
createSubContextAndDoWithUpdatesFilter(
|
||||
updatesFilter = subcontextUpdatesFilter ?.toOneType(triggerData),
|
||||
stopOnCompletion = false,
|
||||
updatesUpstreamFlow = allUpdatesFlow.accumulatorFlow(scope),
|
||||
scope = scope
|
||||
stopOnCompletion = false
|
||||
) {
|
||||
scenarioReceiver(triggerData)
|
||||
}
|
||||
|
||||
@@ -391,6 +391,8 @@ const val requireShippingAddressField = "need_shipping_address"
|
||||
const val shouldSendPhoneNumberToProviderField = "send_phone_number_to_provider"
|
||||
const val shouldSendEmailToProviderField = "send_email_to_provider"
|
||||
|
||||
const val resizeKeyboardField = "resize_keyboard"
|
||||
const val oneTimeKeyboardField = "one_time_keyboard"
|
||||
const val inputFieldPlaceholderField = "input_field_placeholder"
|
||||
|
||||
const val priceDependOnShipAddressField = "is_flexible"
|
||||
|
||||
@@ -1,16 +1,15 @@
|
||||
package dev.inmo.tgbotapi.types.buttons
|
||||
|
||||
import dev.inmo.tgbotapi.types.inputFieldPlaceholderField
|
||||
import dev.inmo.tgbotapi.types.inputFieldPlaceholderLimit
|
||||
import dev.inmo.tgbotapi.types.*
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class ReplyKeyboardMarkup(
|
||||
val keyboard: Matrix<KeyboardButton>,
|
||||
@SerialName("resize_keyboard")
|
||||
@SerialName(resizeKeyboardField)
|
||||
val resizeKeyboard: Boolean? = null,
|
||||
@SerialName("one_time_keyboard")
|
||||
@SerialName(oneTimeKeyboardField)
|
||||
val oneTimeKeyboard: Boolean? = null,
|
||||
@SerialName(inputFieldPlaceholderField)
|
||||
val inputFieldPlaceholder: String? = null,
|
||||
|
||||
Reference in New Issue
Block a user