mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2025-09-08 01:29:30 +00:00
add support of getBusinessAccountGifts
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
package dev.inmo.tgbotapi.extensions.api.business
|
||||
|
||||
import dev.inmo.tgbotapi.bot.TelegramBot
|
||||
import dev.inmo.tgbotapi.requests.business_connection.GetBusinessAccountGifts
|
||||
import dev.inmo.tgbotapi.types.business_connection.BusinessConnectionId
|
||||
import dev.inmo.tgbotapi.types.OwnedGifts
|
||||
import dev.inmo.tgbotapi.types.gifts.GiftSentOrReceived
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.flow
|
||||
import kotlin.runCatching
|
||||
|
||||
public suspend fun TelegramBot.getBusinessAccountGifts(
|
||||
businessConnectionId: BusinessConnectionId,
|
||||
excludeUnsaved: Boolean = false,
|
||||
excludeSaved: Boolean = false,
|
||||
excludeUnlimited: Boolean = false,
|
||||
excludeLimited: Boolean = false,
|
||||
excludeUnique: Boolean = false,
|
||||
sortByPrice: Boolean = false,
|
||||
offset: String? = null,
|
||||
limit: Int? = null
|
||||
): OwnedGifts<GiftSentOrReceived.ReceivedInBusinessAccount> = execute(
|
||||
GetBusinessAccountGifts(
|
||||
businessConnectionId,
|
||||
excludeUnsaved,
|
||||
excludeSaved,
|
||||
excludeUnlimited,
|
||||
excludeLimited,
|
||||
excludeUnique,
|
||||
sortByPrice,
|
||||
offset,
|
||||
limit
|
||||
)
|
||||
)
|
||||
|
||||
/**
|
||||
* Creates a flow that emits business account gifts in a paginated manner.
|
||||
*
|
||||
* This function will automatically handle pagination by using the `nextOffset` from each response
|
||||
* to fetch the next page of gifts until there are no more gifts to fetch.
|
||||
*
|
||||
* @param businessConnectionId The ID of the business connection
|
||||
* @param excludeUnsaved Whether to exclude unsaved gifts
|
||||
* @param excludeSaved Whether to exclude saved gifts
|
||||
* @param excludeUnlimited Whether to exclude unlimited gifts
|
||||
* @param excludeLimited Whether to exclude limited gifts
|
||||
* @param excludeUnique Whether to exclude unique gifts
|
||||
* @param sortByPrice Whether to sort gifts by price
|
||||
* @param initialOffset The initial offset to start fetching from. If null, starts from the beginning
|
||||
* @param limit The maximum number of gifts to fetch per request
|
||||
* @param onErrorContinueChecker A function that determines whether to continue fetching on error.
|
||||
* Returns true to continue, false to stop. Default is to stop on any error.
|
||||
* @return A flow that emits [OwnedGifts] containing the fetched gifts
|
||||
*/
|
||||
public fun TelegramBot.getBusinessAccountGiftsFlow(
|
||||
businessConnectionId: BusinessConnectionId,
|
||||
excludeUnsaved: Boolean = false,
|
||||
excludeSaved: Boolean = false,
|
||||
excludeUnlimited: Boolean = false,
|
||||
excludeLimited: Boolean = false,
|
||||
excludeUnique: Boolean = false,
|
||||
sortByPrice: Boolean = false,
|
||||
initialOffset: String? = null,
|
||||
limit: Int? = null,
|
||||
onErrorContinueChecker: suspend (Throwable?) -> Boolean = { false }
|
||||
): Flow<OwnedGifts<GiftSentOrReceived.ReceivedInBusinessAccount>> = flow {
|
||||
var currentOffset = initialOffset
|
||||
do {
|
||||
val response = runCatching {
|
||||
getBusinessAccountGifts(
|
||||
businessConnectionId,
|
||||
excludeUnsaved,
|
||||
excludeSaved,
|
||||
excludeUnlimited,
|
||||
excludeLimited,
|
||||
excludeUnique,
|
||||
sortByPrice,
|
||||
currentOffset,
|
||||
limit
|
||||
)
|
||||
}
|
||||
if (response.isSuccess) {
|
||||
val result = response.getOrThrow()
|
||||
emit(result)
|
||||
currentOffset = result.nextOffset
|
||||
} else {
|
||||
if (onErrorContinueChecker(response.exceptionOrNull())) {
|
||||
continue
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
} while (currentOffset != null)
|
||||
}
|
Reference in New Issue
Block a user