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

commonMain.com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.UpdatesPolling.kt Maven / Gradle / Ivy

package com.github.insanusmokrassar.TelegramBotAPI.utils.extensions

import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor
import com.github.insanusmokrassar.TelegramBotAPI.bot.UpdatesPoller
import com.github.insanusmokrassar.TelegramBotAPI.types.ALL_UPDATES_LIST
import com.github.insanusmokrassar.TelegramBotAPI.types.update.*
import com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdates.*
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update
import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.KtorUpdatesPoller
import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.UpdatesFilter
import kotlinx.coroutines.*

typealias UpdateReceiver = suspend (T) -> Unit

fun RequestsExecutor.startGettingOfUpdates(
    timeoutMillis: Long = 30 * 1000,
    scope: CoroutineScope = CoroutineScope(Dispatchers.Default),
    allowedUpdates: List? = null,
    block: UpdateReceiver
): UpdatesPoller {
    return KtorUpdatesPoller(
        this,
        timeoutMillis.toInt() / 1000,
        allowedUpdates = allowedUpdates ?: ALL_UPDATES_LIST,
        updatesReceiver = block
    ).also {
        it.start(scope)
    }
}

fun RequestsExecutor.startGettingOfUpdates(
    messageCallback: UpdateReceiver? = null,
    messageMediaGroupCallback: UpdateReceiver? = null,
    editedMessageCallback: UpdateReceiver? = null,
    editedMessageMediaGroupCallback: UpdateReceiver? = null,
    channelPostCallback: UpdateReceiver? = null,
    channelPostMediaGroupCallback: UpdateReceiver? = null,
    editedChannelPostCallback: UpdateReceiver? = null,
    editedChannelPostMediaGroupCallback: UpdateReceiver? = null,
    chosenInlineResultCallback: UpdateReceiver? = null,
    inlineQueryCallback: UpdateReceiver? = null,
    callbackQueryCallback: UpdateReceiver? = null,
    shippingQueryCallback: UpdateReceiver? = null,
    preCheckoutQueryCallback: UpdateReceiver? = null,
    pollCallback: UpdateReceiver? = null,
    pollAnswerCallback: UpdateReceiver? = null,
    timeoutMillis: Long = 30 * 1000,
    scope: CoroutineScope = GlobalScope
): UpdatesPoller {
    val filter = UpdatesFilter(
        messageCallback,
        messageMediaGroupCallback,
        editedMessageCallback,
        editedMessageMediaGroupCallback,
        channelPostCallback,
        channelPostMediaGroupCallback,
        editedChannelPostCallback,
        editedChannelPostMediaGroupCallback,
        chosenInlineResultCallback,
        inlineQueryCallback,
        callbackQueryCallback,
        shippingQueryCallback,
        preCheckoutQueryCallback,
        pollCallback,
        pollAnswerCallback
    )
    return startGettingOfUpdates(
        timeoutMillis,
        scope,
        filter.allowedUpdates,
        filter.asUpdateReceiver
    )
}

fun RequestsExecutor.startGettingOfUpdates(
    messageCallback: UpdateReceiver? = null,
    mediaGroupCallback: UpdateReceiver? = null,
    editedMessageCallback: UpdateReceiver? = null,
    channelPostCallback: UpdateReceiver? = null,
    editedChannelPostCallback: UpdateReceiver? = null,
    chosenInlineResultCallback: UpdateReceiver? = null,
    inlineQueryCallback: UpdateReceiver? = null,
    callbackQueryCallback: UpdateReceiver? = null,
    shippingQueryCallback: UpdateReceiver? = null,
    preCheckoutQueryCallback: UpdateReceiver? = null,
    pollCallback: UpdateReceiver? = null,
    pollAnswerCallback: UpdateReceiver? = null,
    timeoutMillis: Long = 30 * 1000,
    scope: CoroutineScope = CoroutineScope(Dispatchers.Default)
): UpdatesPoller = startGettingOfUpdates(
    messageCallback = messageCallback,
    messageMediaGroupCallback = mediaGroupCallback,
    editedMessageCallback = editedMessageCallback,
    editedMessageMediaGroupCallback = mediaGroupCallback,
    channelPostCallback = channelPostCallback,
    channelPostMediaGroupCallback = mediaGroupCallback,
    editedChannelPostCallback = editedChannelPostCallback,
    editedChannelPostMediaGroupCallback = mediaGroupCallback,
    chosenInlineResultCallback = chosenInlineResultCallback,
    inlineQueryCallback = inlineQueryCallback,
    callbackQueryCallback = callbackQueryCallback,
    shippingQueryCallback = shippingQueryCallback,
    preCheckoutQueryCallback = preCheckoutQueryCallback,
    pollCallback = pollCallback,
    pollAnswerCallback = pollAnswerCallback,
    timeoutMillis = timeoutMillis,
    scope = scope
)




© 2015 - 2024 Weber Informatics LLC | Privacy Policy