1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-22 16:23:48 +00:00

Merge pull request #24 from InsanusMokrassar/0.12.2

0.12.2
This commit is contained in:
InsanusMokrassar 2019-03-15 03:12:44 -05:00 committed by GitHub
commit e43123a0c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 108 additions and 116 deletions

View File

@ -28,6 +28,16 @@
* Added additional media group types (like `MessageMediaGroupUpdate`) * Added additional media group types (like `MessageMediaGroupUpdate`)
* Fixed handling of media group updates in `UpdatesFilter` * Fixed handling of media group updates in `UpdatesFilter`
### 0.12.2
* New in `MediaGroupUpdate`:
* It is subtype of `Update` and can be use as regular update with list of messages
* Data now is list with `MediaGroupMessage`
* Added field `origins` which represent origin updates for `MediaGroupMessage`
* `updateId` now represent LAST id of origins updates
* `UpdatesFilter` and other objects now work with `UpdateReceiver<Update>` as common supertype
for receivers.
## 0.11.0 ## 0.11.0
* Kotlin `1.3.11` -> `1.3.21` * Kotlin `1.3.11` -> `1.3.21`

View File

@ -1,4 +1,4 @@
project.version = "0.12.1" project.version = "0.12.2"
project.group = "com.github.insanusmokrassar" project.group = "com.github.insanusmokrassar"
buildscript { buildscript {

View File

@ -11,7 +11,6 @@ import io.ktor.client.request.forms.MultiPartFormDataContent
import io.ktor.client.request.forms.formData import io.ktor.client.request.forms.formData
import io.ktor.client.request.url import io.ktor.client.request.url
import io.ktor.http.* import io.ktor.http.*
import kotlinx.io.streams.asInput
import kotlinx.serialization.ImplicitReflectionSerializer import kotlinx.serialization.ImplicitReflectionSerializer
class MultipartRequestCallFactory : KtorCallFactory { class MultipartRequestCallFactory : KtorCallFactory {

View File

@ -1,8 +1,8 @@
package com.github.insanusmokrassar.TelegramBotAPI.requests package com.github.insanusmokrassar.TelegramBotAPI.requests
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.SimpleRequest import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.SimpleRequest
import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier
import com.github.insanusmokrassar.TelegramBotAPI.types.ALL_UPDATES_LIST import com.github.insanusmokrassar.TelegramBotAPI.types.ALL_UPDATES_LIST
import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier
import com.github.insanusmokrassar.TelegramBotAPI.types.update.RawUpdate import com.github.insanusmokrassar.TelegramBotAPI.types.update.RawUpdate
import kotlinx.serialization.* import kotlinx.serialization.*
import kotlinx.serialization.internal.ArrayListSerializer import kotlinx.serialization.internal.ArrayListSerializer

View File

@ -3,8 +3,6 @@ package com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts
import com.github.insanusmokrassar.TelegramBotAPI.utils.StorageFile import com.github.insanusmokrassar.TelegramBotAPI.utils.StorageFile
import kotlinx.serialization.* import kotlinx.serialization.*
import java.io.File import java.io.File
import java.nio.file.Files
import java.util.*
sealed class InputFile { sealed class InputFile {
abstract val fileId: String abstract val fileId: String

View File

@ -1,8 +1,6 @@
package com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts package com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts
import kotlinx.serialization.ContextSerializer import kotlinx.serialization.*
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable
@Serializable(SimpleRequestSerializer::class) @Serializable(SimpleRequestSerializer::class)
interface SimpleRequest<T: Any> : Request<T> interface SimpleRequest<T: Any> : Request<T>

View File

@ -1,9 +1,7 @@
package com.github.insanusmokrassar.TelegramBotAPI.requests.send.media.base package com.github.insanusmokrassar.TelegramBotAPI.requests.send.media.base
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.SimpleRequest import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.SimpleRequest
import kotlinx.serialization.ContextSerializer import kotlinx.serialization.*
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable
@Serializable(DataRequestSerializer::class) @Serializable(DataRequestSerializer::class)
interface DataRequest<T: Any> : SimpleRequest<T> interface DataRequest<T: Any> : SimpleRequest<T>

View File

@ -2,7 +2,8 @@ package com.github.insanusmokrassar.TelegramBotAPI.requests.webhook
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.SimpleRequest import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.SimpleRequest
import com.github.insanusmokrassar.TelegramBotAPI.types.WebhookInfo import com.github.insanusmokrassar.TelegramBotAPI.types.WebhookInfo
import kotlinx.serialization.* import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable
@Serializable @Serializable
class GetWebhookInfo : SimpleRequest<WebhookInfo> { class GetWebhookInfo : SimpleRequest<WebhookInfo> {

View File

@ -1,7 +1,8 @@
package com.github.insanusmokrassar.TelegramBotAPI.requests.webhook package com.github.insanusmokrassar.TelegramBotAPI.requests.webhook
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.* import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.*
import com.github.insanusmokrassar.TelegramBotAPI.requests.send.media.base.* import com.github.insanusmokrassar.TelegramBotAPI.requests.send.media.base.DataRequest
import com.github.insanusmokrassar.TelegramBotAPI.requests.send.media.base.MultipartRequestImpl
import com.github.insanusmokrassar.TelegramBotAPI.types.* import com.github.insanusmokrassar.TelegramBotAPI.types.*
import kotlinx.serialization.* import kotlinx.serialization.*
import kotlinx.serialization.internal.BooleanSerializer import kotlinx.serialization.internal.BooleanSerializer

View File

@ -2,15 +2,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.ChannelPostUpdate import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate
import com.github.insanusmokrassar.TelegramBotAPI.types.update.MessageUpdate
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

@ -2,14 +2,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.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

@ -2,15 +2,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.EditMessageUpdate import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate
import com.github.insanusmokrassar.TelegramBotAPI.types.update.MessageUpdate
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

@ -2,14 +2,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.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

@ -1,6 +1,7 @@
package com.github.insanusmokrassar.TelegramBotAPI.utils package com.github.insanusmokrassar.TelegramBotAPI.utils
import kotlinx.serialization.* import kotlinx.serialization.ImplicitReflectionSerializer
import kotlinx.serialization.KSerializer
import kotlinx.serialization.json.* import kotlinx.serialization.json.*
@Deprecated("This method can throw exceptions") @Deprecated("This method can throw exceptions")

View File

@ -3,25 +3,24 @@ package com.github.insanusmokrassar.TelegramBotAPI.utils.extensions
import com.github.insanusmokrassar.TelegramBotAPI.types.* 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.Update
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 +33,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 +78,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,8 @@ 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.Update
import kotlinx.coroutines.* import kotlinx.coroutines.*
import java.util.concurrent.Executors import java.util.concurrent.Executors
@ -13,20 +13,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 +60,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

@ -5,14 +5,16 @@ import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.InputFile
import com.github.insanusmokrassar.TelegramBotAPI.requests.webhook.SetWebhook import com.github.insanusmokrassar.TelegramBotAPI.requests.webhook.SetWebhook
import com.github.insanusmokrassar.TelegramBotAPI.types.MediaGroupIdentifier import com.github.insanusmokrassar.TelegramBotAPI.types.MediaGroupIdentifier
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.RawUpdate
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
import io.ktor.response.respond import io.ktor.response.respond
import io.ktor.routing.* import io.ktor.routing.post
import io.ktor.routing.routing
import io.ktor.server.engine.* import io.ktor.server.engine.*
import io.ktor.server.netty.Netty import io.ktor.server.netty.Netty
import kotlinx.coroutines.* import kotlinx.coroutines.*
@ -22,7 +24,6 @@ import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
/** /**
* Reverse proxy webhook. * Reverse proxy webhook.
* *
@ -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)