1
0
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:
InsanusMokrassar 2019-02-15 09:54:18 +08:00
parent 4003fdbcd1
commit 03e7ca532d
2 changed files with 80 additions and 20 deletions

View File

@ -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

View File

@ -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 -> editedMessageCallback ?.invoke(it) is EditMessageUpdate -> update.mapNotNull { it as? EditMessageUpdate }.let { mappedList ->
is ChannelPostUpdate -> channelPostCallback ?.invoke(it) editedMessageMediaGroupCallback ?.also { receiver ->
is EditChannelPostUpdate -> editedChannelPostCallback ?.invoke(it) receiver(mappedList)
is ChosenInlineResultUpdate -> chosenInlineResultCallback ?.invoke(it) } ?: editedMessageCallback ?.also { receiver ->
is InlineQueryUpdate -> inlineQueryCallback ?.invoke(it) mappedList.forEach { receiver(it) }
is CallbackQueryUpdate -> callbackQueryCallback ?.invoke(it) }
is ShippingQueryUpdate -> shippingQueryCallback ?.invoke(it) }
is PreCheckoutQueryUpdate -> preCheckoutQueryCallback ?.invoke(it) is ChannelPostUpdate -> update.mapNotNull { it as? ChannelPostUpdate }.let { mappedList ->
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,