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

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

package com.github.insanusmokrassar.TelegramBotAPI.extensions.api.updates

import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor
import com.github.insanusmokrassar.TelegramBotAPI.bot.exceptions.RequestException
import com.github.insanusmokrassar.TelegramBotAPI.extensions.api.InternalUtils.convertWithMediaGroupUpdates
import com.github.insanusmokrassar.TelegramBotAPI.extensions.api.InternalUtils.lastUpdateIdentifier
import com.github.insanusmokrassar.TelegramBotAPI.extensions.api.getUpdates
import com.github.insanusmokrassar.TelegramBotAPI.types.Seconds
import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier
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.*
import io.ktor.client.features.HttpRequestTimeoutException
import kotlinx.coroutines.*

fun RequestsExecutor.startGettingOfUpdates(
    timeoutSeconds: Seconds = 30,
    scope: CoroutineScope = CoroutineScope(Dispatchers.Default),
    exceptionsHandler: (suspend (Exception) -> Unit)? = null,
    allowedUpdates: List? = null,
    updatesReceiver: UpdateReceiver
): Job = scope.launch {
    var lastUpdateIdentifier: UpdateIdentifier? = null

    while (isActive) {
        try {
            supervisorScope {
                val updates = getUpdates(
                    offset = lastUpdateIdentifier?.plus(1),
                    timeout = timeoutSeconds,
                    allowed_updates = allowedUpdates
                ).convertWithMediaGroupUpdates()

                supervisorScope {
                    for (update in updates) {
                        updatesReceiver(update)

                        lastUpdateIdentifier = update.lastUpdateIdentifier()
                    }
                }
            }
        } catch (e: HttpRequestTimeoutException) {
            exceptionsHandler ?.invoke(e) // it is ok due to mechanism of long polling
        } catch (e: RequestException) {
            exceptionsHandler ?.invoke(e) // it is not ok, but in most cases it will mean that there is some limit for requests count
            delay(1000L)
        } catch (e: Exception) {
            exceptionsHandler ?.invoke(e)
        }
    }
}

fun RequestsExecutor.startGettingOfUpdates(
    updatesFilter: UpdatesFilter,
    timeoutSeconds: Seconds = 30,
    exceptionsHandler: (suspend (Exception) -> Unit)? = null,
    scope: CoroutineScope = CoroutineScope(Dispatchers.Default)
): Job = startGettingOfUpdates(
    timeoutSeconds,
    scope,
    exceptionsHandler,
    updatesFilter.allowedUpdates,
    updatesFilter.asUpdateReceiver
)

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,
    timeoutSeconds: Seconds = 30,
    exceptionsHandler: (suspend (Exception) -> Unit)? = null,
    scope: CoroutineScope = GlobalScope
): Job {
    return startGettingOfUpdates(
        SimpleUpdatesFilter(
            messageCallback,
            messageMediaGroupCallback,
            editedMessageCallback,
            editedMessageMediaGroupCallback,
            channelPostCallback,
            channelPostMediaGroupCallback,
            editedChannelPostCallback,
            editedChannelPostMediaGroupCallback,
            chosenInlineResultCallback,
            inlineQueryCallback,
            callbackQueryCallback,
            shippingQueryCallback,
            preCheckoutQueryCallback,
            pollCallback,
            pollAnswerCallback
        ),
        timeoutSeconds,
        exceptionsHandler,
        scope
    )
}

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,
    timeoutSeconds: Seconds = 30,
    exceptionsHandler: (suspend (Exception) -> Unit)? = null,
    scope: CoroutineScope = CoroutineScope(Dispatchers.Default)
): Job = 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,
    timeoutSeconds = timeoutSeconds,
    exceptionsHandler = exceptionsHandler,
    scope = scope
)




© 2015 - 2025 Weber Informatics LLC | Privacy Policy