mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2024-11-22 00:03:48 +00:00
add deeplinks triggers
This commit is contained in:
parent
9ea06de27c
commit
05112afe0c
@ -33,6 +33,12 @@ suspend fun BehaviourContext.waitCommandMessage(
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun BehaviourContext.waitCommandMessage(
|
||||
command: String,
|
||||
initRequest: Request<*>? = null,
|
||||
errorFactory: NullableRequestBuilder<*> = { null }
|
||||
) = waitCommandMessage(Regex(command), initRequest, errorFactory)
|
||||
|
||||
fun Flow<CommonMessage<TextContent>>.requireCommandAtStart() = filter {
|
||||
(it.content.textSources.firstOrNull() as? BotCommandTextSource) != null
|
||||
}
|
||||
|
@ -0,0 +1,23 @@
|
||||
package dev.inmo.tgbotapi.extensions.behaviour_builder.expectations
|
||||
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
|
||||
import dev.inmo.tgbotapi.extensions.utils.regularTextSourceOrNull
|
||||
import dev.inmo.tgbotapi.requests.abstracts.Request
|
||||
import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage
|
||||
import dev.inmo.tgbotapi.types.message.content.TextContent
|
||||
import dev.inmo.tgbotapi.types.message.textsources.RegularTextSource
|
||||
import kotlinx.coroutines.flow.*
|
||||
|
||||
suspend fun BehaviourContext.waitDeepLinks(
|
||||
initRequest: Request<*>? = null,
|
||||
errorFactory: NullableRequestBuilder<*> = { null },
|
||||
): Flow<Pair<CommonMessage<TextContent>, String>> = waitCommandMessage(
|
||||
"start",
|
||||
initRequest,
|
||||
errorFactory
|
||||
)
|
||||
.requireSingleCommand()
|
||||
.requireCommandAtStart()
|
||||
.flattenCommandsWithParams().mapNotNull {
|
||||
it.first to (it.second.second.singleOrNull() ?.regularTextSourceOrNull() ?.source ?: return@mapNotNull null)
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
@file:Suppress("unused")
|
||||
|
||||
package dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling
|
||||
|
||||
import dev.inmo.micro_utils.coroutines.*
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.*
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitDeepLinks
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.filters.CommonMessageFilterExcludeMediaGroups
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.filters.MessageFilterByChat
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.ByChatMessageMarkerFactory
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.MarkerFactory
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.times
|
||||
import dev.inmo.tgbotapi.extensions.utils.*
|
||||
import dev.inmo.tgbotapi.extensions.utils.extensions.parseCommandsWithParams
|
||||
import dev.inmo.tgbotapi.types.message.content.TextContent
|
||||
import dev.inmo.tgbotapi.types.message.content.TextMessage
|
||||
import dev.inmo.tgbotapi.types.message.textsources.RegularTextSource
|
||||
import dev.inmo.tgbotapi.types.update.abstracts.Update
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.flow.filter
|
||||
|
||||
suspend fun <BC : BehaviourContext> BC.onDeepLink(
|
||||
initialFilter: SimpleFilter<Pair<TextMessage, String>>? = null,
|
||||
subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver<BC, Boolean, Pair<TextMessage, String>, Update> = { (message, _), update -> MessageFilterByChat(this, message, update) },
|
||||
markerFactory: MarkerFactory<Pair<TextMessage, String>, Any> = MarkerFactory { (message, _) -> ByChatMessageMarkerFactory(message) },
|
||||
scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, Pair<TextMessage, String>>
|
||||
): Job = on(
|
||||
markerFactory,
|
||||
SimpleFilter<Pair<TextMessage, String>> { (message, _) ->
|
||||
message.content.textSources.size == 2
|
||||
&& message.content.textSources.firstOrNull() ?.asBotCommandTextSource() ?.command == "start"
|
||||
&& message.content.textSources.getOrNull(1) is RegularTextSource
|
||||
} * initialFilter,
|
||||
subcontextUpdatesFilter,
|
||||
scenarioReceiver,
|
||||
) {
|
||||
(it.messageUpdateOrNull()) ?.data ?.commonMessageOrNull() ?.withContentOrNull<TextContent>() ?.let { message ->
|
||||
message to message.content.textSources[1].source
|
||||
} ?.let(::listOfNotNull)
|
||||
}
|
@ -4,6 +4,7 @@ fun interface SimpleFilter<in T> {
|
||||
suspend operator fun invoke(o: T): Boolean
|
||||
}
|
||||
val TrueSimpleFilter = SimpleFilter<Any?> { true }
|
||||
val FalseSimpleFilter = SimpleFilter<Any?> { false }
|
||||
|
||||
/**
|
||||
* @return [SimpleFilter] which will return true in case when all the items in incoming data passed [this] filter
|
||||
@ -28,21 +29,29 @@ fun <T> SimpleFilter<T>.listNone() = SimpleFilter<Iterable<T>> {
|
||||
|
||||
/**
|
||||
* Makes an AND (&&) operation between [this] and [other]
|
||||
*
|
||||
* * When both arguments are null, [TrueSimpleFilter] will be returned
|
||||
*/
|
||||
operator fun <T> SimpleFilter<T>?.times(other: SimpleFilter<T>): SimpleFilter<T> = this ?.let {
|
||||
SimpleFilter {
|
||||
this(it) && other(it)
|
||||
}
|
||||
} ?: other
|
||||
infix operator fun <T> SimpleFilter<T>?.times(other: SimpleFilter<T>?): SimpleFilter<T> = this ?.let {
|
||||
other ?.let {
|
||||
SimpleFilter {
|
||||
this(it) && other(it)
|
||||
}
|
||||
} ?: it
|
||||
} ?: other ?: TrueSimpleFilter
|
||||
|
||||
/**
|
||||
* Makes an OR (||) operation between [this] and [other]
|
||||
*
|
||||
* * When both arguments are null, [TrueSimpleFilter] will be returned
|
||||
*/
|
||||
operator fun <T> SimpleFilter<T>?.plus(other: SimpleFilter<T>): SimpleFilter<T> = this ?.let {
|
||||
SimpleFilter {
|
||||
this(it) || other(it)
|
||||
}
|
||||
} ?: other
|
||||
infix operator fun <T> SimpleFilter<T>?.plus(other: SimpleFilter<T>?): SimpleFilter<T> = this ?.let {
|
||||
other ?.let {
|
||||
SimpleFilter {
|
||||
this(it) || other(it)
|
||||
}
|
||||
} ?: it
|
||||
} ?: other ?: TrueSimpleFilter
|
||||
|
||||
/**
|
||||
* Reverse results of [this]
|
||||
|
Loading…
Reference in New Issue
Block a user