All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.dev.inmo.tgbotapi.extensions.utils.CommonMessageConversations.kt Maven / Gradle / Ivy

There is a newer version: 20.0.1
Show newest version
package dev.inmo.tgbotapi.extensions.utils

import dev.inmo.tgbotapi.types.message.abstracts.*
import dev.inmo.tgbotapi.types.message.content.MediaGroupContent
import dev.inmo.tgbotapi.types.message.content.MessageContent
import kotlinx.coroutines.flow.*

/**
 * Simple factory to convert [ContentMessage] to a [CommonMessage]
 */
fun > Flow.onlyCommonMessages() = filterIsInstance>()

/**
 * Shortcut for [onlyCommonMessages]
 */
@Suppress("NOTHING_TO_INLINE")
inline fun > Flow.commonMessages() = onlyCommonMessages()

/**
 * Filter the messages and checking that incoming [CommonMessage] is [PossiblySentViaBotCommonMessage] and its
 * [PossiblySentViaBotCommonMessage.senderBot] is not null
 */
fun > Flow.onlySentViaBot() = mapNotNull {
    if (it is PossiblySentViaBot && it.senderBot != null) {
        it
    } else {
        null
    }
}

/**
 * Filter the messages and checking that incoming [CommonMessage] not is [PossiblySentViaBotCommonMessage] or its
 * [PossiblySentViaBotCommonMessage.senderBot] is null
 */
fun > Flow.withoutSentViaBot() = filter {
    it !is PossiblySentViaBot || it.senderBot == null
}

/**
 * Filter the messages and checking that incoming [ContentMessage.content] is not [MediaGroupContent]
 */
fun > Flow.withoutMediaGroups() = filter {
    it.content !is MediaGroupContent<*>
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy