diff --git a/CHANGELOG.md b/CHANGELOG.md index 207587df79..4bbd304af5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,6 +44,26 @@ * `startGettingOfUpdates` extension which not require filter (but return a new one) was added * `TelegramBotAPI-extensions-utils`: * Subproject was added + * `filterByChat` and `filterByChatId` extensions was added + * `filterExactCommands` and `filterCommandsInsideTextMessages` extensions was added + * `asContentMessages`, `asChatEvents` and `asUnknownMessages` extensions was added + * `withContentType` extension was added + * `onlyAnimationContentMessages` extension was added + * `onlyAudioContentMessages` extension was added + * `onlyContactContentMessages` extension was added + * `onlyDiceContentMessages` extension was added + * `onlyDocumentContentMessages` extension was added + * `onlyGameContentMessages` extension was added + * `onlyInvoiceContentMessages` extension was added + * `onlyLocationContentMessages` extension was added + * `onlyPhotoContentMessages` extension was added + * `onlyPollContentMessages` extension was added + * `onlyStickerContentMessages` extension was added + * `onlyTextContentMessages` extension was added + * `onlyVenueContentMessages` extension was added + * `onlyVideoContentMessages` extension was added + * `onlyVideoNoteContentMessages` extension was added + * `onlyVoiceContentMessages` extension was added ### 0.26.1 diff --git a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/ContentMessageConversations.kt b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/ContentMessageConversations.kt new file mode 100644 index 0000000000..d4752d67e2 --- /dev/null +++ b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/ContentMessageConversations.kt @@ -0,0 +1,36 @@ +package com.github.insanusmokrassar.TelegramBotAPI.extensions.utils + +import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.ContentMessage +import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.* +import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.abstracts.MessageContent +import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.media.* +import com.github.insanusmokrassar.TelegramBotAPI.types.message.payments.InvoiceContent +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.mapNotNull +import kotlin.reflect.KClass + +fun Flow>.withContentType(contentType: KClass) = mapNotNull { + if (contentType.isInstance(it.content)) { + @Suppress("UNCHECKED_CAST") + it as ContentMessage + } else { + null + } +} + +fun Flow>.onlyAnimationContentMessages() = withContentType(AnimationContent::class) +fun Flow>.onlyAudioContentMessages() = withContentType(AudioContent::class) +fun Flow>.onlyContactContentMessages() = withContentType(ContactContent::class) +fun Flow>.onlyDiceContentMessages() = withContentType(DiceContent::class) +fun Flow>.onlyDocumentContentMessages() = withContentType(DocumentContent::class) +fun Flow>.onlyGameContentMessages() = withContentType(GameContent::class) +fun Flow>.onlyInvoiceContentMessages() = withContentType(InvoiceContent::class) +fun Flow>.onlyLocationContentMessages() = withContentType(LocationContent::class) +fun Flow>.onlyPhotoContentMessages() = withContentType(PhotoContent::class) +fun Flow>.onlyPollContentMessages() = withContentType(PollContent::class) +fun Flow>.onlyStickerContentMessages() = withContentType(StickerContent::class) +fun Flow>.onlyTextContentMessages() = withContentType(TextContent::class) +fun Flow>.onlyVenueContentMessages() = withContentType(VenueContent::class) +fun Flow>.onlyVideoContentMessages() = withContentType(VideoContent::class) +fun Flow>.onlyVideoNoteContentMessages() = withContentType(VideoNoteContent::class) +fun Flow>.onlyVoiceContentMessages() = withContentType(VoiceContent::class) diff --git a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/CommandsFilters.kt b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/CommandsFilters.kt new file mode 100644 index 0000000000..5d1fad103e --- /dev/null +++ b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/CommandsFilters.kt @@ -0,0 +1,22 @@ +package com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.updates + +import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.onlyTextContentMessages +import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.textsources.BotCommandTextSource +import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.fullEntitiesList +import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseSentMessageUpdate +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.filter + +fun Flow.filterExactCommands( + commandRegex: Regex +) = asContentMessages().onlyTextContentMessages().filter { contentMessage -> + (contentMessage.content.fullEntitiesList().singleOrNull() as? BotCommandTextSource) ?.let { commandRegex.matches(it.command) } == true +} + +fun Flow.filterCommandsInsideTextMessages( + commandRegex: Regex +) = asContentMessages().onlyTextContentMessages().filter { contentMessage -> + contentMessage.content.fullEntitiesList().any { + (it as? BotCommandTextSource) ?.let { commandRegex.matches(it.command) } == true + } +} diff --git a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/SentMessageUpdatesConversations.kt b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/SentMessageUpdatesConversations.kt new file mode 100644 index 0000000000..8415123400 --- /dev/null +++ b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/SentMessageUpdatesConversations.kt @@ -0,0 +1,18 @@ +package com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.updates + +import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.* +import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseSentMessageUpdate +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.mapNotNull + +fun Flow.asContentMessages() = mapNotNull { + it.data as? ContentMessage<*> +} + +fun Flow.asChatEvents() = mapNotNull { + it.data as? ChatEventMessage +} + +fun Flow.asUnknownMessages() = mapNotNull { + it.data as? UnknownMessageType +} diff --git a/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/UpdatesChatFilters.kt b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/UpdatesChatFilters.kt new file mode 100644 index 0000000000..146bb2e63a --- /dev/null +++ b/TelegramBotAPI-extensions-utils/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/UpdatesChatFilters.kt @@ -0,0 +1,25 @@ +package com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.updates + +import com.github.insanusmokrassar.TelegramBotAPI.types.ChatId +import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.Chat +import com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdates.* +import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.filter + +fun Flow.filterByChatId(chatId: ChatId): Flow = filter { + it.data.chat.id == chatId +} +fun Flow.filterByChat(chat: Chat): Flow = filterByChatId(chat.id) + + +fun Flow.filterByChatId(chatId: ChatId): Flow = filter { + it.data.first().chat.id == chatId +} +fun Flow.filterByChatId(chat: Chat): Flow = filterByChatId(chat.id) + + +fun Flow.filterByChatId(chatId: ChatId): Flow = filter { + it.data.chat.id == chatId +} +fun Flow.filterByChatId(chat: Chat): Flow = filterByChatId(chat.id)