1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-25 19:48:43 +00:00

getting updates refactoring

This commit is contained in:
InsanusMokrassar 2020-03-17 20:24:31 +06:00
parent be64287c42
commit d61aa8b50e
9 changed files with 67 additions and 29 deletions

View File

@ -24,11 +24,14 @@
* `EditMessageUpdate` now is implementing `BaseEditMessageUpdate` interface * `EditMessageUpdate` now is implementing `BaseEditMessageUpdate` interface
* `ChannelPostUpdate` now is implementing `BaseSentMessageUpdate` interface * `ChannelPostUpdate` now is implementing `BaseSentMessageUpdate` interface
* `MessageUpdate` now is implementing `BaseSentMessageUpdate` interface * `MessageUpdate` now is implementing `BaseSentMessageUpdate` interface
* `UpdatesPoller` and all its usages, childs and childs usages now are deprecated
* `GetUpdates#timeout` type now is `Seconds` (in fact it is `Int` as previously)
* `TelegramBotAPI-extensions-api`: * `TelegramBotAPI-extensions-api`:
* All functions from `com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.UpdatesPolling` now available * All functions from `com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.UpdatesPolling` now available
in package `com.github.insanusmokrassar.TelegramBotAPI.extensions.api.updates.UpdatesPolling` in package `com.github.insanusmokrassar.TelegramBotAPI.extensions.api.updates.UpdatesPolling`
* Now new method of getting updates available: `startGettingUpdates` with `UpdatesFilter` as incoming first * Now new method of getting updates available: `startGettingUpdates` with `UpdatesFilter` as incoming first
parameter parameter
* `startGettingUpdates` with `receiver` and `allowedUpdates` parameters now will handle updates by itself
## 0.23.0 TelegramBotAPI 4.6 ## 0.23.0 TelegramBotAPI 4.6

View File

