1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-22 08:13:47 +00:00

update previews

This commit is contained in:
InsanusMokrassar 2022-05-09 02:27:06 +06:00
parent df01976ea8
commit cb37b85234
12 changed files with 21 additions and 19 deletions

View File

@ -88,7 +88,6 @@ suspend fun <T : State> TelegramBot.buildBehaviourWithFSMAndStartLongPolling(
* @see BehaviourContextWithFSM.strictlyOn
* @see BehaviourContextWithFSM.onStateOrSubstate
*/
@PreviewFeature
suspend fun <T : State> TelegramBot.buildBehaviourWithFSM(
flowUpdatesFilter: FlowsUpdatesFilter,
scope: CoroutineScope = defaultCoroutineScopeProvider(),
@ -117,7 +116,6 @@ suspend fun <T : State> TelegramBot.buildBehaviourWithFSM(
* @see BehaviourContextWithFSM.strictlyOn
* @see BehaviourContextWithFSM.onStateOrSubstate
*/
@PreviewFeature
suspend fun <T : State> TelegramBot.buildBehaviourWithFSMAndStartLongPolling(
scope: CoroutineScope = defaultCoroutineScopeProvider(),
defaultExceptionsHandler: ExceptionHandler<Unit>? = null,

View File

@ -38,6 +38,7 @@ class BehaviourWithFSMStateHandlerHolder<I : O, O : State>(
suspend fun BehaviourContextWithFSM<in O>.handleState(
state: O
): O? = with(delegateTo) {
@Suppress("UNCHECKED_CAST")
handleState(state as I)
}
}

View File

@ -25,7 +25,6 @@ expect var defaultCoroutineScopeProvider: () -> CoroutineScope
* @see [BehaviourContext]
* @see startGettingOfUpdatesByLongPolling
*/
@PreviewFeature
suspend fun TelegramBot.buildBehaviour(
flowUpdatesFilter: FlowsUpdatesFilter = FlowsUpdatesFilter(),
scope: CoroutineScope = defaultCoroutineScopeProvider(),
@ -53,7 +52,6 @@ suspend fun TelegramBot.buildBehaviour(
* @see BehaviourContext
* @see startGettingOfUpdatesByLongPolling
*/
@PreviewFeature
suspend fun TelegramBot.buildBehaviourWithLongPolling(
scope: CoroutineScope = defaultCoroutineScopeProvider(),
defaultExceptionsHandler: ExceptionHandler<Unit>? = null,

View File

@ -36,7 +36,7 @@ internal suspend fun <O : MessageContent> BehaviourContext.waitCommonMessage(
val messages = when (it) {
is SentMediaGroupUpdate -> {
if (includeMediaGroups) {
it.data.map { it as CommonMessage<MessageContent> }
it.data
} else {
emptyList()
}
@ -45,7 +45,8 @@ internal suspend fun <O : MessageContent> BehaviourContext.waitCommonMessage(
else -> return@expectFlow emptyList()
}
messages.mapNotNull { message ->
val asCommonMessage = message as CommonMessage<MessageContent>
@Suppress("UNCHECKED_CAST")
val asCommonMessage = message as? CommonMessage<MessageContent> ?: return@mapNotNull null
if (filter == null || filter(asCommonMessage)) {
asCommonMessage.mapper()
} else {

View File

@ -15,7 +15,6 @@ import kotlinx.coroutines.flow.toList
typealias MediaGroupFilter<T> = suspend List<MediaGroupMessage<T>>.() -> Boolean
@PreviewFeature
internal suspend inline fun <reified T : MediaGroupContent> BehaviourContext.buildMediaGroupWaiter(
count: Int = 1,
initRequest: Request<*>? = null,

View File

@ -45,14 +45,10 @@ private suspend inline fun BehaviourContext.waitPollAnswers(
}
}
) {
if (this is PollAnswer) {
if (mapper == null) {
this
} else {
mapper(this)
}
if (mapper == null) {
this
} else {
null
mapper(this)
}
}

View File

@ -8,7 +8,7 @@ import dev.inmo.tgbotapi.types.update.abstracts.Update
/**
* Allow only messages which are not [MediaGroupMessage]
*/
val MessageFilterExcludingMediaGroups: BehaviourContextAndTwoTypesReceiver<Boolean, CommonMessage<*>, Update> = { message, update ->
val MessageFilterExcludingMediaGroups: BehaviourContextAndTwoTypesReceiver<Boolean, CommonMessage<*>, Update> = { _, update ->
update !is MediaGroupMessage<*>
}

View File

@ -7,8 +7,9 @@ import dev.inmo.tgbotapi.extensions.behaviour_builder.filters.CallbackQueryFilte
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.*
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.ByUserCallbackQueryMarkerFactory
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.MarkerFactory
import dev.inmo.tgbotapi.types.CallbackQuery.*
import dev.inmo.tgbotapi.types.queries.callback.*
import dev.inmo.tgbotapi.types.update.abstracts.Update
import dev.inmo.tgbotapi.utils.PreviewFeature
/**
* @param initialFilter This filter will be called to remove unnecessary data BEFORE [scenarioReceiver] call
@ -22,6 +23,7 @@ import dev.inmo.tgbotapi.types.update.abstracts.Update
* @param scenarioReceiver Main callback which will be used to handle incoming data if [initialFilter] will pass that
* data
*/
@PreviewFeature
suspend fun <BC : BehaviourContext> BC.onUnhandledDataCallbackQuery(
initialFilter: SimpleFilter<DataCallbackQuery>? = null,
subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver<BC, Boolean, DataCallbackQuery, Update>? = CallbackQueryFilterByUser,
@ -46,6 +48,7 @@ suspend fun <BC : BehaviourContext> BC.onUnhandledDataCallbackQuery(
* @param scenarioReceiver Main callback which will be used to handle incoming data if [initialFilter] will pass that
* data
*/
@PreviewFeature
suspend fun <BC : BehaviourContext> BC.onUnhandledInlineMessageIdDataCallbackQuery(
initialFilter: SimpleFilter<InlineMessageIdDataCallbackQuery>? = null,
subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver<BC, Boolean, InlineMessageIdDataCallbackQuery, Update>? = CallbackQueryFilterByUser,
@ -70,6 +73,7 @@ suspend fun <BC : BehaviourContext> BC.onUnhandledInlineMessageIdDataCallbackQue
* @param scenarioReceiver Main callback which will be used to handle incoming data if [initialFilter] will pass that
* data
*/
@PreviewFeature
suspend fun <BC : BehaviourContext> BC.onUnhandledMessageDataCallbackQuery(
initialFilter: SimpleFilter<MessageDataCallbackQuery>? = null,
subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver<BC, Boolean, MessageDataCallbackQuery, Update>? = CallbackQueryFilterByUser,

View File

@ -13,9 +13,11 @@ import dev.inmo.tgbotapi.extensions.utils.extensions.parseCommandsWithParams
import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage
import dev.inmo.tgbotapi.types.message.content.TextContent
import dev.inmo.tgbotapi.types.update.abstracts.Update
import dev.inmo.tgbotapi.utils.PreviewFeature
import kotlinx.coroutines.Job
@PreviewFeature
suspend fun <BC : BehaviourContext> BC.unhandledCommand(
requireOnlyCommandInMessage: Boolean = true,
initialFilter: CommonMessageFilter<TextContent>? = CommonMessageFilterExcludeMediaGroups,
@ -43,6 +45,7 @@ suspend fun <BC : BehaviourContext> BC.unhandledCommand(
scenarioReceiver
)
@PreviewFeature
suspend fun <BC : BehaviourContext> BC.onUnhandledCommand(
requireOnlyCommandInMessage: Boolean = true,
initialFilter: CommonMessageFilter<TextContent>? = CommonMessageFilterExcludeMediaGroups,
@ -51,6 +54,7 @@ suspend fun <BC : BehaviourContext> BC.onUnhandledCommand(
scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, CommonMessage<TextContent>>
): Job = unhandledCommand(requireOnlyCommandInMessage, initialFilter, subcontextUpdatesFilter, markerFactory, scenarioReceiver)
@PreviewFeature
suspend fun <BC : BehaviourContext> BC.unhandledCommandWithArgs(
initialFilter: CommonMessageFilter<TextContent>? = CommonMessageFilterExcludeMediaGroups,
subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver<BC, Boolean, CommonMessage<TextContent>, Update> = MessageFilterByChat,
@ -68,6 +72,7 @@ suspend fun <BC : BehaviourContext> BC.unhandledCommandWithArgs(
scenarioReceiver(it, args)
}
@PreviewFeature
suspend fun <BC : BehaviourContext> BC.onUnhandledCommandWithArgs(
initialFilter: CommonMessageFilter<TextContent>? = CommonMessageFilterExcludeMediaGroups,
subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver<BC, Boolean, CommonMessage<TextContent>, Update> = MessageFilterByChat,

View File

@ -36,7 +36,6 @@ import dev.inmo.tgbotapi.types.update.abstracts.BaseEditMessageUpdate
import dev.inmo.tgbotapi.types.update.abstracts.Update
import dev.inmo.tgbotapi.utils.PreviewFeature
@PreviewFeature
internal suspend inline fun <BC : BehaviourContext, reified T : MessageContent> BC.onEditedContent(
initialFilter: CommonMessageFilter<T>? = null,
noinline subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver<BC, Boolean, CommonMessage<T>, Update>? = MessageFilterByChat,

View File

@ -23,6 +23,7 @@ internal suspend inline fun <BC : BehaviourContext, reified T : ChatEvent> BC.on
markerFactory: MarkerFactory<in ChatEventMessage<T>, Any> = ByChatMessageMarkerFactory,
noinline scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, ChatEventMessage<T>>
) = on(markerFactory, initialFilter, subcontextUpdatesFilter, scenarioReceiver) {
@Suppress("UNCHECKED_CAST")
(it.asBaseSentMessageUpdate() ?.data ?.asChatEventMessage() ?.takeIf { it.chatEvent is T } as? ChatEventMessage<T>) ?.let(::listOfNotNull)
}
@ -584,7 +585,7 @@ suspend fun <BC : BehaviourContext> BC.onWebAppData(
scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, PrivateEventMessage<WebAppData>>
) = onEvent(
initialFilter ?.let { { it is PrivateEventMessage<WebAppData> && initialFilter(it) } },
subcontextUpdatesFilter ?.let { { it: ChatEventMessage<WebAppData>, update: Update -> it is PrivateEventMessage<WebAppData> && subcontextUpdatesFilter(it, update) } },
subcontextUpdatesFilter ?.let { { message: ChatEventMessage<WebAppData>, update: Update -> message is PrivateEventMessage<WebAppData> && subcontextUpdatesFilter(message, update) } },
markerFactory
) {
if (it is PrivateEventMessage<WebAppData>) {

View File

@ -1,6 +1,6 @@
package dev.inmo.tgbotapi.extensions.behaviour_builder.utils.handlers_registrar
import dev.inmo.tgbotapi.types.CallbackQuery.DataCallbackQuery
import dev.inmo.tgbotapi.types.queries.callback.DataCallbackQuery
class TriggersHolder {
val handleableCommandsHolder = HandleableRegexesHolder()