mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2024-12-23 00:57:13 +00:00
clear methodology of converting common updates to media group updates
This commit is contained in:
parent
94e88ddc37
commit
26c67b2f99
@ -38,28 +38,28 @@ data class UpdatesFilter(
|
||||
when (update) {
|
||||
is MessageUpdate -> messageCallback ?.invoke(update)
|
||||
is List<*> -> when (update.firstOrNull()) {
|
||||
is MessageUpdate, is MessageMediaGroupUpdate -> update.mapNotNull { it as? MessageUpdate }.let { mappedList ->
|
||||
is MessageUpdate -> update.mapNotNull { it as? MessageUpdate }.let { mappedList ->
|
||||
messageMediaGroupCallback ?.also { receiver ->
|
||||
receiver(mappedList.mapNotNull { it.toMediaGroupUpdate() })
|
||||
} ?: messageCallback ?.also { receiver ->
|
||||
mappedList.forEach { receiver(it) }
|
||||
}
|
||||
}
|
||||
is EditMessageUpdate, is EditMessageMediaGroupUpdate -> update.mapNotNull { it as? EditMessageUpdate }.let { mappedList ->
|
||||
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, is ChannelPostMediaGroupUpdate -> update.mapNotNull { it as? ChannelPostUpdate }.let { mappedList ->
|
||||
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, is EditChannelPostMediaGroupUpdate -> update.mapNotNull { it as? EditChannelPostUpdate }.let { mappedList ->
|
||||
is EditChannelPostUpdate -> update.mapNotNull { it as? EditChannelPostUpdate }.let { mappedList ->
|
||||
editedChannelPostMediaGroupCallback ?.also { receiver ->
|
||||
receiver(mappedList.mapNotNull { it.toMediaGroupUpdate() })
|
||||
} ?: editedChannelPostCallback ?.also { receiver ->
|
||||
|
@ -3,11 +3,10 @@ package com.github.insanusmokrassar.TelegramBotAPI.utils.extensions
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.requests.GetUpdates
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdates.MediaGroupUpdate
|
||||
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.Update
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.mediaGroupId
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.toMediaGroupUpdate
|
||||
import kotlinx.coroutines.*
|
||||
import java.util.concurrent.Executors
|
||||
|
||||
@ -19,7 +18,7 @@ class UpdatesPoller(
|
||||
private val block: UpdateReceiver<Any>
|
||||
) {
|
||||
private var lastHandledUpdate: UpdateIdentifier = 0L
|
||||
private val mediaGroup: MutableList<MediaGroupUpdate> = mutableListOf()
|
||||
private val mediaGroup: MutableList<BaseMessageUpdate> = mutableListOf()
|
||||
|
||||
private var pollerJob: Job? = null
|
||||
|
||||
@ -36,14 +35,14 @@ class UpdatesPoller(
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun pushMediaGroupUpdate(mediaGroupUpdate: MediaGroupUpdate? = null) {
|
||||
val inputMediaGroupId = mediaGroupUpdate ?.data ?.mediaGroupId
|
||||
private suspend fun pushMediaGroupUpdate(mediaGroupUpdate: BaseMessageUpdate? = null) {
|
||||
val inputMediaGroupId = (mediaGroupUpdate ?.data as? MediaGroupMessage) ?.mediaGroupId
|
||||
if (mediaGroup.isNotEmpty() && inputMediaGroupId ?.equals(mediaGroup.mediaGroupId) != true) {
|
||||
sendToBlock(listOf(*mediaGroup.toTypedArray()))
|
||||
mediaGroup.clear()
|
||||
}
|
||||
mediaGroupUpdate ?.let {
|
||||
mediaGroup.add(it)
|
||||
inputMediaGroupId ?.let {
|
||||
mediaGroup.add(mediaGroupUpdate)
|
||||
}
|
||||
}
|
||||
|
||||
@ -60,10 +59,9 @@ class UpdatesPoller(
|
||||
|
||||
private suspend fun handleUpdates(updates: List<Update>) {
|
||||
updates.forEach { update ->
|
||||
val mediaGroupUpdate = (update as? BaseMessageUpdate) ?.toMediaGroupUpdate()
|
||||
mediaGroupUpdate ?.let { _ ->
|
||||
pushMediaGroupUpdate(mediaGroupUpdate)
|
||||
} ?: let {
|
||||
(update as? BaseMessageUpdate) ?.let {
|
||||
pushMediaGroupUpdate(it)
|
||||
} ?:let {
|
||||
pushMediaGroupUpdate()
|
||||
sendToBlock(update)
|
||||
}
|
||||
|
@ -6,6 +6,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.requests.webhook.SetWebhook
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.MediaGroupIdentifier
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.MediaGroupMessage
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.*
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update
|
||||
import io.ktor.application.Application
|
||||
import io.ktor.application.call
|
||||
@ -17,8 +18,6 @@ import io.ktor.server.netty.Netty
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.channels.Channel
|
||||
import kotlinx.serialization.json.Json
|
||||
import java.io.FileInputStream
|
||||
import java.security.KeyStore
|
||||
import java.util.concurrent.Executors
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
@ -121,7 +120,7 @@ suspend fun RequestsExecutor.setWebhook(
|
||||
}
|
||||
launch {
|
||||
for (mediaGroupUpdate in mediaGroupAccumulatedChannel) {
|
||||
block(mediaGroupUpdate.second)
|
||||
block(mediaGroupUpdate.second.mapNotNull { (it as? BaseMessageUpdate) })
|
||||
}
|
||||
}
|
||||
engine.start(false)
|
||||
|
Loading…
Reference in New Issue
Block a user