mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2024-11-22 16:23:48 +00:00
startGettingUpdates with better media groups callbacks
This commit is contained in:
parent
4003fdbcd1
commit
03e7ca532d
@ -14,6 +14,7 @@
|
|||||||
### 0.10.1
|
### 0.10.1
|
||||||
|
|
||||||
* Change algorithm of `executeUnsafe`: now it use loop instead of recursive calling
|
* Change algorithm of `executeUnsafe`: now it use loop instead of recursive calling
|
||||||
|
* Add additional `startGettingUpdates` with better management of received updates for media groups
|
||||||
|
|
||||||
## 0.9.0
|
## 0.9.0
|
||||||
|
|
||||||
|
@ -99,10 +99,13 @@ fun RequestsExecutor.startGettingOfUpdates(
|
|||||||
|
|
||||||
fun RequestsExecutor.startGettingOfUpdates(
|
fun RequestsExecutor.startGettingOfUpdates(
|
||||||
messageCallback: UpdateReceiver<MessageUpdate>? = null,
|
messageCallback: UpdateReceiver<MessageUpdate>? = null,
|
||||||
mediaGroupCallback: UpdateReceiver<List<BaseMessageUpdate>>? = null,
|
messageMediaGroupCallback: UpdateReceiver<List<MessageUpdate>>? = null,
|
||||||
editedMessageCallback: UpdateReceiver<EditMessageUpdate>? = null,
|
editedMessageCallback: UpdateReceiver<EditMessageUpdate>? = null,
|
||||||
|
editedMessageMediaGroupCallback: UpdateReceiver<List<EditMessageUpdate>>? = null,
|
||||||
channelPostCallback: UpdateReceiver<ChannelPostUpdate>? = null,
|
channelPostCallback: UpdateReceiver<ChannelPostUpdate>? = null,
|
||||||
|
channelPostMediaGroupCallback: UpdateReceiver<List<ChannelPostUpdate>>? = null,
|
||||||
editedChannelPostCallback: UpdateReceiver<EditChannelPostUpdate>? = null,
|
editedChannelPostCallback: UpdateReceiver<EditChannelPostUpdate>? = null,
|
||||||
|
editedChannelPostMediaGroupCallback: UpdateReceiver<List<EditChannelPostUpdate>>? = null,
|
||||||
chosenInlineResultCallback: UpdateReceiver<ChosenInlineResultUpdate>? = null,
|
chosenInlineResultCallback: UpdateReceiver<ChosenInlineResultUpdate>? = null,
|
||||||
inlineQueryCallback: UpdateReceiver<InlineQueryUpdate>? = null,
|
inlineQueryCallback: UpdateReceiver<InlineQueryUpdate>? = null,
|
||||||
callbackQueryCallback: UpdateReceiver<CallbackQueryUpdate>? = null,
|
callbackQueryCallback: UpdateReceiver<CallbackQueryUpdate>? = null,
|
||||||
@ -115,36 +118,92 @@ fun RequestsExecutor.startGettingOfUpdates(
|
|||||||
requestsDelayMillis,
|
requestsDelayMillis,
|
||||||
scope,
|
scope,
|
||||||
listOfNotNull(
|
listOfNotNull(
|
||||||
messageCallback ?.let { UPDATE_MESSAGE },
|
(messageCallback ?: messageMediaGroupCallback) ?.let { UPDATE_MESSAGE },
|
||||||
editedMessageCallback ?.let { UPDATE_EDITED_MESSAGE },
|
(editedMessageCallback ?: editedMessageMediaGroupCallback) ?.let { UPDATE_EDITED_MESSAGE },
|
||||||
channelPostCallback ?.let { UPDATE_CHANNEL_POST },
|
(channelPostCallback ?: channelPostMediaGroupCallback) ?.let { UPDATE_CHANNEL_POST },
|
||||||
editedChannelPostCallback ?.let { UPDATE_EDITED_CHANNEL_POST },
|
(editedChannelPostCallback ?: editedChannelPostMediaGroupCallback) ?.let { UPDATE_EDITED_CHANNEL_POST },
|
||||||
chosenInlineResultCallback ?.let { UPDATE_CHOSEN_INLINE_RESULT },
|
chosenInlineResultCallback ?.let { UPDATE_CHOSEN_INLINE_RESULT },
|
||||||
inlineQueryCallback ?.let { UPDATE_INLINE_QUERY },
|
inlineQueryCallback ?.let { UPDATE_INLINE_QUERY },
|
||||||
callbackQueryCallback ?.let { UPDATE_CALLBACK_QUERY },
|
callbackQueryCallback ?.let { UPDATE_CALLBACK_QUERY },
|
||||||
shippingQueryCallback ?.let { UPDATE_SHIPPING_QUERY },
|
shippingQueryCallback ?.let { UPDATE_SHIPPING_QUERY },
|
||||||
preCheckoutQueryCallback ?.let { UPDATE_PRE_CHECKOUT_QUERY }
|
preCheckoutQueryCallback ?.let { UPDATE_PRE_CHECKOUT_QUERY }
|
||||||
)
|
)
|
||||||
) {
|
) { update ->
|
||||||
when (it) {
|
when (update) {
|
||||||
is MessageUpdate -> messageCallback ?.invoke(it)
|
is MessageUpdate -> messageCallback ?.invoke(update)
|
||||||
is List<*> -> mediaGroupCallback ?.invoke(
|
is List<*> -> when (update.firstOrNull()) {
|
||||||
it.mapNotNull {
|
is MessageUpdate -> update.mapNotNull { it as? MessageUpdate }.let { mappedList ->
|
||||||
it as? BaseMessageUpdate
|
messageMediaGroupCallback ?.also { receiver ->
|
||||||
|
receiver(mappedList)
|
||||||
|
} ?: messageCallback ?.also { receiver ->
|
||||||
|
mappedList.forEach { receiver(it) }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
)
|
is EditMessageUpdate -> update.mapNotNull { it as? EditMessageUpdate }.let { mappedList ->
|
||||||
is EditMessageUpdate -> editedMessageCallback ?.invoke(it)
|
editedMessageMediaGroupCallback ?.also { receiver ->
|
||||||
is ChannelPostUpdate -> channelPostCallback ?.invoke(it)
|
receiver(mappedList)
|
||||||
is EditChannelPostUpdate -> editedChannelPostCallback ?.invoke(it)
|
} ?: editedMessageCallback ?.also { receiver ->
|
||||||
is ChosenInlineResultUpdate -> chosenInlineResultCallback ?.invoke(it)
|
mappedList.forEach { receiver(it) }
|
||||||
is InlineQueryUpdate -> inlineQueryCallback ?.invoke(it)
|
}
|
||||||
is CallbackQueryUpdate -> callbackQueryCallback ?.invoke(it)
|
}
|
||||||
is ShippingQueryUpdate -> shippingQueryCallback ?.invoke(it)
|
is ChannelPostUpdate -> update.mapNotNull { it as? ChannelPostUpdate }.let { mappedList ->
|
||||||
is PreCheckoutQueryUpdate -> preCheckoutQueryCallback ?.invoke(it)
|
channelPostMediaGroupCallback ?.also { receiver ->
|
||||||
|
receiver(mappedList)
|
||||||
|
} ?: channelPostCallback ?.also { receiver ->
|
||||||
|
mappedList.forEach { receiver(it) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
is EditChannelPostUpdate -> update.mapNotNull { it as? EditChannelPostUpdate }.let { mappedList ->
|
||||||
|
editedChannelPostMediaGroupCallback ?.also { receiver ->
|
||||||
|
receiver(mappedList)
|
||||||
|
} ?: editedChannelPostCallback ?.also { receiver ->
|
||||||
|
mappedList.forEach { receiver(it) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
is EditMessageUpdate -> editedMessageCallback ?.invoke(update)
|
||||||
|
is ChannelPostUpdate -> channelPostCallback ?.invoke(update)
|
||||||
|
is EditChannelPostUpdate -> editedChannelPostCallback ?.invoke(update)
|
||||||
|
is ChosenInlineResultUpdate -> chosenInlineResultCallback ?.invoke(update)
|
||||||
|
is InlineQueryUpdate -> inlineQueryCallback ?.invoke(update)
|
||||||
|
is CallbackQueryUpdate -> callbackQueryCallback ?.invoke(update)
|
||||||
|
is ShippingQueryUpdate -> shippingQueryCallback ?.invoke(update)
|
||||||
|
is PreCheckoutQueryUpdate -> preCheckoutQueryCallback ?.invoke(update)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun RequestsExecutor.startGettingOfUpdates(
|
||||||
|
messageCallback: UpdateReceiver<MessageUpdate>? = null,
|
||||||
|
mediaGroupCallback: UpdateReceiver<List<BaseMessageUpdate>>? = null,
|
||||||
|
editedMessageCallback: UpdateReceiver<EditMessageUpdate>? = null,
|
||||||
|
channelPostCallback: UpdateReceiver<ChannelPostUpdate>? = null,
|
||||||
|
editedChannelPostCallback: UpdateReceiver<EditChannelPostUpdate>? = null,
|
||||||
|
chosenInlineResultCallback: UpdateReceiver<ChosenInlineResultUpdate>? = null,
|
||||||
|
inlineQueryCallback: UpdateReceiver<InlineQueryUpdate>? = null,
|
||||||
|
callbackQueryCallback: UpdateReceiver<CallbackQueryUpdate>? = null,
|
||||||
|
shippingQueryCallback: UpdateReceiver<ShippingQueryUpdate>? = null,
|
||||||
|
preCheckoutQueryCallback: UpdateReceiver<PreCheckoutQueryUpdate>? = null,
|
||||||
|
requestsDelayMillis: Long = 1000,
|
||||||
|
scope: CoroutineScope = GlobalScope
|
||||||
|
): 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,
|
||||||
|
requestsDelayMillis = requestsDelayMillis,
|
||||||
|
scope = scope
|
||||||
|
)
|
||||||
|
|
||||||
fun <T: Any> RequestsExecutor.executeAsync(
|
fun <T: Any> RequestsExecutor.executeAsync(
|
||||||
request: Request<T>,
|
request: Request<T>,
|
||||||
onFail: (suspend (Response<*>) -> Unit)? = null,
|
onFail: (suspend (Response<*>) -> Unit)? = null,
|
||||||
|
Loading…
Reference in New Issue
Block a user