1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-22 08:13:47 +00:00

rewrite structure of media groups updates

This commit is contained in:
InsanusMokrassar 2019-03-15 07:47:46 +08:00
parent 2c33c87663
commit 1f0af84adc
10 changed files with 85 additions and 90 deletions

View File

@ -4,13 +4,11 @@ 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.ChannelPostUpdate import com.github.insanusmokrassar.TelegramBotAPI.types.update.ChannelPostUpdate
import com.github.insanusmokrassar.TelegramBotAPI.types.update.MessageUpdate import com.github.insanusmokrassar.TelegramBotAPI.types.update.MessageUpdate
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate
data class ChannelPostMediaGroupUpdate( data class ChannelPostMediaGroupUpdate(
override val updateId: UpdateIdentifier, override val origins: List<BaseMessageUpdate>
override val data: MediaGroupMessage
) : MediaGroupUpdate { ) : MediaGroupUpdate {
constructor(sourceUpdate: ChannelPostUpdate) : this( override val updateId: UpdateIdentifier = origins.last().updateId
sourceUpdate.updateId, override val data: List<MediaGroupMessage> = origins.mapNotNull { it.data as? MediaGroupMessage }
sourceUpdate.data as MediaGroupMessage }
)
}

View File

@ -3,13 +3,11 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdate
import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier 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.abstracts.BaseMessageUpdate
data class EditChannelPostMediaGroupUpdate( data class EditChannelPostMediaGroupUpdate(
override val updateId: UpdateIdentifier, override val origins: List<BaseMessageUpdate>
override val data: MediaGroupMessage
) : MediaGroupUpdate { ) : MediaGroupUpdate {
constructor(sourceUpdate: EditChannelPostUpdate) : this( override val updateId: UpdateIdentifier = origins.last().updateId
sourceUpdate.updateId, override val data: List<MediaGroupMessage> = origins.mapNotNull { it.data as? MediaGroupMessage }
sourceUpdate.data as MediaGroupMessage
)
} }

View File

@ -4,13 +4,11 @@ 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.EditMessageUpdate import com.github.insanusmokrassar.TelegramBotAPI.types.update.EditMessageUpdate
import com.github.insanusmokrassar.TelegramBotAPI.types.update.MessageUpdate import com.github.insanusmokrassar.TelegramBotAPI.types.update.MessageUpdate
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate
data class EditMessageMediaGroupUpdate( data class EditMessageMediaGroupUpdate(
override val updateId: UpdateIdentifier, override val origins: List<BaseMessageUpdate>
override val data: MediaGroupMessage
) : MediaGroupUpdate { ) : MediaGroupUpdate {
constructor(sourceUpdate: EditMessageUpdate) : this( override val updateId: UpdateIdentifier = origins.last().updateId
sourceUpdate.updateId, override val data: List<MediaGroupMessage> = origins.mapNotNull { it.data as? MediaGroupMessage }
sourceUpdate.data as MediaGroupMessage }
)
}

View File

@ -2,7 +2,9 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdate
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.abstracts.BaseMessageUpdate import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update
interface MediaGroupUpdate: BaseMessageUpdate { interface MediaGroupUpdate: Update {
override val data: MediaGroupMessage override val data: List<MediaGroupMessage>
val origins: List<BaseMessageUpdate>
} }

View File

@ -3,13 +3,11 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdate
import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier 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.MessageUpdate import com.github.insanusmokrassar.TelegramBotAPI.types.update.MessageUpdate
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate
data class MessageMediaGroupUpdate( data class MessageMediaGroupUpdate(
override val updateId: UpdateIdentifier, override val origins: List<BaseMessageUpdate>
override val data: MediaGroupMessage
) : MediaGroupUpdate { ) : MediaGroupUpdate {
constructor(sourceUpdate: MessageUpdate) : this( override val updateId: UpdateIdentifier = origins.last().updateId
sourceUpdate.updateId, override val data: List<MediaGroupMessage> = origins.mapNotNull { it.data as? MediaGroupMessage }
sourceUpdate.data as MediaGroupMessage
)
} }

