mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2024-11-22 08:13:47 +00:00
behaviour builder updates
This commit is contained in:
parent
8ced95bc2f
commit
e2e235bd6c
@ -2,6 +2,14 @@
|
|||||||
|
|
||||||
## 0.33.2
|
## 0.33.2
|
||||||
|
|
||||||
|
* `Behaviour Builder`:
|
||||||
|
* New typealias `MediaGroupFilter` has been added for `MediaGroup` expectators
|
||||||
|
* Several typealiases became `suspend`:
|
||||||
|
* `CallbackQueryMapper`
|
||||||
|
* `ChatMemberUpdatedMapper`
|
||||||
|
* `InlineQueryMapper`
|
||||||
|
* Commands got an additional parameter - `additionalFilter`. It will be called when all command filters were passed
|
||||||
|
|
||||||
## 0.33.1
|
## 0.33.1
|
||||||
|
|
||||||
* `Common`:
|
* `Common`:
|
||||||
|
@ -8,7 +8,7 @@ import dev.inmo.tgbotapi.requests.abstracts.Request
|
|||||||
import dev.inmo.tgbotapi.types.CallbackQuery.*
|
import dev.inmo.tgbotapi.types.CallbackQuery.*
|
||||||
import kotlinx.coroutines.flow.toList
|
import kotlinx.coroutines.flow.toList
|
||||||
|
|
||||||
typealias CallbackQueryMapper<T> = T.() -> T?
|
typealias CallbackQueryMapper<T> = suspend T.() -> T?
|
||||||
|
|
||||||
private suspend fun <O> BehaviourContext.waitCallbackQueries(
|
private suspend fun <O> BehaviourContext.waitCallbackQueries(
|
||||||
count: Int = 1,
|
count: Int = 1,
|
||||||
|
@ -11,7 +11,7 @@ import dev.inmo.tgbotapi.types.update.MyChatMemberUpdatedUpdate
|
|||||||
import dev.inmo.tgbotapi.types.update.abstracts.ChatMemberUpdatedUpdate
|
import dev.inmo.tgbotapi.types.update.abstracts.ChatMemberUpdatedUpdate
|
||||||
import kotlinx.coroutines.flow.toList
|
import kotlinx.coroutines.flow.toList
|
||||||
|
|
||||||
typealias ChatMemberUpdatedMapper<T> = T.() -> T?
|
typealias ChatMemberUpdatedMapper<T> = suspend T.() -> T?
|
||||||
|
|
||||||
private suspend inline fun <reified T : ChatMemberUpdatedUpdate> BehaviourContext.waitChatMemberUpdated(
|
private suspend inline fun <reified T : ChatMemberUpdatedUpdate> BehaviourContext.waitChatMemberUpdated(
|
||||||
count: Int = 1,
|
count: Int = 1,
|
||||||
|
@ -8,7 +8,7 @@ import dev.inmo.tgbotapi.types.InlineQueries.query.BaseInlineQuery
|
|||||||
import dev.inmo.tgbotapi.types.InlineQueries.query.LocationInlineQuery
|
import dev.inmo.tgbotapi.types.InlineQueries.query.LocationInlineQuery
|
||||||
import kotlinx.coroutines.flow.toList
|
import kotlinx.coroutines.flow.toList
|
||||||
|
|
||||||
typealias InlineQueryMapper<T> = T.() -> T?
|
typealias InlineQueryMapper<T> = suspend T.() -> T?
|
||||||
|
|
||||||
private suspend fun <O> BehaviourContext.waitInlineQueries(
|
private suspend fun <O> BehaviourContext.waitInlineQueries(
|
||||||
count: Int = 1,
|
count: Int = 1,
|
||||||
|
@ -11,12 +11,14 @@ import dev.inmo.tgbotapi.utils.PreviewFeature
|
|||||||
import kotlinx.coroutines.flow.take
|
import kotlinx.coroutines.flow.take
|
||||||
import kotlinx.coroutines.flow.toList
|
import kotlinx.coroutines.flow.toList
|
||||||
|
|
||||||
|
typealias MediaGroupFilter<T> = suspend List<MediaGroupMessage<T>>.() -> Boolean
|
||||||
|
|
||||||
@PreviewFeature
|
@PreviewFeature
|
||||||
internal suspend inline fun <reified T : MediaGroupContent> BehaviourContext.buildMediaGroupWaiter(
|
internal suspend inline fun <reified T : MediaGroupContent> BehaviourContext.buildMediaGroupWaiter(
|
||||||
count: Int = 1,
|
count: Int = 1,
|
||||||
initRequest: Request<*>? = null,
|
initRequest: Request<*>? = null,
|
||||||
noinline errorFactory: NullableRequestBuilder<*> = { null },
|
noinline errorFactory: NullableRequestBuilder<*> = { null },
|
||||||
noinline filter: (suspend (List<MediaGroupMessage<T>>) -> Boolean)? = null
|
noinline filter: MediaGroupFilter<T>? = null
|
||||||
) = flowsUpdatesFilter.expectFlow(bot, initRequest, count, errorFactory) { update ->
|
) = flowsUpdatesFilter.expectFlow(bot, initRequest, count, errorFactory) { update ->
|
||||||
update.asSentMediaGroupUpdate() ?.data ?.let { mediaGroup ->
|
update.asSentMediaGroupUpdate() ?.data ?.let { mediaGroup ->
|
||||||
if (mediaGroup.all { message -> message.content is T } && (filter == null || filter(mediaGroup as List<MediaGroupMessage<T>>))) {
|
if (mediaGroup.all { message -> message.content is T } && (filter == null || filter(mediaGroup as List<MediaGroupMessage<T>>))) {
|
||||||
@ -33,35 +35,35 @@ suspend fun BehaviourContext.waitMediaGroup(
|
|||||||
initRequest: Request<*>? = null,
|
initRequest: Request<*>? = null,
|
||||||
errorFactory: NullableRequestBuilder<*> = { null },
|
errorFactory: NullableRequestBuilder<*> = { null },
|
||||||
count: Int = 1,
|
count: Int = 1,
|
||||||
filter: (suspend (List<MediaGroupMessage<MediaGroupContent>>) -> Boolean)? = null
|
filter: MediaGroupFilter<MediaGroupContent>? = null
|
||||||
) = buildMediaGroupWaiter(count, initRequest, errorFactory, filter)
|
) = buildMediaGroupWaiter(count, initRequest, errorFactory, filter)
|
||||||
suspend fun BehaviourContext.waitPlaylist(
|
suspend fun BehaviourContext.waitPlaylist(
|
||||||
initRequest: Request<*>? = null,
|
initRequest: Request<*>? = null,
|
||||||
errorFactory: NullableRequestBuilder<*> = { null },
|
errorFactory: NullableRequestBuilder<*> = { null },
|
||||||
count: Int = 1,
|
count: Int = 1,
|
||||||
filter: (suspend (List<MediaGroupMessage<AudioMediaGroupContent>>) -> Boolean)? = null
|
filter: MediaGroupFilter<AudioMediaGroupContent>? = null
|
||||||
) = buildMediaGroupWaiter(count, initRequest, errorFactory, filter)
|
) = buildMediaGroupWaiter(count, initRequest, errorFactory, filter)
|
||||||
suspend fun BehaviourContext.waitDocumentsGroup(
|
suspend fun BehaviourContext.waitDocumentsGroup(
|
||||||
initRequest: Request<*>? = null,
|
initRequest: Request<*>? = null,
|
||||||
errorFactory: NullableRequestBuilder<*> = { null },
|
errorFactory: NullableRequestBuilder<*> = { null },
|
||||||
count: Int = 1,
|
count: Int = 1,
|
||||||
filter: (suspend (List<MediaGroupMessage<DocumentMediaGroupContent>>) -> Boolean)? = null
|
filter: MediaGroupFilter<DocumentMediaGroupContent>? = null
|
||||||
) = buildMediaGroupWaiter(count, initRequest, errorFactory, filter)
|
) = buildMediaGroupWaiter(count, initRequest, errorFactory, filter)
|
||||||
suspend fun BehaviourContext.waitVisualGallery(
|
suspend fun BehaviourContext.waitVisualGallery(
|
||||||
initRequest: Request<*>? = null,
|
initRequest: Request<*>? = null,
|
||||||
errorFactory: NullableRequestBuilder<*> = { null },
|
errorFactory: NullableRequestBuilder<*> = { null },
|
||||||
count: Int = 1,
|
count: Int = 1,
|
||||||
filter: (suspend (List<MediaGroupMessage<VisualMediaGroupContent>>) -> Boolean)? = null
|
filter: MediaGroupFilter<VisualMediaGroupContent>? = null
|
||||||
) = buildMediaGroupWaiter(count, initRequest, errorFactory, filter)
|
) = buildMediaGroupWaiter(count, initRequest, errorFactory, filter)
|
||||||
suspend fun BehaviourContext.waitPhotoGallery(
|
suspend fun BehaviourContext.waitPhotoGallery(
|
||||||
initRequest: Request<*>? = null,
|
initRequest: Request<*>? = null,
|
||||||
errorFactory: NullableRequestBuilder<*> = { null },
|
errorFactory: NullableRequestBuilder<*> = { null },
|
||||||
count: Int = 1,
|
count: Int = 1,
|
||||||
filter: (suspend (List<MediaGroupMessage<PhotoContent>>) -> Boolean)? = null
|
filter: MediaGroupFilter<PhotoContent>? = null
|
||||||
) = buildMediaGroupWaiter(count, initRequest, errorFactory, filter)
|
) = buildMediaGroupWaiter(count, initRequest, errorFactory, filter)
|
||||||
suspend fun BehaviourContext.waitVideoGallery(
|
suspend fun BehaviourContext.waitVideoGallery(
|
||||||
initRequest: Request<*>? = null,
|
initRequest: Request<*>? = null,
|
||||||
errorFactory: NullableRequestBuilder<*> = { null },
|
errorFactory: NullableRequestBuilder<*> = { null },
|
||||||
count: Int = 1,
|
count: Int = 1,
|
||||||
filter: (suspend (List<MediaGroupMessage<VideoContent>>) -> Boolean)? = null
|
filter: MediaGroupFilter<VideoContent>? = null
|
||||||
) = buildMediaGroupWaiter(count, initRequest, errorFactory, filter)
|
) = buildMediaGroupWaiter(count, initRequest, errorFactory, filter)
|
||||||
|
@ -11,6 +11,7 @@ suspend fun BehaviourContext.command(
|
|||||||
commandRegex: Regex,
|
commandRegex: Regex,
|
||||||
requireOnlyCommandInMessage: Boolean = true,
|
requireOnlyCommandInMessage: Boolean = true,
|
||||||
includeFilterByChatInBehaviourSubContext: Boolean = true,
|
includeFilterByChatInBehaviourSubContext: Boolean = true,
|
||||||
|
additionalFilter: CommonMessageFilter<TextContent>? = null,
|
||||||
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<TextContent>>
|
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<TextContent>>
|
||||||
): Job = onText(
|
): Job = onText(
|
||||||
includeFilterByChatInBehaviourSubContext,
|
includeFilterByChatInBehaviourSubContext,
|
||||||
@ -22,7 +23,9 @@ suspend fun BehaviourContext.command(
|
|||||||
} else {
|
} else {
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
sizeRequirement && textSources.any { commandRegex.matches(it.asBotCommandTextSource() ?.command ?: return@any false) }
|
sizeRequirement && textSources.any {
|
||||||
|
commandRegex.matches(it.asBotCommandTextSource() ?.command ?: return@any false)
|
||||||
|
} && (additionalFilter ?.invoke(message) != false)
|
||||||
},
|
},
|
||||||
scenarioReceiver
|
scenarioReceiver
|
||||||
)
|
)
|
||||||
@ -30,19 +33,22 @@ suspend fun BehaviourContext.command(
|
|||||||
command: String,
|
command: String,
|
||||||
requireOnlyCommandInMessage: Boolean = true,
|
requireOnlyCommandInMessage: Boolean = true,
|
||||||
includeFilterByChatInBehaviourSubContext: Boolean = true,
|
includeFilterByChatInBehaviourSubContext: Boolean = true,
|
||||||
|
additionalFilter: CommonMessageFilter<TextContent>? = null,
|
||||||
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<TextContent>>
|
scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<TextContent>>
|
||||||
) = command(command.toRegex(), requireOnlyCommandInMessage, includeFilterByChatInBehaviourSubContext, scenarioReceiver)
|
) = command(command.toRegex(), requireOnlyCommandInMessage, includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver)
|
||||||
|
|
||||||
suspend inline fun BehaviourContext.onCommand(
|
suspend inline fun BehaviourContext.onCommand(
|
||||||
commandRegex: Regex,
|
commandRegex: Regex,
|
||||||
requireOnlyCommandInMessage: Boolean = true,
|
requireOnlyCommandInMessage: Boolean = true,
|
||||||
includeFilterByChatInBehaviourSubContext: Boolean = true,
|
includeFilterByChatInBehaviourSubContext: Boolean = true,
|
||||||
|
noinline additionalFilter: CommonMessageFilter<TextContent>? = null,
|
||||||
noinline scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<TextContent>>
|
noinline scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<TextContent>>
|
||||||
): Job = command(commandRegex, requireOnlyCommandInMessage, includeFilterByChatInBehaviourSubContext, scenarioReceiver)
|
): Job = command(commandRegex, requireOnlyCommandInMessage, includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver)
|
||||||
|
|
||||||
suspend inline fun BehaviourContext.onCommand(
|
suspend inline fun BehaviourContext.onCommand(
|
||||||
command: String,
|
command: String,
|
||||||
requireOnlyCommandInMessage: Boolean = true,
|
requireOnlyCommandInMessage: Boolean = true,
|
||||||
includeFilterByChatInBehaviourSubContext: Boolean = true,
|
includeFilterByChatInBehaviourSubContext: Boolean = true,
|
||||||
|
noinline additionalFilter: CommonMessageFilter<TextContent>? = null,
|
||||||
noinline scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<TextContent>>
|
noinline scenarioReceiver: BehaviourContextAndTypeReceiver<Unit, CommonMessage<TextContent>>
|
||||||
): Job = onCommand(command.toRegex(), requireOnlyCommandInMessage, includeFilterByChatInBehaviourSubContext, scenarioReceiver)
|
): Job = onCommand(command.toRegex(), requireOnlyCommandInMessage, includeFilterByChatInBehaviourSubContext, additionalFilter, scenarioReceiver)
|
||||||
|
Loading…
Reference in New Issue
Block a user