fixes in startGettingUpdates

This commit is contained in:
InsanusMokrassar 2020-03-31 11:28:48 +06:00
parent ee1f115d77
commit 43ac09a79b
5 changed files with 31 additions and 6 deletions

View File

@ -18,6 +18,9 @@
* `AddAnimatedStickerToSet` request was added
* `SetStickerSetThumb` request was added
* Most of sticker actions now implements `StandardStickerSetAction` instead of `StickerSetAction`
* `getUpdatesLimit` was added to be ensure in get updates limit
* `GetUpdates` now will check count of requesting updates and throw exception if it is not in range `1 .. 100`
* `GetUpdates#limit` now is not nullable and by default set up to 100
* `TelegramBotAPI-extensions-api`:
* Extensions `sendDice` was added
* Extension `getMyCommands` request was added
@ -28,6 +31,9 @@
* **All extensions `addStickerToSet` was renamed to `addStaticStickerToSet`**
* Extensions `addAnimatedStickerToSet` was added
* Extensions `setStickerSetThumb` was added
* Extension `startGettingUpdates` now will drop `SentMediaGroupUpdate` in case if it is the last in updates group
and size of retrieved updates is equal to 100 (max count of retrieved updates)
* Extensions `getUpdates` now will receive only not nullable `limit` parameter
## 0.25.0

View File

@ -7,7 +7,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update
suspend fun RequestsExecutor.getUpdates(
offset: UpdateIdentifier? = null,
limit: Int? = null,
limit: Int = getUpdatesLimit.last,
timeout: Seconds? = null,
allowed_updates: List<String>? = ALL_UPDATES_LIST
) = execute(
@ -18,7 +18,7 @@ suspend fun RequestsExecutor.getUpdates(
suspend fun RequestsExecutor.getUpdates(
lastUpdate: Update,
limit: Int? = null,
limit: Int = getUpdatesLimit.last,
timeout: Seconds? = null,
allowed_updates: List<String>? = ALL_UPDATES_LIST
) = getUpdates(

View File

@ -5,8 +5,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.bot.exceptions.RequestExceptio
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.*
import com.github.insanusmokrassar.TelegramBotAPI.types.update.*
import com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdates.*
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update
@ -30,7 +29,20 @@ fun RequestsExecutor.startGettingOfUpdates(
offset = lastUpdateIdentifier?.plus(1),
timeout = timeoutSeconds,
allowed_updates = allowedUpdates
).convertWithMediaGroupUpdates()
).let { originalUpdates ->
val converted = originalUpdates.convertWithMediaGroupUpdates()
/**
* Dirty hack for cases when the media group was retrieved not fully:
*
* We are throw out the last media group and will reretrieve it again in the next get updates
* and it will guarantee that it is full
*/
if (originalUpdates.size == getUpdatesLimit.last && converted.last() is SentMediaGroupUpdate) {
converted - converted.last()
} else {
converted
}
}
supervisorScope {
for (update in updates) {

View File

@ -14,7 +14,7 @@ private val updatesListSerializer = ListSerializer(
@Serializable
data class GetUpdates(
val offset: UpdateIdentifier? = null,// set `last update id + 1` to receive next part of updates
val limit: Int? = null,
val limit: Int = getUpdatesLimit.last,
val timeout: Seconds? = null,
val allowed_updates: List<String>? = ALL_UPDATES_LIST
): SimpleRequest<List<Update>> {
@ -25,4 +25,10 @@ data class GetUpdates(
override val requestSerializer: SerializationStrategy<*>
get() = serializer()
init {
if (limit !in getUpdatesLimit) {
error("GetUpdates request can be called only with limit in range $getUpdatesLimit (actual value is $limit)")
}
}
}

View File

@ -23,6 +23,7 @@ typealias DiceResult = Int
typealias Seconds = Int
val getUpdatesLimit = 1 .. 100
val callbackQueryAnswerLength = 0 until 200
val captionLength = 0 until 1024
val textLength = 0 until 4096