@ -2,14 +2,13 @@ package com.github.insanusmokrassar.TelegramBotAPI.extensions.api
import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor
import com.github.insanusmokrassar.TelegramBotAPI.requests.GetUpdates import com.github.insanusmokrassar.TelegramBotAPI.requests.GetUpdates
import com.github.insanusmokrassar.TelegramBotAPI.types.ALL_UPDATES_LIST import com.github.insanusmokrassar.TelegramBotAPI.types.*
import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update
suspend fun RequestsExecutor.getUpdates( suspend fun RequestsExecutor.getUpdates(
offset: UpdateIdentifier? = null, offset: UpdateIdentifier? = null,
limit: Int? = null, limit: Int? = null,
timeout: Int? = null, timeout: Seconds? = null,
allowed_updates: List<String>? = ALL_UPDATES_LIST allowed_updates: List<String>? = ALL_UPDATES_LIST
) = execute( ) = execute(
GetUpdates( GetUpdates(
@ -20,7 +19,7 @@ suspend fun RequestsExecutor.getUpdates(
suspend fun RequestsExecutor.getUpdates( suspend fun RequestsExecutor.getUpdates(
lastUpdate: Update, lastUpdate: Update,
limit: Int? = null, limit: Int? = null,
timeout: Int? = null, timeout: Seconds? = null,
allowed_updates: List<String>? = ALL_UPDATES_LIST allowed_updates: List<String>? = ALL_UPDATES_LIST
) = getUpdates( ) = getUpdates(
lastUpdate.updateId + 1, limit, timeout, allowed_updates lastUpdate.updateId + 1, limit, timeout, allowed_updates

View File

@ -1,15 +1,32 @@
package com.github.insanusmokrassar.TelegramBotAPI.extensions.api.InternalUtils package com.github.insanusmokrassar.TelegramBotAPI.extensions.api.InternalUtils
import com.github.insanusmokrassar.TelegramBotAPI.types.MediaGroupIdentifier import com.github.insanusmokrassar.TelegramBotAPI.types.MediaGroupIdentifier
import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.MediaGroupMessage import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.MediaGroupMessage
import com.github.insanusmokrassar.TelegramBotAPI.types.update.* import com.github.insanusmokrassar.TelegramBotAPI.types.update.*
import com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdates.* import com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdates.*
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.* import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.*
internal fun List<BaseMessageUpdate>.convertWithMediaGroupUpdates(): List<Update> { internal fun Update.lastUpdateIdentifier(): UpdateIdentifier {
return if (this is SentMediaGroupUpdate) {
origins.last().updateId
} else {
updateId
}
}
internal fun List<Update>.lastUpdateIdentifier(): UpdateIdentifier? {
return maxBy { it.updateId } ?.lastUpdateIdentifier()
}
internal fun List<Update>.convertWithMediaGroupUpdates(): List<Update> {
val resultUpdates = mutableListOf<Update>() val resultUpdates = mutableListOf<Update>()
val mediaGroups = mutableMapOf<MediaGroupIdentifier, MutableList<BaseMessageUpdate>>() val mediaGroups = mutableMapOf<MediaGroupIdentifier, MutableList<BaseMessageUpdate>>()
for (update in this) { for (update in this) {
if (update !is BaseMessageUpdate) {
resultUpdates.add(update)
continue
}
val asEditMediaGroupMessage = update.toEditMediaGroupUpdate() val asEditMediaGroupMessage = update.toEditMediaGroupUpdate()
if (asEditMediaGroupMessage != null) { if (asEditMediaGroupMessage != null) {
resultUpdates.add(asEditMediaGroupMessage) resultUpdates.add(asEditMediaGroupMessage)
@ -27,7 +44,8 @@ internal fun List<BaseMessageUpdate>.convertWithMediaGroupUpdates(): List<Update
resultUpdates.add(mediaGroupUpdate) resultUpdates.add(mediaGroupUpdate)
} }
} }
return resultUpdates.sortedBy { it.updateId } resultUpdates.sortBy { it.updateId }
return resultUpdates
} }
internal fun List<BaseMessageUpdate>.toSentMediaGroupUpdate(): SentMediaGroupUpdate? = (this as? SentMediaGroupUpdate) ?: let { internal fun List<BaseMessageUpdate>.toSentMediaGroupUpdate(): SentMediaGroupUpdate? = (this as? SentMediaGroupUpdate) ?: let {

View File

@ -1,36 +1,49 @@
package com.github.insanusmokrassar.TelegramBotAPI.extensions.api.updates package com.github.insanusmokrassar.TelegramBotAPI.extensions.api.updates
import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor
import com.github.insanusmokrassar.TelegramBotAPI.bot.UpdatesPoller import com.github.insanusmokrassar.TelegramBotAPI.extensions.api.InternalUtils.convertWithMediaGroupUpdates
import com.github.insanusmokrassar.TelegramBotAPI.types.ALL_UPDATES_LIST 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.*
import com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdates.* import com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdates.*
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update
import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.* import com.github.insanusmokrassar.TelegramBotAPI.updateshandlers.*
import kotlinx.coroutines.* import kotlinx.coroutines.*
fun RequestsExecutor.startGettingOfUpdates( fun RequestsExecutor.startGettingOfUpdates(
timeoutMillis: Long = 30 * 1000, timeoutMillis: Seconds = 30,
scope: CoroutineScope = CoroutineScope(Dispatchers.Default), scope: CoroutineScope = CoroutineScope(Dispatchers.Default),
allowedUpdates: List<String>? = null, allowedUpdates: List<String>? = null,
block: UpdateReceiver<Update> updatesReceiver: UpdateReceiver<Update>
): UpdatesPoller { ): Job = scope.launch {
return KtorUpdatesPoller( var lastUpdateIdentifier: UpdateIdentifier? = null
this,
timeoutMillis.toInt() / 1000, while (isActive) {
allowedUpdates = allowedUpdates ?: ALL_UPDATES_LIST, supervisorScope {
updatesReceiver = block val updates = getUpdates(
).also { offset = lastUpdateIdentifier,
it.start(scope) timeout = timeoutMillis,
allowed_updates = allowedUpdates
).convertWithMediaGroupUpdates()
supervisorScope {
for (update in updates) {
updatesReceiver(update)
lastUpdateIdentifier = update.lastUpdateIdentifier()
}
}
}
} }
} }
fun RequestsExecutor.startGettingOfUpdates( fun RequestsExecutor.startGettingOfUpdates(
updatesFilter: UpdatesFilter, updatesFilter: UpdatesFilter,
timeoutMillis: Long = 30 * 1000, timeoutMillis: Seconds = 30,
scope: CoroutineScope = CoroutineScope(Dispatchers.Default) scope: CoroutineScope = CoroutineScope(Dispatchers.Default)
): UpdatesPoller = startGettingOfUpdates( ): Job = startGettingOfUpdates(
timeoutMillis, timeoutMillis,
scope, scope,
updatesFilter.allowedUpdates, updatesFilter.allowedUpdates,
@ -53,9 +66,9 @@ fun RequestsExecutor.startGettingOfUpdates(
preCheckoutQueryCallback: UpdateReceiver<PreCheckoutQueryUpdate>? = null, preCheckoutQueryCallback: UpdateReceiver<PreCheckoutQueryUpdate>? = null,
pollCallback: UpdateReceiver<PollUpdate>? = null, pollCallback: UpdateReceiver<PollUpdate>? = null,
pollAnswerCallback: UpdateReceiver<PollAnswerUpdate>? = null, pollAnswerCallback: UpdateReceiver<PollAnswerUpdate>? = null,
timeoutMillis: Long = 30 * 1000, timeoutMillis: Seconds = 30,
scope: CoroutineScope = GlobalScope scope: CoroutineScope = GlobalScope
): UpdatesPoller { ): Job {
return startGettingOfUpdates( return startGettingOfUpdates(
SimpleUpdatesFilter( SimpleUpdatesFilter(
messageCallback, messageCallback,
@ -92,9 +105,9 @@ fun RequestsExecutor.startGettingOfUpdates(
preCheckoutQueryCallback: UpdateReceiver<PreCheckoutQueryUpdate>? = null, preCheckoutQueryCallback: UpdateReceiver<PreCheckoutQueryUpdate>? = null,
pollCallback: UpdateReceiver<PollUpdate>? = null, pollCallback: UpdateReceiver<PollUpdate>? = null,
pollAnswerCallback: UpdateReceiver<PollAnswerUpdate>? = null, pollAnswerCallback: UpdateReceiver<PollAnswerUpdate>? = null,
timeoutMillis: Long = 30 * 1000, timeoutMillis: Seconds = 30,
scope: CoroutineScope = CoroutineScope(Dispatchers.Default) scope: CoroutineScope = CoroutineScope(Dispatchers.Default)
): UpdatesPoller = startGettingOfUpdates( ): Job = startGettingOfUpdates(
messageCallback = messageCallback, messageCallback = messageCallback,
messageMediaGroupCallback = mediaGroupCallback, messageMediaGroupCallback = mediaGroupCallback,
editedMessageCallback = editedMessageCallback, editedMessageCallback = editedMessageCallback,

View File

@ -4,6 +4,7 @@ import io.ktor.utils.io.core.Closeable
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
@Deprecated("Deprecated due to more simple way to get updates using TelegramBotAPI-extensions-api")
interface UpdatesPoller : Closeable { interface UpdatesPoller : Closeable {
fun start(scope: CoroutineScope = CoroutineScope(Dispatchers.Default)) fun start(scope: CoroutineScope = CoroutineScope(Dispatchers.Default))
} }

View File

@ -1,9 +1,7 @@
package com.github.insanusmokrassar.TelegramBotAPI.requests package com.github.insanusmokrassar.TelegramBotAPI.requests
import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.SimpleRequest import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.SimpleRequest
import com.github.insanusmokrassar.TelegramBotAPI.types.ALL_UPDATES_LIST import com.github.insanusmokrassar.TelegramBotAPI.types.*
import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.UpdateSerializerWithoutDeserialization import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.UpdateSerializerWithoutDeserialization
import kotlinx.serialization.* import kotlinx.serialization.*
@ -17,7 +15,7 @@ private val updatesListSerializer = ArrayListSerializer(
data class GetUpdates( data class GetUpdates(
val offset: UpdateIdentifier? = null,// set `last update id + 1` to receive next part of updates val offset: UpdateIdentifier? = null,// set `last update id + 1` to receive next part of updates
val limit: Int? = null, val limit: Int? = null,
val timeout: Int? = null, val timeout: Seconds? = null,
val allowed_updates: List<String>? = ALL_UPDATES_LIST val allowed_updates: List<String>? = ALL_UPDATES_LIST
): SimpleRequest<List<Update>> { ): SimpleRequest<List<Update>> {
override fun method(): String = "getUpdates" override fun method(): String = "getUpdates"

View File

@ -20,6 +20,8 @@ typealias PollIdentifier = String
typealias StickerSetName = String typealias StickerSetName = String
typealias FileUniqueId = String typealias FileUniqueId = String
typealias Seconds = Int
val callbackQueryAnswerLength = 0 until 200 val callbackQueryAnswerLength = 0 until 200
val captionLength = 0 until 1024 val captionLength = 0 until 1024
val textLength = 0 until 4096 val textLength = 0 until 4096

View File

@ -17,6 +17,7 @@ import io.ktor.client.engine.HttpClientEngine
import kotlinx.coroutines.* import kotlinx.coroutines.*
import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.channels.Channel
@Deprecated("Deprecated due to more simple way to get updates using TelegramBotAPI-extensions-api")
fun KtorUpdatesPoller( fun KtorUpdatesPoller(
telegramAPIUrlsKeeper: TelegramAPIUrlsKeeper, telegramAPIUrlsKeeper: TelegramAPIUrlsKeeper,
engine: HttpClientEngine, engine: HttpClientEngine,
@ -41,6 +42,7 @@ fun KtorUpdatesPoller(
) )
} }
@Deprecated("Deprecated due to more simple way to get updates using TelegramBotAPI-extensions-api")
class KtorUpdatesPoller( class KtorUpdatesPoller(
private val executor: RequestsExecutor, private val executor: RequestsExecutor,
private val timeoutSeconds: Int? = null, private val timeoutSeconds: Int? = null,

View File

@ -10,6 +10,7 @@ import io.ktor.client.engine.cio.CIO
import io.ktor.client.engine.cio.endpoint import io.ktor.client.engine.cio.endpoint
import io.ktor.util.KtorExperimentalAPI import io.ktor.util.KtorExperimentalAPI
@Deprecated("Deprecated due to more simple way to get updates using TelegramBotAPI-extensions-api")
@KtorExperimentalAPI @KtorExperimentalAPI
fun KtorUpdatesPoller( fun KtorUpdatesPoller(
telegramAPIUrlsKeeper: TelegramAPIUrlsKeeper, telegramAPIUrlsKeeper: TelegramAPIUrlsKeeper,
@ -43,6 +44,7 @@ fun KtorUpdatesPoller(
) )
} }
@Deprecated("Deprecated due to more simple way to get updates using TelegramBotAPI-extensions-api")
@KtorExperimentalAPI @KtorExperimentalAPI
fun KtorUpdatesPoller( fun KtorUpdatesPoller(
telegramAPIUrlsKeeper: TelegramAPIUrlsKeeper, telegramAPIUrlsKeeper: TelegramAPIUrlsKeeper,