mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2024-11-22 08:13:47 +00:00
commit
5d1c59ff75
37
CHANGELOG.md
37
CHANGELOG.md
@ -49,6 +49,43 @@
|
||||
* `closePollExactAfter`
|
||||
* `closePollAfter`
|
||||
|
||||
### 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#sentMessages`
|
||||
* `FlowsUpdatesFilter#sentMessagesWithMediaGroups`
|
||||
* `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`
|
||||
* `Flow<BaseSentMessageUpdate>#contactMessages`
|
||||
* `Flow<BaseSentMessageUpdate>#diceMessages`
|
||||
* `Flow<BaseSentMessageUpdate>#documentMessages`
|
||||
* `Flow<BaseSentMessageUpdate>#gameMessages`
|
||||
* `Flow<BaseSentMessageUpdate>#invoiceMessages`
|
||||
* `Flow<BaseSentMessageUpdate>#locationMessages`
|
||||
* `Flow<BaseSentMessageUpdate>#photoMessages`
|
||||
* `Flow<BaseSentMessageUpdate>#imageMessages`
|
||||
* `Flow<BaseSentMessageUpdate>#pollMessages`
|
||||
* `Flow<BaseSentMessageUpdate>#stickerMessages`
|
||||
* `Flow<BaseSentMessageUpdate>#textMessages`
|
||||
* `Flow<BaseSentMessageUpdate>#venueMessages`
|
||||
* `Flow<BaseSentMessageUpdate>#videoMessages`
|
||||
* `Flow<BaseSentMessageUpdate>#videoNoteMessages`
|
||||
* `Flow<BaseSentMessageUpdate>#voiceMessages`
|
||||
* `Flow<BaseSentMessageUpdate>#mediaGroupMessages`
|
||||
* `Flow<BaseSentMessageUpdate>#mediaGroupPhotosMessages`
|
||||
* `Flow<BaseSentMessageUpdate>#mediaGroupVideosMessages`
|
||||
|
||||
### 0.27.10
|
||||
|
||||
* `TelegramBotAPI-extensions-api`:
|
||||
|
@ -5,6 +5,9 @@ import kotlinx.coroutines.channels.BroadcastChannel
|
||||
import kotlinx.coroutines.channels.Channel
|
||||
import kotlinx.coroutines.flow.*
|
||||
|
||||
/**
|
||||
* Analog of [merge] function for [Flow]s. The difference is in the usage of [BroadcastChannel] in this case
|
||||
*/
|
||||
fun <T> aggregateFlows(
|
||||
withScope: CoroutineScope,
|
||||
vararg flows: Flow<T>,
|
||||
@ -18,3 +21,19 @@ fun <T> aggregateFlows(
|
||||
}
|
||||
return bc.asFlow()
|
||||
}
|
||||
|
||||
fun <T> Flow<Iterable<T>>.flatMap(): Flow<T> = flow {
|
||||
collect {
|
||||
it.forEach {
|
||||
emit(it)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun <T, R> Flow<T>.flatMap(mapper: (T) -> Iterable<R>): Flow<R> = flow {
|
||||
collect {
|
||||
mapper(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,12 +1,17 @@
|
||||
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.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
|
||||
import kotlinx.coroutines.flow.*
|
||||
@ -19,60 +24,120 @@ inline fun <reified T : MessageContent> filterForContentMessage(): suspend (Cont
|
||||
}
|
||||
}
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
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")
|
||||
inline fun <reified T: MessageContent> FlowsUpdatesFilter.filterContentMessages(
|
||||
scopeToIncludeChannels: CoroutineScope? = null
|
||||
): Flow<ContentMessage<T>> {
|
||||
val filter = filterForContentMessage<T>()
|
||||
return scopeToIncludeChannels ?.let { scope ->
|
||||
return (scopeToIncludeChannels ?.let { scope ->
|
||||
aggregateFlows(
|
||||
scope,
|
||||
messageFlow.asContentMessagesFlow().mapNotNull(filter),
|
||||
channelPostFlow.asContentMessagesFlow().mapNotNull(filter)
|
||||
messageFlow,
|
||||
channelPostFlow
|
||||
)
|
||||
} ?: messageFlow.asContentMessagesFlow().mapNotNull(filter)
|
||||
} ?: 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 FlowsUpdatesFilter.sentMessages(
|
||||
scopeToIncludeChannels: CoroutineScope? = null
|
||||
): Flow<ContentMessage<MessageContent>> = filterContentMessages(scopeToIncludeChannels)
|
||||
fun FlowsUpdatesFilter.sentMessagesWithMediaGroups(
|
||||
scopeToIncludeChannels: CoroutineScope? = null
|
||||
): Flow<ContentMessage<MessageContent>> = merge(
|
||||
sentMessages(scopeToIncludeChannels),
|
||||
mediaGroupMessages(scopeToIncludeChannels).flatMap {
|
||||
it.mapNotNull { it as? ContentMessage<MessageContent> }
|
||||
}
|
||||
)
|
||||
|
||||
fun Flow<BaseSentMessageUpdate>.animationMessages() = filterContentMessages<AnimationContent>()
|
||||
fun FlowsUpdatesFilter.animationMessages(
|
||||
scopeToIncludeChannels: CoroutineScope? = null
|
||||
) = filterContentMessages<AnimationContent>(scopeToIncludeChannels)
|
||||
|
||||
fun Flow<BaseSentMessageUpdate>.audioMessages() = filterContentMessages<AudioContent>()
|
||||
fun FlowsUpdatesFilter.audioMessages(
|
||||
scopeToIncludeChannels: CoroutineScope? = null
|
||||
) = filterContentMessages<AudioContent>(scopeToIncludeChannels)
|
||||
|
||||
fun Flow<BaseSentMessageUpdate>.contactMessages() = filterContentMessages<ContactContent>()
|
||||
fun FlowsUpdatesFilter.contactMessages(
|
||||
scopeToIncludeChannels: CoroutineScope? = null
|
||||
) = filterContentMessages<ContactContent>(scopeToIncludeChannels)
|
||||
|
||||
fun Flow<BaseSentMessageUpdate>.diceMessages() = filterContentMessages<DiceContent>()
|
||||
fun FlowsUpdatesFilter.diceMessages(
|
||||
scopeToIncludeChannels: CoroutineScope? = null
|
||||
) = filterContentMessages<DiceContent>(scopeToIncludeChannels)
|
||||
|
||||
fun Flow<BaseSentMessageUpdate>.documentMessages() = filterContentMessages<DocumentContent>()
|
||||
fun FlowsUpdatesFilter.documentMessages(
|
||||
scopeToIncludeChannels: CoroutineScope? = null
|
||||
) = filterContentMessages<DocumentContent>(scopeToIncludeChannels)
|
||||
|
||||
fun Flow<BaseSentMessageUpdate>.gameMessages() = filterContentMessages<GameContent>()
|
||||
fun FlowsUpdatesFilter.gameMessages(
|
||||
scopeToIncludeChannels: CoroutineScope? = null
|
||||
) = filterContentMessages<GameContent>(scopeToIncludeChannels)
|
||||
|
||||
fun Flow<BaseSentMessageUpdate>.invoiceMessages() = filterContentMessages<InvoiceContent>()
|
||||
fun FlowsUpdatesFilter.invoiceMessages(
|
||||
scopeToIncludeChannels: CoroutineScope? = null
|
||||
) = filterContentMessages<InvoiceContent>(scopeToIncludeChannels)
|
||||
|
||||
fun Flow<BaseSentMessageUpdate>.locationMessages() = filterContentMessages<LocationContent>()
|
||||
fun FlowsUpdatesFilter.locationMessages(
|
||||
scopeToIncludeChannels: CoroutineScope? = null
|
||||
) = filterContentMessages<LocationContent>(scopeToIncludeChannels)
|
||||
|
||||
fun Flow<BaseSentMessageUpdate>.photoMessages() = filterContentMessages<PhotoContent>()
|
||||
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]
|
||||
*/
|
||||
@ -80,33 +145,63 @@ 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(
|
||||
scopeToIncludeChannels: CoroutineScope? = null
|
||||
) = filterContentMessages<PollContent>(scopeToIncludeChannels)
|
||||
|
||||
fun Flow<BaseSentMessageUpdate>.stickerMessages() = filterContentMessages<StickerContent>()
|
||||
fun FlowsUpdatesFilter.stickerMessages(
|
||||
scopeToIncludeChannels: CoroutineScope? = null
|
||||
) = filterContentMessages<StickerContent>(scopeToIncludeChannels)
|
||||
|
||||
fun Flow<BaseSentMessageUpdate>.textMessages() = filterContentMessages<TextContent>()
|
||||
fun FlowsUpdatesFilter.textMessages(
|
||||
scopeToIncludeChannels: CoroutineScope? = null
|
||||
) = filterContentMessages<TextContent>(scopeToIncludeChannels)
|
||||
|
||||
fun Flow<BaseSentMessageUpdate>.venueMessages() = filterContentMessages<VenueContent>()
|
||||
fun FlowsUpdatesFilter.venueMessages(
|
||||
scopeToIncludeChannels: CoroutineScope? = null
|
||||
) = filterContentMessages<VenueContent>(scopeToIncludeChannels)
|
||||
|
||||
fun Flow<BaseSentMessageUpdate>.videoMessages() = filterContentMessages<VideoContent>()
|
||||
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(
|
||||
scopeToIncludeChannels: CoroutineScope? = null
|
||||
) = filterContentMessages<VideoNoteContent>(scopeToIncludeChannels)
|
||||
|
||||
fun Flow<BaseSentMessageUpdate>.voiceMessages() = filterContentMessages<VoiceContent>()
|
||||
fun FlowsUpdatesFilter.voiceMessages(
|
||||
scopeToIncludeChannels: CoroutineScope? = null
|
||||
) = 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)
|
||||
|
@ -114,7 +114,7 @@ fun RequestsExecutor.startGettingOfUpdatesByLongPolling(
|
||||
pollAnswerCallback: UpdateReceiver<PollAnswerUpdate>? = null,
|
||||
timeoutSeconds: Seconds = 30,
|
||||
exceptionsHandler: ExceptionHandler<Unit>? = null,
|
||||
scope: CoroutineScope = GlobalScope
|
||||
scope: CoroutineScope = CoroutineScope(Dispatchers.Default)
|
||||
): Job {
|
||||
return startGettingOfUpdatesByLongPolling(
|
||||
SimpleUpdatesFilter(
|
||||
|
@ -9,6 +9,6 @@ ktor_version=1.3.2
|
||||
javax_activation_version=1.1.1
|
||||
|
||||
library_group=com.github.insanusmokrassar
|
||||
library_version=0.27.10
|
||||
library_version=0.27.11
|
||||
|
||||
gradle_bintray_plugin_version=1.8.4
|
||||
|
Loading…
Reference in New Issue
Block a user