mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2024-12-22 16:47:13 +00:00
updates in behaviour builder
This commit is contained in:
parent
f4b1e4a150
commit
b975a1b036
@ -2,6 +2,10 @@
|
||||
|
||||
## 0.33.3
|
||||
|
||||
* `Behaviour Builder`:
|
||||
* Rewrite logic of `doInSubContextWithUpdatesFilter` and `doInSubContextWithFlowsUpdatesFilterSetup` extensions
|
||||
* All triggers now work with `stopOnCompletion` set up to `false`
|
||||
|
||||
## 0.33.2
|
||||
|
||||
* `Common`:
|
||||
|
@ -36,12 +36,13 @@ suspend fun <T> BehaviourContext.doInSubContextWithFlowsUpdatesFilterSetup(
|
||||
newFlowsUpdatesFilterSetUp: BehaviourContextAndTypeReceiver<Unit, FlowsUpdatesFilter>?,
|
||||
stopOnCompletion: Boolean = true,
|
||||
behaviourContextReceiver: BehaviourContextReceiver<T>
|
||||
) = copy(
|
||||
flowsUpdatesFilter = FlowsUpdatesFilter(),
|
||||
scope = CoroutineScope(scope.coroutineContext + SupervisorJob())
|
||||
).run {
|
||||
): T = supervisorScope {
|
||||
val newContext = copy(
|
||||
flowsUpdatesFilter = FlowsUpdatesFilter(),
|
||||
scope = this
|
||||
)
|
||||
newFlowsUpdatesFilterSetUp ?.let {
|
||||
it.apply { invoke(this@run, this@doInSubContextWithFlowsUpdatesFilterSetup.flowsUpdatesFilter) }
|
||||
it.apply { invoke(newContext, this@doInSubContextWithFlowsUpdatesFilterSetup.flowsUpdatesFilter) }
|
||||
}
|
||||
behaviourContextReceiver().also { if (stopOnCompletion) stop() }
|
||||
}
|
||||
@ -54,17 +55,23 @@ suspend fun <T> BehaviourContext.doInSubContextWithUpdatesFilter(
|
||||
updatesFilter: BehaviourContextAndTypeReceiver<Boolean, Update>?,
|
||||
stopOnCompletion: Boolean = true,
|
||||
behaviourContextReceiver: BehaviourContextReceiver<T>
|
||||
) = doInSubContextWithFlowsUpdatesFilterSetup(
|
||||
newFlowsUpdatesFilterSetUp = updatesFilter ?.let {
|
||||
{ oldOne ->
|
||||
oldOne.allUpdatesFlow.filter { updatesFilter(it) }.subscribeSafelyWithoutExceptions(this, asUpdateReceiver)
|
||||
}
|
||||
} ?: { oldOne ->
|
||||
oldOne.allUpdatesFlow.subscribeSafelyWithoutExceptions(this, asUpdateReceiver)
|
||||
},
|
||||
stopOnCompletion,
|
||||
behaviourContextReceiver
|
||||
)
|
||||
): T {
|
||||
val updatesScope = CoroutineScope(coroutineContext + SupervisorJob())
|
||||
|
||||
return doInSubContextWithFlowsUpdatesFilterSetup(
|
||||
newFlowsUpdatesFilterSetUp = updatesFilter ?.let {
|
||||
{ oldOne ->
|
||||
oldOne.allUpdatesFlow.filter { updatesFilter(it) }.subscribeSafelyWithoutExceptions(updatesScope, asUpdateReceiver)
|
||||
}
|
||||
} ?: { oldOne ->
|
||||
oldOne.allUpdatesFlow.subscribeSafelyWithoutExceptions(updatesScope, asUpdateReceiver)
|
||||
},
|
||||
stopOnCompletion
|
||||
) {
|
||||
coroutineContext.job.invokeOnCompletion { updatesScope.cancel() }
|
||||
behaviourContextReceiver()
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun <T> BehaviourContext.doInSubContext(
|
||||
stopOnCompletion: Boolean = true,
|
||||
|
@ -26,7 +26,8 @@ internal suspend inline fun <reified T : CallbackQuery> BehaviourContext.onCallb
|
||||
{ it.sourceChat() ?.id ?.chatId == triggerQuery.user.id.chatId }
|
||||
} else {
|
||||
null
|
||||
}
|
||||
},
|
||||
stopOnCompletion = false
|
||||
) {
|
||||
scenarioReceiver(triggerQuery)
|
||||
}
|
||||
|
@ -24,7 +24,8 @@ internal suspend inline fun <reified U : ChatMemberUpdatedUpdate> BehaviourConte
|
||||
{ it.sourceChat() ?.id ?.chatId == triggerChatMemberUpdated.chat.id.chatId }
|
||||
} else {
|
||||
null
|
||||
}
|
||||
},
|
||||
stopOnCompletion = false
|
||||
) {
|
||||
scenarioReceiver(triggerChatMemberUpdated)
|
||||
}
|
||||
|
@ -50,7 +50,8 @@ internal suspend inline fun <reified T : MessageContent> BehaviourContext.onCont
|
||||
{ it.sourceChat() ?.id ?.chatId == triggerMessage.chat.id.chatId }
|
||||
} else {
|
||||
null
|
||||
}
|
||||
},
|
||||
stopOnCompletion = false
|
||||
) {
|
||||
scenarioReceiver(triggerMessage)
|
||||
}
|
||||
|
@ -29,7 +29,8 @@ internal suspend inline fun <reified T : ChatEvent> BehaviourContext.onEvent(
|
||||
doInSubContextWithUpdatesFilter(
|
||||
updatesFilter = if (includeFilterByChatInBehaviourSubContext) {
|
||||
{ it.sourceChat() ?.id ?.chatId == triggerMessage.chat.id.chatId }
|
||||
} else null
|
||||
} else null,
|
||||
stopOnCompletion = false
|
||||
) {
|
||||
scenarioReceiver(triggerMessage)
|
||||
}
|
||||
|
@ -27,7 +27,8 @@ internal suspend inline fun <reified T : InlineQuery> BehaviourContext.onInlineQ
|
||||
{ it.sourceChat() ?.id ?.chatId == triggerQuery.from.id.chatId }
|
||||
} else {
|
||||
null
|
||||
}
|
||||
},
|
||||
stopOnCompletion = false
|
||||
) {
|
||||
scenarioReceiver(triggerQuery)
|
||||
}
|
||||
|
@ -32,7 +32,8 @@ internal suspend inline fun <reified T : MediaGroupContent> BehaviourContext.bui
|
||||
doInSubContextWithUpdatesFilter(
|
||||
updatesFilter = if (includeFilterByChatInBehaviourSubContext) {
|
||||
{ it.sourceChat() ?.id ?.chatId == mediaGroupChat.id.chatId }
|
||||
} else null
|
||||
} else null,
|
||||
stopOnCompletion = false
|
||||
) {
|
||||
scenarioReceiver(mediaGroup)
|
||||
}
|
||||
|
@ -25,7 +25,8 @@ suspend inline fun <reified T : EncryptedPassportElement> BehaviourContext.onPas
|
||||
doInSubContextWithUpdatesFilter(
|
||||
updatesFilter = if (includeFilterByChatInBehaviourSubContext) {
|
||||
{ it.sourceChat() ?.id ?.chatId == triggerMessage.chat.id.chatId }
|
||||
} else null
|
||||
} else null,
|
||||
stopOnCompletion = false
|
||||
) {
|
||||
scenarioReceiver(triggerMessage)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user