mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2024-11-26 12:08:43 +00:00
events filters
This commit is contained in:
parent
e4ce6f8fc7
commit
33b50c6c68
@ -4,11 +4,12 @@ import com.soywiz.klock.DateTime
|
||||
import dev.inmo.tgbotapi.types.MessageIdentifier
|
||||
import dev.inmo.tgbotapi.types.chat.abstracts.ChannelChat
|
||||
import dev.inmo.tgbotapi.types.message.ChatEvents.abstracts.ChannelEvent
|
||||
import dev.inmo.tgbotapi.types.message.ChatEvents.abstracts.GroupEvent
|
||||
import dev.inmo.tgbotapi.types.message.abstracts.ChatEventMessage
|
||||
|
||||
data class ChannelEventMessage(
|
||||
data class ChannelEventMessage<T : ChannelEvent>(
|
||||
override val messageId: MessageIdentifier,
|
||||
override val chat: ChannelChat,
|
||||
override val chatEvent: ChannelEvent,
|
||||
override val chatEvent: T,
|
||||
override val date: DateTime
|
||||
) : ChatEventMessage
|
||||
) : ChatEventMessage<T>
|
||||
|
@ -8,12 +8,12 @@ import dev.inmo.tgbotapi.types.message.ChatEvents.abstracts.GroupEvent
|
||||
import dev.inmo.tgbotapi.types.message.abstracts.GroupEventMessage
|
||||
|
||||
@Deprecated("Renamed", ReplaceWith("CommonGroupEventMessage"))
|
||||
typealias GroupEventMessage = CommonGroupEventMessage
|
||||
typealias GroupEventMessage = CommonGroupEventMessage<*>
|
||||
|
||||
data class CommonGroupEventMessage(
|
||||
data class CommonGroupEventMessage<T : GroupEvent>(
|
||||
override val messageId: MessageIdentifier,
|
||||
override val user: User,
|
||||
override val chat: GroupChat,
|
||||
override val chatEvent: GroupEvent,
|
||||
override val chatEvent: T,
|
||||
override val date: DateTime
|
||||
) : GroupEventMessage
|
||||
) : GroupEventMessage<T>
|
||||
|
@ -4,16 +4,17 @@ import com.soywiz.klock.DateTime
|
||||
import dev.inmo.tgbotapi.types.MessageIdentifier
|
||||
import dev.inmo.tgbotapi.types.User
|
||||
import dev.inmo.tgbotapi.types.chat.abstracts.SupergroupChat
|
||||
import dev.inmo.tgbotapi.types.message.ChatEvents.abstracts.GroupEvent
|
||||
import dev.inmo.tgbotapi.types.message.ChatEvents.abstracts.SupergroupEvent
|
||||
import dev.inmo.tgbotapi.types.message.abstracts.SupergroupEventMessage
|
||||
|
||||
@Deprecated("Renamed", ReplaceWith("CommonSupergroupEventMessage"))
|
||||
typealias SupergroupEventMessage = CommonSupergroupEventMessage
|
||||
typealias SupergroupEventMessage = CommonSupergroupEventMessage<*>
|
||||
|
||||
data class CommonSupergroupEventMessage(
|
||||
data class CommonSupergroupEventMessage<T : SupergroupEvent>(
|
||||
override val messageId: MessageIdentifier,
|
||||
override val user: User,
|
||||
override val chat: SupergroupChat,
|
||||
override val chatEvent: SupergroupEvent,
|
||||
override val chatEvent: T,
|
||||
override val date: DateTime
|
||||
) : SupergroupEventMessage
|
||||
) : SupergroupEventMessage<T>
|
||||
|
@ -2,6 +2,6 @@ package dev.inmo.tgbotapi.types.message.abstracts
|
||||
|
||||
import dev.inmo.tgbotapi.types.message.ChatEvents.abstracts.ChatEvent
|
||||
|
||||
interface ChatEventMessage : Message {
|
||||
val chatEvent: ChatEvent
|
||||
interface ChatEventMessage<T : ChatEvent> : Message {
|
||||
val chatEvent: T
|
||||
}
|
@ -2,6 +2,4 @@ package dev.inmo.tgbotapi.types.message.abstracts
|
||||
|
||||
import dev.inmo.tgbotapi.types.message.ChatEvents.abstracts.GroupEvent
|
||||
|
||||
interface GroupEventMessage : ChatEventMessage, FromUserMessage {
|
||||
override val chatEvent: GroupEvent
|
||||
}
|
||||
interface GroupEventMessage<T : GroupEvent> : ChatEventMessage<T>, FromUserMessage
|
||||
|
@ -2,6 +2,4 @@ package dev.inmo.tgbotapi.types.message.abstracts
|
||||
|
||||
import dev.inmo.tgbotapi.types.message.ChatEvents.abstracts.SupergroupEvent
|
||||
|
||||
interface SupergroupEventMessage : GroupEventMessage {
|
||||
override val chatEvent: SupergroupEvent
|
||||
}
|
||||
interface SupergroupEventMessage<T : SupergroupEvent> : GroupEventMessage<T>
|
||||
|
@ -1,6 +1,7 @@
|
||||
package dev.inmo.tgbotapi.extensions.utils.shortcuts
|
||||
|
||||
import dev.inmo.tgbotapi.CommonAbstracts.TextSource
|
||||
import dev.inmo.tgbotapi.CommonAbstracts.textSources
|
||||
import dev.inmo.tgbotapi.extensions.utils.onlyTextContentMessages
|
||||
import dev.inmo.tgbotapi.extensions.utils.updates.asContentMessagesFlow
|
||||
import dev.inmo.tgbotapi.types.MessageEntity.textsources.BotCommandTextSource
|
||||
@ -27,7 +28,7 @@ import kotlinx.coroutines.flow.*
|
||||
fun <T : ContentMessage<TextContent>> Flow<T>.filterExactCommands(
|
||||
commandRegex: Regex
|
||||
) = filter { contentMessage ->
|
||||
(contentMessage.content.fullEntitiesList().singleOrNull() as? BotCommandTextSource) ?.let { commandRegex.matches(it.command) } == true
|
||||
(contentMessage.content.textSources.singleOrNull() as? BotCommandTextSource) ?.let { commandRegex.matches(it.command) } == true
|
||||
}
|
||||
|
||||
/**
|
||||
@ -46,7 +47,7 @@ fun <T : ContentMessage<TextContent>> Flow<T>.filterExactCommands(
|
||||
fun <T : ContentMessage<TextContent>> Flow<T>.filterCommandsInsideTextMessages(
|
||||
commandRegex: Regex
|
||||
) = filter { contentMessage ->
|
||||
contentMessage.content.fullEntitiesList().any {
|
||||
contentMessage.content.textSources.any {
|
||||
(it as? BotCommandTextSource) ?.let { commandRegex.matches(it.command) } == true
|
||||
}
|
||||
}
|
||||
@ -70,7 +71,7 @@ fun <T : ContentMessage<TextContent>> Flow<T>.filterCommandsInsideTextMessages(
|
||||
fun <T : ContentMessage<TextContent>> Flow<T>.filterCommandsWithArgs(
|
||||
commandRegex: Regex
|
||||
) = mapNotNull { contentMessage ->
|
||||
val allEntities = contentMessage.content.fullEntitiesList()
|
||||
val allEntities = contentMessage.content.textSources
|
||||
(allEntities.firstOrNull() as? BotCommandTextSource) ?.let {
|
||||
if (commandRegex.matches(it.command)) {
|
||||
contentMessage to allEntities.flatMap {
|
||||
|
@ -0,0 +1,74 @@
|
||||
package dev.inmo.tgbotapi.extensions.utils.shortcuts
|
||||
|
||||
import dev.inmo.tgbotapi.extensions.utils.aggregateFlows
|
||||
import dev.inmo.tgbotapi.types.message.ChannelEventMessage
|
||||
import dev.inmo.tgbotapi.types.message.ChatEvents.*
|
||||
import dev.inmo.tgbotapi.types.message.ChatEvents.abstracts.*
|
||||
import dev.inmo.tgbotapi.types.message.abstracts.*
|
||||
import dev.inmo.tgbotapi.types.message.content.abstracts.MessageContent
|
||||
import dev.inmo.tgbotapi.updateshandlers.FlowsUpdatesFilter
|
||||
import dev.inmo.tgbotapi.utils.RiskFeature
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.flow.*
|
||||
|
||||
/**
|
||||
* @param scopeToIncludeChannels This parameter is required when you want to include [textMessages] for channels too.
|
||||
* In this case will be created new channel which will aggregate messages from [FlowsUpdatesFilter.messageFlow] and
|
||||
* [FlowsUpdatesFilter.channelPostFlow]. In case it is null will be used [Flow]s mapping
|
||||
*/
|
||||
@Suppress("UNCHECKED_CAST", "NOTHING_TO_INLINE")
|
||||
@RiskFeature("Use with caution")
|
||||
inline fun FlowsUpdatesFilter.filterChannelEvents(): Flow<ChannelEventMessage<*>> = channelPostFlow.mapNotNull {
|
||||
it.data as? ChannelEventMessage<*>
|
||||
}
|
||||
|
||||
/**
|
||||
* @param scopeToIncludeChannels This parameter is required when you want to include [textMessages] for channels too.
|
||||
* In this case will be created new channel which will aggregate messages from [FlowsUpdatesFilter.messageFlow] and
|
||||
* [FlowsUpdatesFilter.channelPostFlow]. In case it is null will be used [Flow]s mapping
|
||||
*/
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
@RiskFeature("Use with caution")
|
||||
inline fun <reified T: GroupEventMessage<*>> FlowsUpdatesFilter.filterGroupEvents(): Flow<T> = messageFlow.mapNotNull {
|
||||
it.data as? T
|
||||
}
|
||||
|
||||
@RiskFeature("Use with caution")
|
||||
inline fun <reified T: ChatEvent, reified O: ChatEventMessage<T>> Flow<ChatEventMessage<*>>.filterByChatEvent(): Flow<O> = mapNotNull {
|
||||
if (it.chatEvent is T) {
|
||||
it as? O
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
@RiskFeature("Use with caution")
|
||||
inline fun <reified T : ChannelEvent> Flow<ChatEventMessage<*>>.channelEvents() = filterByChatEvent<T, ChannelEventMessage<T>>()
|
||||
inline fun Flow<ChatEventMessage<*>>.channelCreatedEvents() = channelEvents<ChannelChatCreated>()
|
||||
inline fun Flow<ChatEventMessage<*>>.deletedChannelPhotoEvents() = channelEvents<DeleteChatPhoto>()
|
||||
inline fun Flow<ChatEventMessage<*>>.newChannelPhotoEvents() = channelEvents<NewChatPhoto>()
|
||||
inline fun Flow<ChatEventMessage<*>>.newChannelTitleEvents() = channelEvents<NewChatTitle>()
|
||||
inline fun Flow<ChatEventMessage<*>>.newChannelPinnedMessageEvents() = channelEvents<PinnedMessage>()
|
||||
|
||||
@RiskFeature("Use with caution")
|
||||
inline fun <reified T : GroupEvent> Flow<ChatEventMessage<*>>.groupEvents() = filterByChatEvent<T, GroupEventMessage<T>>()
|
||||
inline fun Flow<ChatEventMessage<*>>.groupCreatedEvents() = groupEvents<GroupChatCreated>()
|
||||
inline fun Flow<ChatEventMessage<*>>.deletedGroupPhotoEvents() = groupEvents<DeleteChatPhoto>()
|
||||
inline fun Flow<ChatEventMessage<*>>.newGroupMembersEvents() = groupEvents<NewChatMembers>()
|
||||
inline fun Flow<ChatEventMessage<*>>.leftGroupMemberEvents() = groupEvents<LeftChatMember>()
|
||||
inline fun Flow<ChatEventMessage<*>>.newGroupPhotoEvents() = groupEvents<NewChatPhoto>()
|
||||
inline fun Flow<ChatEventMessage<*>>.newGroupTitleEvents() = groupEvents<NewChatTitle>()
|
||||
inline fun Flow<ChatEventMessage<*>>.newGroupPinnedMessageEvents() = groupEvents<PinnedMessage>()
|
||||
inline fun Flow<ChatEventMessage<*>>.proximityAlertTriggeredInGroupEvents() = groupEvents<ProximityAlertTriggered>()
|
||||
|
||||
|
||||
@RiskFeature("Use with caution")
|
||||
inline fun <reified T : SupergroupEvent> Flow<ChatEventMessage<*>>.supergroupEvents() = filterByChatEvent<T, SupergroupEventMessage<T>>()
|
||||
inline fun Flow<ChatEventMessage<*>>.supergroupCreatedEvents() = supergroupEvents<SupergroupChatCreated>()
|
||||
inline fun Flow<ChatEventMessage<*>>.deletedSupergroupPhotoEvents() = supergroupEvents<DeleteChatPhoto>()
|
||||
inline fun Flow<ChatEventMessage<*>>.newSupergroupMembersEvents() = supergroupEvents<NewChatMembers>()
|
||||
inline fun Flow<ChatEventMessage<*>>.leftSupergroupMemberEvents() = supergroupEvents<LeftChatMember>()
|
||||
inline fun Flow<ChatEventMessage<*>>.newSupergroupPhotoEvents() = supergroupEvents<NewChatPhoto>()
|
||||
inline fun Flow<ChatEventMessage<*>>.newSupergroupTitleEvents() = supergroupEvents<NewChatTitle>()
|
||||
inline fun Flow<ChatEventMessage<*>>.newSupergroupPinnedMessageEvents() = supergroupEvents<PinnedMessage>()
|
||||
inline fun Flow<ChatEventMessage<*>>.proximityAlertTriggeredInSupergroupEvents() = supergroupEvents<ProximityAlertTriggered>()
|
Loading…
Reference in New Issue
Block a user