View File

@ -1,16 +1,15 @@
package com.github.insanusmokrassar.TelegramBotAPI.utils package com.github.insanusmokrassar.TelegramBotAPI.utils
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.BaseMessageUpdate import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate
fun BaseMessageUpdate.toMediaGroupUpdate(): MediaGroupUpdate? = (this as? MediaGroupUpdate) ?: ((data as? MediaGroupMessage) ?.let { fun List<BaseMessageUpdate>.toMediaGroupUpdate(): MediaGroupUpdate? = (this as? MediaGroupUpdate) ?: let {
when (this) { when (first()) {
is MessageUpdate -> MessageMediaGroupUpdate(this) is MessageUpdate -> MessageMediaGroupUpdate(this)
is EditMessageUpdate -> EditMessageMediaGroupUpdate(this) is EditMessageUpdate -> EditMessageMediaGroupUpdate(this)
is ChannelPostUpdate -> ChannelPostMediaGroupUpdate(this) is ChannelPostUpdate -> ChannelPostMediaGroupUpdate(this)
is EditChannelPostUpdate -> EditChannelPostMediaGroupUpdate(this) is EditChannelPostUpdate -> EditChannelPostMediaGroupUpdate(this)
else -> null else -> null
} }
}) }

View File

@ -4,24 +4,25 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.*
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.BaseMessageUpdate import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update
import com.github.insanusmokrassar.TelegramBotAPI.utils.toMediaGroupUpdate import com.github.insanusmokrassar.TelegramBotAPI.utils.toMediaGroupUpdate
data class UpdatesFilter( data class UpdatesFilter(
private val messageCallback: UpdateReceiver<MessageUpdate>? = null, private val messageCallback: UpdateReceiver<MessageUpdate>? = null,
private val messageMediaGroupCallback: UpdateReceiver<List<MediaGroupUpdate>>? = null, private val messageMediaGroupCallback: UpdateReceiver<MessageMediaGroupUpdate>? = null,
private val editedMessageCallback: UpdateReceiver<EditMessageUpdate>? = null, private val editedMessageCallback: UpdateReceiver<EditMessageUpdate>? = null,
private val editedMessageMediaGroupCallback: UpdateReceiver<List<MediaGroupUpdate>>? = null, private val editedMessageMediaGroupCallback: UpdateReceiver<EditMessageMediaGroupUpdate>? = null,
private val channelPostCallback: UpdateReceiver<ChannelPostUpdate>? = null, private val channelPostCallback: UpdateReceiver<ChannelPostUpdate>? = null,
private val channelPostMediaGroupCallback: UpdateReceiver<List<MediaGroupUpdate>>? = null, private val channelPostMediaGroupCallback: UpdateReceiver<ChannelPostMediaGroupUpdate>? = null,
private val editedChannelPostCallback: UpdateReceiver<EditChannelPostUpdate>? = null, private val editedChannelPostCallback: UpdateReceiver<EditChannelPostUpdate>? = null,
private val editedChannelPostMediaGroupCallback: UpdateReceiver<List<MediaGroupUpdate>>? = null, private val editedChannelPostMediaGroupCallback: UpdateReceiver<EditChannelPostMediaGroupUpdate>? = null,
private val chosenInlineResultCallback: UpdateReceiver<ChosenInlineResultUpdate>? = null, private val chosenInlineResultCallback: UpdateReceiver<ChosenInlineResultUpdate>? = null,
private val inlineQueryCallback: UpdateReceiver<InlineQueryUpdate>? = null, private val inlineQueryCallback: UpdateReceiver<InlineQueryUpdate>? = null,
private val callbackQueryCallback: UpdateReceiver<CallbackQueryUpdate>? = null, private val callbackQueryCallback: UpdateReceiver<CallbackQueryUpdate>? = null,
private val shippingQueryCallback: UpdateReceiver<ShippingQueryUpdate>? = null, private val shippingQueryCallback: UpdateReceiver<ShippingQueryUpdate>? = null,
private val preCheckoutQueryCallback: UpdateReceiver<PreCheckoutQueryUpdate>? = null private val preCheckoutQueryCallback: UpdateReceiver<PreCheckoutQueryUpdate>? = null
) { ) {
val asUpdateReceiver: UpdateReceiver<Any> = this::invoke val asUpdateReceiver: UpdateReceiver<Update> = this::invoke
val allowedUpdates = listOfNotNull( val allowedUpdates = listOfNotNull(
(messageCallback ?: messageMediaGroupCallback) ?.let { UPDATE_MESSAGE }, (messageCallback ?: messageMediaGroupCallback) ?.let { UPDATE_MESSAGE },
(editedMessageCallback ?: editedMessageMediaGroupCallback) ?.let { UPDATE_EDITED_MESSAGE }, (editedMessageCallback ?: editedMessageMediaGroupCallback) ?.let { UPDATE_EDITED_MESSAGE },
@ -34,42 +35,40 @@ data class UpdatesFilter(
preCheckoutQueryCallback ?.let { UPDATE_PRE_CHECKOUT_QUERY } preCheckoutQueryCallback ?.let { UPDATE_PRE_CHECKOUT_QUERY }
) )
suspend fun invoke(update: Any) { suspend fun invoke(update: Update) {
when (update) { when (update) {
is MessageUpdate -> messageCallback ?.invoke(update) is MessageUpdate -> messageCallback ?.invoke(update)
is List<*> -> when (update.firstOrNull()) { is MessageMediaGroupUpdate -> messageMediaGroupCallback ?.also { receiver ->
is MessageUpdate -> update.mapNotNull { it as? MessageUpdate }.let { mappedList -> receiver(update)
messageMediaGroupCallback ?.also { receiver -> } ?: messageCallback ?.also { receiver ->
receiver(mappedList.mapNotNull { it.toMediaGroupUpdate() }) update.origins.mapNotNull { it as? MessageUpdate }.forEach {
} ?: messageCallback ?.also { receiver -> receiver(it)
mappedList.forEach { receiver(it) }
}
}
is EditMessageUpdate -> update.mapNotNull { it as? EditMessageUpdate }.let { mappedList ->
editedMessageMediaGroupCallback ?.also { receiver ->
receiver(mappedList.mapNotNull { it.toMediaGroupUpdate() })
} ?: editedMessageCallback ?.also { receiver ->
mappedList.forEach { receiver(it) }
}
}
is ChannelPostUpdate -> update.mapNotNull { it as? ChannelPostUpdate }.let { mappedList ->
channelPostMediaGroupCallback ?.also { receiver ->
receiver(mappedList.mapNotNull { it.toMediaGroupUpdate() })
} ?: channelPostCallback ?.also { receiver ->
mappedList.forEach { receiver(it) }
}
}
is EditChannelPostUpdate -> update.mapNotNull { it as? EditChannelPostUpdate }.let { mappedList ->
editedChannelPostMediaGroupCallback ?.also { receiver ->
receiver(mappedList.mapNotNull { it.toMediaGroupUpdate() })
} ?: editedChannelPostCallback ?.also { receiver ->
mappedList.forEach { receiver(it) }
}
} }
} }
is EditMessageUpdate -> editedMessageCallback ?.invoke(update) is EditMessageUpdate -> editedMessageCallback ?.invoke(update)
is EditMessageMediaGroupUpdate -> editedMessageMediaGroupCallback ?.also { receiver ->
receiver(update)
} ?: editedMessageCallback ?.also { receiver ->
update.origins.mapNotNull { it as? EditMessageUpdate }.forEach {
receiver(it)
}
}
is ChannelPostUpdate -> channelPostCallback ?.invoke(update) is ChannelPostUpdate -> channelPostCallback ?.invoke(update)
is ChannelPostMediaGroupUpdate -> channelPostMediaGroupCallback ?.also { receiver ->
receiver(update)
} ?: channelPostCallback ?.also { receiver ->
update.origins.mapNotNull { it as? ChannelPostUpdate }.forEach {
receiver(it)
}
}
is EditChannelPostUpdate -> editedChannelPostCallback ?.invoke(update) is EditChannelPostUpdate -> editedChannelPostCallback ?.invoke(update)
is EditChannelPostMediaGroupUpdate -> editedChannelPostMediaGroupCallback ?.also { receiver ->
receiver(update)
} ?: editedChannelPostCallback ?.also { receiver ->
update.origins.mapNotNull { it as? EditChannelPostUpdate }.forEach {
receiver(it)
}
}
is ChosenInlineResultUpdate -> chosenInlineResultCallback ?.invoke(update) is ChosenInlineResultUpdate -> chosenInlineResultCallback ?.invoke(update)
is InlineQueryUpdate -> inlineQueryCallback ?.invoke(update) is InlineQueryUpdate -> inlineQueryCallback ?.invoke(update)
is CallbackQueryUpdate -> callbackQueryCallback ?.invoke(update) is CallbackQueryUpdate -> callbackQueryCallback ?.invoke(update)
@ -81,7 +80,7 @@ data class UpdatesFilter(
fun createSimpleUpdateFilter( fun createSimpleUpdateFilter(
messageCallback: UpdateReceiver<MessageUpdate>? = null, messageCallback: UpdateReceiver<MessageUpdate>? = null,
mediaGroupCallback: UpdateReceiver<List<BaseMessageUpdate>>? = null, mediaGroupCallback: UpdateReceiver<MediaGroupUpdate>? = null,
editedMessageCallback: UpdateReceiver<EditMessageUpdate>? = null, editedMessageCallback: UpdateReceiver<EditMessageUpdate>? = null,
channelPostCallback: UpdateReceiver<ChannelPostUpdate>? = null, channelPostCallback: UpdateReceiver<ChannelPostUpdate>? = null,
editedChannelPostCallback: UpdateReceiver<EditChannelPostUpdate>? = null, editedChannelPostCallback: UpdateReceiver<EditChannelPostUpdate>? = null,

View File

@ -7,6 +7,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.MediaG
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update
import com.github.insanusmokrassar.TelegramBotAPI.utils.mediaGroupId import com.github.insanusmokrassar.TelegramBotAPI.utils.mediaGroupId
import com.github.insanusmokrassar.TelegramBotAPI.utils.toMediaGroupUpdate
import kotlinx.coroutines.* import kotlinx.coroutines.*
import java.util.concurrent.Executors import java.util.concurrent.Executors
@ -15,30 +16,26 @@ class UpdatesPoller(
private val requestsDelayMillis: Long = 1000, private val requestsDelayMillis: Long = 1000,
private val scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()), private val scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()),
private val allowedUpdates: List<String>? = null, private val allowedUpdates: List<String>? = null,
private val block: UpdateReceiver<Any> private val block: UpdateReceiver<Update>
) { ) {
private var lastHandledUpdate: UpdateIdentifier = 0L private var lastHandledUpdate: UpdateIdentifier = 0L
private val mediaGroup: MutableList<BaseMessageUpdate> = mutableListOf() private val mediaGroup: MutableList<BaseMessageUpdate> = mutableListOf()
private var pollerJob: Job? = null private var pollerJob: Job? = null
private suspend fun sendToBlock(data: Any) { private suspend fun sendToBlock(data: Update) {
block(data) block(data)
lastHandledUpdate = when (data) { lastHandledUpdate = data.updateId
is Update -> data.updateId
is List<*> -> (data.last() as? Update) ?.updateId ?: throw IllegalStateException(
"Found non-updates oriented list"
)
else -> throw IllegalStateException(
"Unknown type of data"
)
}
} }
private suspend fun pushMediaGroupUpdate(update: BaseMessageUpdate? = null) { private suspend fun pushMediaGroupUpdate(update: BaseMessageUpdate? = null) {
val inputMediaGroupId = (update ?.data as? MediaGroupMessage) ?.mediaGroupId val inputMediaGroupId = (update ?.data as? MediaGroupMessage) ?.mediaGroupId
if (mediaGroup.isNotEmpty() && inputMediaGroupId ?.equals(mediaGroup.mediaGroupId) != true) { if (mediaGroup.isNotEmpty() && inputMediaGroupId ?.equals(mediaGroup.mediaGroupId) != true) {
sendToBlock(listOf(*mediaGroup.toTypedArray())) listOf(*mediaGroup.toTypedArray()).toMediaGroupUpdate() ?.let {
sendToBlock(it)
} ?: mediaGroup.forEach {
sendToBlock(it)
}
mediaGroup.clear() mediaGroup.clear()
} }
inputMediaGroupId ?.let { inputMediaGroupId ?.let {

View File

@ -2,8 +2,9 @@ package com.github.insanusmokrassar.TelegramBotAPI.utils.extensions
import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor
import com.github.insanusmokrassar.TelegramBotAPI.types.update.* import com.github.insanusmokrassar.TelegramBotAPI.types.update.*
import com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdates.MediaGroupUpdate import com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdates.*
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update
import kotlinx.coroutines.* import kotlinx.coroutines.*
import java.util.concurrent.Executors import java.util.concurrent.Executors
@ -13,20 +14,20 @@ fun RequestsExecutor.startGettingOfUpdates(
requestsDelayMillis: Long = 1000, requestsDelayMillis: Long = 1000,
scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()), scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()),
allowedUpdates: List<String>? = null, allowedUpdates: List<String>? = null,
block: UpdateReceiver<Any> block: UpdateReceiver<Update>
): Job { ): Job {
return UpdatesPoller(this, requestsDelayMillis, scope, allowedUpdates, block).start() return UpdatesPoller(this, requestsDelayMillis, scope, allowedUpdates, block).start()
} }
fun RequestsExecutor.startGettingOfUpdates( fun RequestsExecutor.startGettingOfUpdates(
messageCallback: UpdateReceiver<MessageUpdate>? = null, messageCallback: UpdateReceiver<MessageUpdate>? = null,
messageMediaGroupCallback: UpdateReceiver<List<MediaGroupUpdate>>? = null, messageMediaGroupCallback: UpdateReceiver<MessageMediaGroupUpdate>? = null,
editedMessageCallback: UpdateReceiver<EditMessageUpdate>? = null, editedMessageCallback: UpdateReceiver<EditMessageUpdate>? = null,
editedMessageMediaGroupCallback: UpdateReceiver<List<MediaGroupUpdate>>? = null, editedMessageMediaGroupCallback: UpdateReceiver<EditMessageMediaGroupUpdate>? = null,
channelPostCallback: UpdateReceiver<ChannelPostUpdate>? = null, channelPostCallback: UpdateReceiver<ChannelPostUpdate>? = null,
channelPostMediaGroupCallback: UpdateReceiver<List<MediaGroupUpdate>>? = null, channelPostMediaGroupCallback: UpdateReceiver<ChannelPostMediaGroupUpdate>? = null,
editedChannelPostCallback: UpdateReceiver<EditChannelPostUpdate>? = null, editedChannelPostCallback: UpdateReceiver<EditChannelPostUpdate>? = null,
editedChannelPostMediaGroupCallback: UpdateReceiver<List<MediaGroupUpdate>>? = null, editedChannelPostMediaGroupCallback: UpdateReceiver<EditChannelPostMediaGroupUpdate>? = 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,
@ -60,7 +61,7 @@ fun RequestsExecutor.startGettingOfUpdates(
fun RequestsExecutor.startGettingOfUpdates( fun RequestsExecutor.startGettingOfUpdates(
messageCallback: UpdateReceiver<MessageUpdate>? = null, messageCallback: UpdateReceiver<MessageUpdate>? = null,
mediaGroupCallback: UpdateReceiver<List<BaseMessageUpdate>>? = null, mediaGroupCallback: UpdateReceiver<MediaGroupUpdate>? = null,
editedMessageCallback: UpdateReceiver<EditMessageUpdate>? = null, editedMessageCallback: UpdateReceiver<EditMessageUpdate>? = null,
channelPostCallback: UpdateReceiver<ChannelPostUpdate>? = null, channelPostCallback: UpdateReceiver<ChannelPostUpdate>? = null,
editedChannelPostCallback: UpdateReceiver<EditChannelPostUpdate>? = null, editedChannelPostCallback: UpdateReceiver<EditChannelPostUpdate>? = null,

View File

@ -8,6 +8,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.MediaG
import com.github.insanusmokrassar.TelegramBotAPI.types.update.* import com.github.insanusmokrassar.TelegramBotAPI.types.update.*
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update
import com.github.insanusmokrassar.TelegramBotAPI.utils.toMediaGroupUpdate
import io.ktor.application.Application import io.ktor.application.Application
import io.ktor.application.call import io.ktor.application.call
import io.ktor.request.receiveText import io.ktor.request.receiveText
@ -41,7 +42,7 @@ suspend fun RequestsExecutor.setWebhook(
allowedUpdates: List<String>? = null, allowedUpdates: List<String>? = null,
maxAllowedConnections: Int? = null, maxAllowedConnections: Int? = null,
engineFactory: ApplicationEngineFactory<*, *> = Netty, engineFactory: ApplicationEngineFactory<*, *> = Netty,
block: UpdateReceiver<Any> block: UpdateReceiver<Update>
): Job { ): Job {
val executeDeferred = certificate ?.let { val executeDeferred = certificate ?.let {
executeAsync( executeAsync(
@ -60,7 +61,7 @@ suspend fun RequestsExecutor.setWebhook(
) )
) )
val updatesChannel = Channel<Update>(Channel.UNLIMITED) val updatesChannel = Channel<Update>(Channel.UNLIMITED)
val mediaGroupChannel = Channel<Pair<MediaGroupIdentifier, Update>>(Channel.UNLIMITED) val mediaGroupChannel = Channel<Pair<MediaGroupIdentifier, BaseMessageUpdate>>(Channel.UNLIMITED)
val mediaGroupAccumulatedChannel = mediaGroupChannel.accumulateByKey( val mediaGroupAccumulatedChannel = mediaGroupChannel.accumulateByKey(
1000L, 1000L,
scope = scope scope = scope
@ -113,14 +114,18 @@ suspend fun RequestsExecutor.setWebhook(
for (update in updatesChannel) { for (update in updatesChannel) {
val data = update.data val data = update.data
when (data) { when (data) {
is MediaGroupMessage -> mediaGroupChannel.send(data.mediaGroupId to update) is MediaGroupMessage -> mediaGroupChannel.send(data.mediaGroupId to update as BaseMessageUpdate)
else -> block(update) else -> block(update)
} }
} }
} }
launch { launch {
for (mediaGroupUpdate in mediaGroupAccumulatedChannel) { for (mediaGroupUpdate in mediaGroupAccumulatedChannel) {
block(mediaGroupUpdate.second.mapNotNull { (it as? BaseMessageUpdate) }) mediaGroupUpdate.second.toMediaGroupUpdate() ?.let {
block(it)
} ?: mediaGroupUpdate.second.forEach {
block(it)
}
} }
} }
engine.start(false) engine.start(false)