mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2024-12-22 16:47:13 +00:00
add a lot of extension for FlowUpdatesFilter and Flow
This commit is contained in:
parent
ce9f7f35dc
commit
790959ea49
13
CHANGELOG.md
13
CHANGELOG.md
@ -52,6 +52,16 @@
|
||||
### 0.27.11
|
||||
|
||||
* `TelegramBotAPI-extensions-utils`:
|
||||
* `Flow<Iterable<T>>.flatMap` extension was added
|
||||
* Extensions for `FlowUpdatesFilter` were added:
|
||||
* `FlowsUpdatesFilter#allSentMessagesFlow` (combination of `messageFlow` and `channelPostFlow`)
|
||||
* `FlowsUpdatesFilter#allSentMediaGroupsFlow` (combination of `messageMediaGroupFlow` and `channelPostMediaGroupFlow`)
|
||||
* `FlowsUpdatesFilter#photoMessagesWithMediaGroups`
|
||||
* `FlowsUpdatesFilter#imageMessagesWithMediaGroups`
|
||||
* `FlowsUpdatesFilter#videoMessagesWithMediaGroups`
|
||||
* `FlowsUpdatesFilter#mediaGroupMessages`
|
||||
* `FlowsUpdatesFilter#mediaGroupPhotosMessages`
|
||||
* `FlowsUpdatesFilter#mediaGroupVideosMessages`
|
||||
* A lot of extensions like `Flow<BaseSentMessageUpdate>#textMessages` were added:
|
||||
* `Flow<BaseSentMessageUpdate>#animationMessages`
|
||||
* `Flow<BaseSentMessageUpdate>#audioMessages`
|
||||
@ -70,6 +80,9 @@
|
||||
* `Flow<BaseSentMessageUpdate>#videoMessages`
|
||||
* `Flow<BaseSentMessageUpdate>#videoNoteMessages`
|
||||
* `Flow<BaseSentMessageUpdate>#voiceMessages`
|
||||
* `Flow<BaseSentMessageUpdate>#mediaGroupMessages`
|
||||
* `Flow<BaseSentMessageUpdate>#mediaGroupPhotosMessages`
|
||||
* `Flow<BaseSentMessageUpdate>#mediaGroupVideosMessages`
|
||||
|
||||
### 0.27.10
|
||||
|
||||
|
@ -21,3 +21,11 @@ fun <T> aggregateFlows(
|
||||
}
|
||||
return bc.asFlow()
|
||||
}
|
||||
|
||||
fun <T> Flow<Iterable<T>>.flatMap(): Flow<T> = flow {
|
||||
collect {
|
||||
it.forEach {
|
||||
emit(it)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,19 @@
|
||||
package com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.extensions
|
||||
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdates.SentMediaGroupUpdate
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseSentMessageUpdate
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.FlowsUpdatesFilter
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.merge
|
||||
|
||||
val FlowsUpdatesFilter.allSentMessagesFlow: Flow<BaseSentMessageUpdate>
|
||||
get() = merge(
|
||||
messageFlow,
|
||||
channelPostFlow
|
||||
)
|
||||
|
||||
val FlowsUpdatesFilter.allSentMediaGroupsFlow: Flow<SentMediaGroupUpdate>
|
||||
get() = merge(
|
||||
messageMediaGroupFlow,
|
||||
channelPostMediaGroupFlow
|
||||
)
|
@ -1,13 +1,16 @@
|
||||
package com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.shortcuts
|
||||
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.aggregateFlows
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.flatMap
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.updates.asContentMessagesFlow
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.CommonMessage
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.ContentMessage
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.Message
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.*
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.abstracts.MediaGroupContent
|
||||
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 com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdates.SentMediaGroupUpdate
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseSentMessageUpdate
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.FlowsUpdatesFilter
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
@ -25,9 +28,20 @@ inline fun <reified T : MessageContent> filterForContentMessage(): suspend (Cont
|
||||
inline fun <reified T: MessageContent> Flow<BaseSentMessageUpdate>.filterContentMessages(
|
||||
): Flow<ContentMessage<T>> = asContentMessagesFlow().mapNotNull(filterForContentMessage())
|
||||
|
||||
inline fun <reified T : MediaGroupContent> Flow<SentMediaGroupUpdate>.filterMediaGroupMessages(
|
||||
): Flow<List<CommonMessage<T>>> = map {
|
||||
it.data.mapNotNull { message ->
|
||||
if (message.content is T) {
|
||||
message as CommonMessage<T>
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @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 agregate messages from [FlowsUpdatesFilter.messageFlow] and
|
||||
* 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")
|
||||
@ -43,6 +57,24 @@ inline fun <reified T: MessageContent> FlowsUpdatesFilter.filterContentMessages(
|
||||
} ?: messageFlow).filterContentMessages()
|
||||
}
|
||||
|
||||
/**
|
||||
* @param scopeToIncludeChannels This parameter is required when you want to include [SentMediaGroupUpdate] 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")
|
||||
inline fun <reified T: MediaGroupContent> FlowsUpdatesFilter.filterMediaGroupMessages(
|
||||
scopeToIncludeChannels: CoroutineScope? = null
|
||||
): Flow<List<CommonMessage<T>>> {
|
||||
return (scopeToIncludeChannels ?.let { scope ->
|
||||
aggregateFlows(
|
||||
scope,
|
||||
messageMediaGroupFlow,
|
||||
channelPostMediaGroupFlow
|
||||
)
|
||||
} ?: messageMediaGroupFlow).filterMediaGroupMessages()
|
||||
}
|
||||
|
||||
fun Flow<BaseSentMessageUpdate>.animationMessages() = filterContentMessages<AnimationContent>()
|
||||
fun FlowsUpdatesFilter.animationMessages(
|
||||
scopeToIncludeChannels: CoroutineScope? = null
|
||||
@ -88,6 +120,12 @@ fun Flow<BaseSentMessageUpdate>.imageMessages() = photoMessages()
|
||||
fun FlowsUpdatesFilter.photoMessages(
|
||||
scopeToIncludeChannels: CoroutineScope? = null
|
||||
) = filterContentMessages<PhotoContent>(scopeToIncludeChannels)
|
||||
fun FlowsUpdatesFilter.photoMessagesWithMediaGroups(
|
||||
scopeToIncludeChannels: CoroutineScope? = null
|
||||
) = merge(
|
||||
filterContentMessages<PhotoContent>(scopeToIncludeChannels),
|
||||
mediaGroupPhotosMessages(scopeToIncludeChannels).flatMap()
|
||||
)
|
||||
/**
|
||||
* Shortcut for [photoMessages]
|
||||
*/
|
||||
@ -95,6 +133,9 @@ fun FlowsUpdatesFilter.photoMessages(
|
||||
inline fun FlowsUpdatesFilter.imageMessages(
|
||||
scopeToIncludeChannels: CoroutineScope? = null
|
||||
) = photoMessages(scopeToIncludeChannels)
|
||||
fun FlowsUpdatesFilter.imageMessagesWithMediaGroups(
|
||||
scopeToIncludeChannels: CoroutineScope? = null
|
||||
) = photoMessagesWithMediaGroups(scopeToIncludeChannels)
|
||||
|
||||
fun Flow<BaseSentMessageUpdate>.pollMessages() = filterContentMessages<PollContent>()
|
||||
fun FlowsUpdatesFilter.pollMessages(
|
||||
@ -120,6 +161,12 @@ fun Flow<BaseSentMessageUpdate>.videoMessages() = filterContentMessages<VideoCon
|
||||
fun FlowsUpdatesFilter.videoMessages(
|
||||
scopeToIncludeChannels: CoroutineScope? = null
|
||||
) = filterContentMessages<VideoContent>(scopeToIncludeChannels)
|
||||
fun FlowsUpdatesFilter.videoMessagesWithMediaGroups(
|
||||
scopeToIncludeChannels: CoroutineScope? = null
|
||||
) = merge(
|
||||
filterContentMessages<VideoContent>(scopeToIncludeChannels),
|
||||
mediaGroupVideosMessages(scopeToIncludeChannels).flatMap()
|
||||
)
|
||||
|
||||
fun Flow<BaseSentMessageUpdate>.videoNoteMessages() = filterContentMessages<VideoNoteContent>()
|
||||
fun FlowsUpdatesFilter.videoNoteMessages(
|
||||
@ -132,3 +179,17 @@ fun FlowsUpdatesFilter.voiceMessages(
|
||||
) = filterContentMessages<VoiceContent>(scopeToIncludeChannels)
|
||||
|
||||
|
||||
fun Flow<SentMediaGroupUpdate>.mediaGroupMessages() = filterMediaGroupMessages<MediaGroupContent>()
|
||||
fun FlowsUpdatesFilter.mediaGroupMessages(
|
||||
scopeToIncludeChannels: CoroutineScope? = null
|
||||
) = filterMediaGroupMessages<MediaGroupContent>(scopeToIncludeChannels)
|
||||
|
||||
fun Flow<SentMediaGroupUpdate>.mediaGroupPhotosMessages() = filterMediaGroupMessages<PhotoContent>()
|
||||
fun FlowsUpdatesFilter.mediaGroupPhotosMessages(
|
||||
scopeToIncludeChannels: CoroutineScope? = null
|
||||
) = filterMediaGroupMessages<PhotoContent>(scopeToIncludeChannels)
|
||||
|
||||
fun Flow<SentMediaGroupUpdate>.mediaGroupVideosMessages() = filterMediaGroupMessages<VideoContent>()
|
||||
fun FlowsUpdatesFilter.mediaGroupVideosMessages(
|
||||
scopeToIncludeChannels: CoroutineScope? = null
|
||||
) = filterMediaGroupMessages<VideoContent>(scopeToIncludeChannels)
|
||||
|
Loading…
Reference in New Issue
Block a user