mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2024-11-22 08:13:47 +00:00
commit
e43123a0c0
10
CHANGELOG.md
10
CHANGELOG.md
@ -28,6 +28,16 @@
|
||||
* Added additional media group types (like `MessageMediaGroupUpdate`)
|
||||
* 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
|
||||
|
||||
* Kotlin `1.3.11` -> `1.3.21`
|
||||
|
@ -1,4 +1,4 @@
|
||||
project.version = "0.12.1"
|
||||
project.version = "0.12.2"
|
||||
project.group = "com.github.insanusmokrassar"
|
||||
|
||||
buildscript {
|
||||
|
@ -11,7 +11,6 @@ import io.ktor.client.request.forms.MultiPartFormDataContent
|
||||
import io.ktor.client.request.forms.formData
|
||||
import io.ktor.client.request.url
|
||||
import io.ktor.http.*
|
||||
import kotlinx.io.streams.asInput
|
||||
import kotlinx.serialization.ImplicitReflectionSerializer
|
||||
|
||||
class MultipartRequestCallFactory : KtorCallFactory {
|
||||
|
@ -1,8 +1,8 @@
|
||||
package com.github.insanusmokrassar.TelegramBotAPI.requests
|
||||
|
||||
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.UpdateIdentifier
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.RawUpdate
|
||||
import kotlinx.serialization.*
|
||||
import kotlinx.serialization.internal.ArrayListSerializer
|
||||
|
@ -3,8 +3,6 @@ package com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.StorageFile
|
||||
import kotlinx.serialization.*
|
||||
import java.io.File
|
||||
import java.nio.file.Files
|
||||
import java.util.*
|
||||
|
||||
sealed class InputFile {
|
||||
abstract val fileId: String
|
||||
|
@ -1,8 +1,6 @@
|
||||
package com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts
|
||||
|
||||
import kotlinx.serialization.ContextSerializer
|
||||
import kotlinx.serialization.KSerializer
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlinx.serialization.*
|
||||
|
||||
@Serializable(SimpleRequestSerializer::class)
|
||||
interface SimpleRequest<T: Any> : Request<T>
|
||||
|
@ -1,9 +1,7 @@
|
||||
package com.github.insanusmokrassar.TelegramBotAPI.requests.send.media.base
|
||||
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.SimpleRequest
|
||||
import kotlinx.serialization.ContextSerializer
|
||||
import kotlinx.serialization.KSerializer
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlinx.serialization.*
|
||||
|
||||
@Serializable(DataRequestSerializer::class)
|
||||
interface DataRequest<T: Any> : SimpleRequest<T>
|
||||
|
@ -2,7 +2,8 @@ package com.github.insanusmokrassar.TelegramBotAPI.requests.webhook
|
||||
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.SimpleRequest
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.WebhookInfo
|
||||
import kotlinx.serialization.*
|
||||
import kotlinx.serialization.KSerializer
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
class GetWebhookInfo : SimpleRequest<WebhookInfo> {
|
||||
|
@ -1,7 +1,8 @@
|
||||
package com.github.insanusmokrassar.TelegramBotAPI.requests.webhook
|
||||
|
||||
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 kotlinx.serialization.*
|
||||
import kotlinx.serialization.internal.BooleanSerializer
|
||||
|
@ -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.message.abstracts.MediaGroupMessage
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.ChannelPostUpdate
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.MessageUpdate
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate
|
||||
|
||||
data class ChannelPostMediaGroupUpdate(
|
||||
override val updateId: UpdateIdentifier,
|
||||
override val data: MediaGroupMessage
|
||||
override val origins: List<BaseMessageUpdate>
|
||||
) : MediaGroupUpdate {
|
||||
constructor(sourceUpdate: ChannelPostUpdate) : this(
|
||||
sourceUpdate.updateId,
|
||||
sourceUpdate.data as MediaGroupMessage
|
||||
)
|
||||
}
|
||||
override val updateId: UpdateIdentifier = origins.last().updateId
|
||||
override val data: List<MediaGroupMessage> = origins.mapNotNull { it.data as? MediaGroupMessage }
|
||||
}
|
||||
|
@ -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.message.abstracts.MediaGroupMessage
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.*
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate
|
||||
|
||||
data class EditChannelPostMediaGroupUpdate(
|
||||
override val updateId: UpdateIdentifier,
|
||||
override val data: MediaGroupMessage
|
||||
override val origins: List<BaseMessageUpdate>
|
||||
) : MediaGroupUpdate {
|
||||
constructor(sourceUpdate: EditChannelPostUpdate) : this(
|
||||
sourceUpdate.updateId,
|
||||
sourceUpdate.data as MediaGroupMessage
|
||||
)
|
||||
override val updateId: UpdateIdentifier = origins.last().updateId
|
||||
override val data: List<MediaGroupMessage> = origins.mapNotNull { it.data as? MediaGroupMessage }
|
||||
}
|
@ -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.message.abstracts.MediaGroupMessage
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.EditMessageUpdate
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.MessageUpdate
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate
|
||||
|
||||
data class EditMessageMediaGroupUpdate(
|
||||
override val updateId: UpdateIdentifier,
|
||||
override val data: MediaGroupMessage
|
||||
override val origins: List<BaseMessageUpdate>
|
||||
) : MediaGroupUpdate {
|
||||
constructor(sourceUpdate: EditMessageUpdate) : this(
|
||||
sourceUpdate.updateId,
|
||||
sourceUpdate.data as MediaGroupMessage
|
||||
)
|
||||
}
|
||||
override val updateId: UpdateIdentifier = origins.last().updateId
|
||||
override val data: List<MediaGroupMessage> = origins.mapNotNull { it.data as? MediaGroupMessage }
|
||||
}
|
||||
|
@ -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.update.abstracts.BaseMessageUpdate
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update
|
||||
|
||||
interface MediaGroupUpdate: BaseMessageUpdate {
|
||||
override val data: MediaGroupMessage
|
||||
interface MediaGroupUpdate: Update {
|
||||
override val data: List<MediaGroupMessage>
|
||||
val origins: List<BaseMessageUpdate>
|
||||
}
|
||||
|
@ -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.message.abstracts.MediaGroupMessage
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.MessageUpdate
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate
|
||||
|
||||
data class MessageMediaGroupUpdate(
|
||||
override val updateId: UpdateIdentifier,
|
||||
override val data: MediaGroupMessage
|
||||
override val origins: List<BaseMessageUpdate>
|
||||
) : MediaGroupUpdate {
|
||||
constructor(sourceUpdate: MessageUpdate) : this(
|
||||
sourceUpdate.updateId,
|
||||
sourceUpdate.data as MediaGroupMessage
|
||||
)
|
||||
override val updateId: UpdateIdentifier = origins.last().updateId
|
||||
override val data: List<MediaGroupMessage> = origins.mapNotNull { it.data as? MediaGroupMessage }
|
||||
}
|
@ -1,16 +1,15 @@
|
||||
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.MediaGroupUpdates.*
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate
|
||||
|
||||
fun BaseMessageUpdate.toMediaGroupUpdate(): MediaGroupUpdate? = (this as? MediaGroupUpdate) ?: ((data as? MediaGroupMessage) ?.let {
|
||||
when (this) {
|
||||
fun List<BaseMessageUpdate>.toMediaGroupUpdate(): MediaGroupUpdate? = (this as? MediaGroupUpdate) ?: let {
|
||||
when (first()) {
|
||||
is MessageUpdate -> MessageMediaGroupUpdate(this)
|
||||
is EditMessageUpdate -> EditMessageMediaGroupUpdate(this)
|
||||
is ChannelPostUpdate -> ChannelPostMediaGroupUpdate(this)
|
||||
is EditChannelPostUpdate -> EditChannelPostMediaGroupUpdate(this)
|
||||
else -> null
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package com.github.insanusmokrassar.TelegramBotAPI.utils
|
||||
|
||||
import kotlinx.serialization.*
|
||||
import kotlinx.serialization.ImplicitReflectionSerializer
|
||||
import kotlinx.serialization.KSerializer
|
||||
import kotlinx.serialization.json.*
|
||||
|
||||
@Deprecated("This method can throw exceptions")
|
||||
|
@ -3,25 +3,24 @@ package com.github.insanusmokrassar.TelegramBotAPI.utils.extensions
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.*
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.*
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdates.*
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.toMediaGroupUpdate
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update
|
||||
|
||||
data class UpdatesFilter(
|
||||
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 editedMessageMediaGroupCallback: UpdateReceiver<List<MediaGroupUpdate>>? = null,
|
||||
private val editedMessageMediaGroupCallback: UpdateReceiver<EditMessageMediaGroupUpdate>? = 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 editedChannelPostMediaGroupCallback: UpdateReceiver<List<MediaGroupUpdate>>? = null,
|
||||
private val editedChannelPostMediaGroupCallback: UpdateReceiver<EditChannelPostMediaGroupUpdate>? = null,
|
||||
private val chosenInlineResultCallback: UpdateReceiver<ChosenInlineResultUpdate>? = null,
|
||||
private val inlineQueryCallback: UpdateReceiver<InlineQueryUpdate>? = null,
|
||||
private val callbackQueryCallback: UpdateReceiver<CallbackQueryUpdate>? = null,
|
||||
private val shippingQueryCallback: UpdateReceiver<ShippingQueryUpdate>? = null,
|
||||
private val preCheckoutQueryCallback: UpdateReceiver<PreCheckoutQueryUpdate>? = null
|
||||
) {
|
||||
val asUpdateReceiver: UpdateReceiver<Any> = this::invoke
|
||||
val asUpdateReceiver: UpdateReceiver<Update> = this::invoke
|
||||
val allowedUpdates = listOfNotNull(
|
||||
(messageCallback ?: messageMediaGroupCallback) ?.let { UPDATE_MESSAGE },
|
||||
(editedMessageCallback ?: editedMessageMediaGroupCallback) ?.let { UPDATE_EDITED_MESSAGE },
|
||||
@ -34,42 +33,40 @@ data class UpdatesFilter(
|
||||
preCheckoutQueryCallback ?.let { UPDATE_PRE_CHECKOUT_QUERY }
|
||||
)
|
||||
|
||||
suspend fun invoke(update: Any) {
|
||||
suspend fun invoke(update: Update) {
|
||||
when (update) {
|
||||
is MessageUpdate -> messageCallback ?.invoke(update)
|
||||
is List<*> -> when (update.firstOrNull()) {
|
||||
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 -> 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 MessageMediaGroupUpdate -> messageMediaGroupCallback ?.also { receiver ->
|
||||
receiver(update)
|
||||
} ?: messageCallback ?.also { receiver ->
|
||||
update.origins.mapNotNull { it as? MessageUpdate }.forEach {
|
||||
receiver(it)
|
||||
}
|
||||
}
|
||||
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 ChannelPostMediaGroupUpdate -> channelPostMediaGroupCallback ?.also { receiver ->
|
||||
receiver(update)
|
||||
} ?: channelPostCallback ?.also { receiver ->
|
||||
update.origins.mapNotNull { it as? ChannelPostUpdate }.forEach {
|
||||
receiver(it)
|
||||
}
|
||||
}
|
||||
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 InlineQueryUpdate -> inlineQueryCallback ?.invoke(update)
|
||||
is CallbackQueryUpdate -> callbackQueryCallback ?.invoke(update)
|
||||
@ -81,7 +78,7 @@ data class UpdatesFilter(
|
||||
|
||||
fun createSimpleUpdateFilter(
|
||||
messageCallback: UpdateReceiver<MessageUpdate>? = null,
|
||||
mediaGroupCallback: UpdateReceiver<List<BaseMessageUpdate>>? = null,
|
||||
mediaGroupCallback: UpdateReceiver<MediaGroupUpdate>? = null,
|
||||
editedMessageCallback: UpdateReceiver<EditMessageUpdate>? = null,
|
||||
channelPostCallback: UpdateReceiver<ChannelPostUpdate>? = null,
|
||||
editedChannelPostCallback: UpdateReceiver<EditChannelPostUpdate>? = null,
|
||||
|
@ -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.Update
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.mediaGroupId
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.utils.toMediaGroupUpdate
|
||||
import kotlinx.coroutines.*
|
||||
import java.util.concurrent.Executors
|
||||
|
||||
@ -15,30 +16,26 @@ class UpdatesPoller(
|
||||
private val requestsDelayMillis: Long = 1000,
|
||||
private val scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()),
|
||||
private val allowedUpdates: List<String>? = null,
|
||||
private val block: UpdateReceiver<Any>
|
||||
private val block: UpdateReceiver<Update>
|
||||
) {
|
||||
private var lastHandledUpdate: UpdateIdentifier = 0L
|
||||
private val mediaGroup: MutableList<BaseMessageUpdate> = mutableListOf()
|
||||
|
||||
private var pollerJob: Job? = null
|
||||
|
||||
private suspend fun sendToBlock(data: Any) {
|
||||
private suspend fun sendToBlock(data: Update) {
|
||||
block(data)
|
||||
lastHandledUpdate = when (data) {
|
||||
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"
|
||||
)
|
||||
}
|
||||
lastHandledUpdate = data.updateId
|
||||
}
|
||||
|
||||
private suspend fun pushMediaGroupUpdate(update: BaseMessageUpdate? = null) {
|
||||
val inputMediaGroupId = (update ?.data as? MediaGroupMessage) ?.mediaGroupId
|
||||
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()
|
||||
}
|
||||
inputMediaGroupId ?.let {
|
||||
|
@ -2,8 +2,8 @@ package com.github.insanusmokrassar.TelegramBotAPI.utils.extensions
|
||||
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.*
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdates.MediaGroupUpdate
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.MediaGroupUpdates.*
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update
|
||||
import kotlinx.coroutines.*
|
||||
import java.util.concurrent.Executors
|
||||
|
||||
@ -13,20 +13,20 @@ fun RequestsExecutor.startGettingOfUpdates(
|
||||
requestsDelayMillis: Long = 1000,
|
||||
scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()),
|
||||
allowedUpdates: List<String>? = null,
|
||||
block: UpdateReceiver<Any>
|
||||
block: UpdateReceiver<Update>
|
||||
): Job {
|
||||
return UpdatesPoller(this, requestsDelayMillis, scope, allowedUpdates, block).start()
|
||||
}
|
||||
|
||||
fun RequestsExecutor.startGettingOfUpdates(
|
||||
messageCallback: UpdateReceiver<MessageUpdate>? = null,
|
||||
messageMediaGroupCallback: UpdateReceiver<List<MediaGroupUpdate>>? = null,
|
||||
messageMediaGroupCallback: UpdateReceiver<MessageMediaGroupUpdate>? = null,
|
||||
editedMessageCallback: UpdateReceiver<EditMessageUpdate>? = null,
|
||||
editedMessageMediaGroupCallback: UpdateReceiver<List<MediaGroupUpdate>>? = null,
|
||||
editedMessageMediaGroupCallback: UpdateReceiver<EditMessageMediaGroupUpdate>? = null,
|
||||
channelPostCallback: UpdateReceiver<ChannelPostUpdate>? = null,
|
||||
channelPostMediaGroupCallback: UpdateReceiver<List<MediaGroupUpdate>>? = null,
|
||||
channelPostMediaGroupCallback: UpdateReceiver<ChannelPostMediaGroupUpdate>? = null,
|
||||
editedChannelPostCallback: UpdateReceiver<EditChannelPostUpdate>? = null,
|
||||
editedChannelPostMediaGroupCallback: UpdateReceiver<List<MediaGroupUpdate>>? = null,
|
||||
editedChannelPostMediaGroupCallback: UpdateReceiver<EditChannelPostMediaGroupUpdate>? = null,
|
||||
chosenInlineResultCallback: UpdateReceiver<ChosenInlineResultUpdate>? = null,
|
||||
inlineQueryCallback: UpdateReceiver<InlineQueryUpdate>? = null,
|
||||
callbackQueryCallback: UpdateReceiver<CallbackQueryUpdate>? = null,
|
||||
@ -60,7 +60,7 @@ fun RequestsExecutor.startGettingOfUpdates(
|
||||
|
||||
fun RequestsExecutor.startGettingOfUpdates(
|
||||
messageCallback: UpdateReceiver<MessageUpdate>? = null,
|
||||
mediaGroupCallback: UpdateReceiver<List<BaseMessageUpdate>>? = null,
|
||||
mediaGroupCallback: UpdateReceiver<MediaGroupUpdate>? = null,
|
||||
editedMessageCallback: UpdateReceiver<EditMessageUpdate>? = null,
|
||||
channelPostCallback: UpdateReceiver<ChannelPostUpdate>? = null,
|
||||
editedChannelPostCallback: UpdateReceiver<EditChannelPostUpdate>? = null,
|
||||
|
@ -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.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.RawUpdate
|
||||
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 io.ktor.application.Application
|
||||
import io.ktor.application.call
|
||||
import io.ktor.request.receiveText
|
||||
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.netty.Netty
|
||||
import kotlinx.coroutines.*
|
||||
@ -22,7 +24,6 @@ import java.util.concurrent.Executors
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Reverse proxy webhook.
|
||||
*
|
||||
@ -41,7 +42,7 @@ suspend fun RequestsExecutor.setWebhook(
|
||||
allowedUpdates: List<String>? = null,
|
||||
maxAllowedConnections: Int? = null,
|
||||
engineFactory: ApplicationEngineFactory<*, *> = Netty,
|
||||
block: UpdateReceiver<Any>
|
||||
block: UpdateReceiver<Update>
|
||||
): Job {
|
||||
val executeDeferred = certificate ?.let {
|
||||
executeAsync(
|
||||
@ -60,7 +61,7 @@ suspend fun RequestsExecutor.setWebhook(
|
||||
)
|
||||
)
|
||||
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(
|
||||
1000L,
|
||||
scope = scope
|
||||
@ -113,14 +114,18 @@ suspend fun RequestsExecutor.setWebhook(
|
||||
for (update in updatesChannel) {
|
||||
val data = update.data
|
||||
when (data) {
|
||||
is MediaGroupMessage -> mediaGroupChannel.send(data.mediaGroupId to update)
|
||||
is MediaGroupMessage -> mediaGroupChannel.send(data.mediaGroupId to update as BaseMessageUpdate)
|
||||
else -> block(update)
|
||||
}
|
||||
}
|
||||
}
|
||||
launch {
|
||||
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)
|
||||
|
Loading…
Reference in New Issue
Block a user