mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2024-11-22 00:03:48 +00:00
extensions for CommonMessage
This commit is contained in:
parent
ca4beee95f
commit
130e00b62b
@ -62,6 +62,8 @@
|
||||
* Abstraction `ThumbedWithMimeTypeInlineQueryResult` with `thumbMimeType` field was added
|
||||
* `InlineQueryResultGif` and `InlineQueryResultMpeg4Gif` now extend `ThumbedWithMimeTypeInlineQueryResult`
|
||||
instead of `ThumbedInlineQueryResult`
|
||||
* `TelegramBotAPI-extensions-utils`:
|
||||
* New extensions `onlyCommonMessages`, `onlySentViaBot` and `withoutSentViaBot` was added
|
||||
|
||||
### 0.27.5
|
||||
|
||||
|
@ -0,0 +1,34 @@
|
||||
package com.github.insanusmokrassar.TelegramBotAPI.extensions.utils
|
||||
|
||||
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.abstracts.MessageContent
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.abstracts.PossiblySentViaBotCommonMessage
|
||||
import kotlinx.coroutines.flow.*
|
||||
|
||||
/**
|
||||
* Simple factory to convert [ContentMessage] to a [CommonMessage]
|
||||
*/
|
||||
fun <C: MessageContent, T : ContentMessage<C>> Flow<T>.onlyCommonMessages() = filterIsInstance<CommonMessage<C>>()
|
||||
|
||||
/**
|
||||
* Filter the messages and checking that incoming [CommonMessage] is [PossiblySentViaBotCommonMessage] and its
|
||||
* [PossiblySentViaBotCommonMessage.senderBot] is not null
|
||||
*/
|
||||
fun <T : MessageContent> Flow<CommonMessage<T>>.onlySentViaBot() = mapNotNull {
|
||||
(it as? PossiblySentViaBotCommonMessage) ?.let { possiblySentViaBot ->
|
||||
if (possiblySentViaBot.senderBot != null) {
|
||||
possiblySentViaBot
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter the messages and checking that incoming [CommonMessage] not is [PossiblySentViaBotCommonMessage] or its
|
||||
* [PossiblySentViaBotCommonMessage.senderBot] is null
|
||||
*/
|
||||
fun <T : MessageContent> Flow<CommonMessage<T>>.withoutSentViaBot() = filter {
|
||||
it !is PossiblySentViaBotCommonMessage || it.senderBot == null
|
||||
}
|
@ -12,6 +12,13 @@ fun <T : BaseSentMessageUpdate> Flow<T>.asContentMessagesFlow() = mapNotNull {
|
||||
it.data as? ContentMessage<*>
|
||||
}
|
||||
|
||||
/**
|
||||
* Will map incoming [BaseSentMessageUpdate]s to [CommonMessage] from [BaseSentMessageUpdate.data]
|
||||
*/
|
||||
fun <T : BaseSentMessageUpdate> Flow<T>.asCommonMessagesFlow() = mapNotNull {
|
||||
it.data as? CommonMessage<*>
|
||||
}
|
||||
|
||||
/**
|
||||
* Will map incoming [BaseSentMessageUpdate]s to [ChatEventMessage] from [BaseSentMessageUpdate.data]
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user