mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2024-11-21 15:53:47 +00:00
change signature of Update + mediaGroupCallback
This commit is contained in:
parent
09cb429a59
commit
116344c0a6
@ -32,3 +32,6 @@
|
||||
* Now all media sending factories which contains `thumb` have default `null` value
|
||||
* `ChatIdentifier` classes now are `data` classes
|
||||
* Now `MediaGroupContent` interface contains `toMediaGroupMemberInputMedia` method for easily creating mirror input media
|
||||
* Change signature of `Update`
|
||||
* Now `Update` is untyped and data is `Any`
|
||||
* Media groups now are separated type of updates and you can subscribe on that receiving directly
|
||||
|
@ -7,4 +7,4 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update
|
||||
data class CallbackQueryUpdate(
|
||||
override val updateId: UpdateIdentifier,
|
||||
override val data: CallbackQuery
|
||||
) : Update<CallbackQuery>
|
||||
) : Update
|
||||
|
@ -7,4 +7,4 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update
|
||||
data class ChosenInlineResultUpdate(
|
||||
override val updateId: UpdateIdentifier,
|
||||
override val data: ChosenInlineResult
|
||||
) : Update<ChosenInlineResult>
|
||||
) : Update
|
@ -7,4 +7,4 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update
|
||||
data class InlineQueryUpdate(
|
||||
override val updateId: UpdateIdentifier,
|
||||
override val data: InlineQuery
|
||||
) : Update<InlineQuery>
|
||||
) : Update
|
||||
|
@ -7,4 +7,4 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update
|
||||
data class PreCheckoutQueryUpdate(
|
||||
override val updateId: UpdateIdentifier,
|
||||
override val data: PreCheckoutQuery
|
||||
) : Update<PreCheckoutQuery>
|
||||
) : Update
|
||||
|
@ -28,7 +28,7 @@ data class RawUpdate constructor(
|
||||
@Optional private val pre_checkout_query: PreCheckoutQuery? = null
|
||||
) {
|
||||
@Transient
|
||||
val asUpdate: Update<*> by lazy {
|
||||
val asUpdate: Update by lazy {
|
||||
when {
|
||||
message != null -> MessageUpdate(updateId, message.asMessage)
|
||||
edited_message != null -> EditMessageUpdate(updateId, edited_message.asMessage)
|
||||
|
@ -7,4 +7,4 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update
|
||||
data class ShippingQueryUpdate(
|
||||
override val updateId: UpdateIdentifier,
|
||||
override val data: ShippingQuery
|
||||
) : Update<ShippingQuery>
|
||||
) : Update
|
||||
|
@ -2,4 +2,6 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts
|
||||
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.Message
|
||||
|
||||
interface BaseMessageUpdate : Update<Message>
|
||||
interface BaseMessageUpdate : Update {
|
||||
override val data: Message
|
||||
}
|
@ -2,7 +2,7 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts
|
||||
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier
|
||||
|
||||
interface Update<T: Any> {
|
||||
interface Update {
|
||||
val updateId: UpdateIdentifier
|
||||
val data: T
|
||||
val data: Any
|
||||
}
|
||||
|
@ -9,20 +9,22 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.InlineQueries.abstracts.
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.InlineQueries.abstracts.InlineQuery
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.ResponseParameters
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.MediaGroupMessage
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.Message
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.payments.PreCheckoutQuery
|
||||
import com.github.insanusmokrassar.TelegramBotAPI.types.payments.ShippingQuery
|
||||
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 kotlinx.coroutines.*
|
||||
|
||||
typealias UpdateReceiver<T> = suspend Update<T>.() -> Unit
|
||||
typealias UpdateReceiver<T> = suspend (T) -> Unit
|
||||
|
||||
fun RequestsExecutor.startGettingOfUpdates(
|
||||
requestsDelayMillis: Long = 1000,
|
||||
scope: CoroutineScope = GlobalScope,
|
||||
allowedUpdates: List<String>? = null,
|
||||
block: UpdateReceiver<*>
|
||||
block: UpdateReceiver<Any>
|
||||
): Job {
|
||||
return scope.launch {
|
||||
var lastHandledUpdate: UpdateIdentifier = 0L
|
||||
@ -36,11 +38,57 @@ fun RequestsExecutor.startGettingOfUpdates(
|
||||
)
|
||||
)
|
||||
|
||||
for (rawUpdate in updates) {
|
||||
val adaptedUpdates = mutableListOf<Any>()
|
||||
var mediaGroup: MutableList<Update>? = null
|
||||
|
||||
fun pushMediaGroup() {
|
||||
mediaGroup ?.also {
|
||||
adaptedUpdates.add(it)
|
||||
mediaGroup = null
|
||||
}
|
||||
}
|
||||
|
||||
updates.map {
|
||||
it.asUpdate
|
||||
}.forEach { update ->
|
||||
val data = update.data
|
||||
if (data is MediaGroupMessage) {
|
||||
mediaGroup ?.let {
|
||||
val message = it.first().data as MediaGroupMessage
|
||||
if (message.mediaGroupId == data.mediaGroupId) {
|
||||
it.add(update)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
} ?: data.also {
|
||||
pushMediaGroup()
|
||||
mediaGroup = mutableListOf()
|
||||
mediaGroup ?.add(update)
|
||||
}
|
||||
} else {
|
||||
pushMediaGroup()
|
||||
adaptedUpdates.add(update)
|
||||
}
|
||||
}
|
||||
|
||||
mediaGroup ?.also {
|
||||
adaptedUpdates.add(it)
|
||||
mediaGroup = null
|
||||
}
|
||||
|
||||
for (update in adaptedUpdates) {
|
||||
|
||||
try {
|
||||
val update = rawUpdate.asUpdate
|
||||
block(update)
|
||||
lastHandledUpdate = update.updateId
|
||||
lastHandledUpdate = when (update) {
|
||||
is Update -> update.updateId
|
||||
is List<*> -> (update.last() as? Update) ?.updateId ?: throw IllegalStateException(
|
||||
"Found non-updates oriented list"
|
||||
)
|
||||
else -> throw IllegalStateException(
|
||||
"Unknown type of data"
|
||||
)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
// TODO:: add exception handling
|
||||
e.printStackTrace()
|
||||
@ -56,15 +104,16 @@ fun RequestsExecutor.startGettingOfUpdates(
|
||||
}
|
||||
|
||||
fun RequestsExecutor.startGettingOfUpdates(
|
||||
messageCallback: UpdateReceiver<Message>? = null,
|
||||
editedMessageCallback: UpdateReceiver<Message>? = null,
|
||||
channelPostCallback: UpdateReceiver<Message>? = null,
|
||||
editedChannelPostCallback: UpdateReceiver<Message>? = null,
|
||||
chosenInlineResultCallback: UpdateReceiver<ChosenInlineResult>? = null,
|
||||
inlineQueryCallback: UpdateReceiver<InlineQuery>? = null,
|
||||
callbackQueryCallback: UpdateReceiver<CallbackQuery>? = null,
|
||||
shippingQueryCallback: UpdateReceiver<ShippingQuery>? = null,
|
||||
preCheckoutQueryCallback: UpdateReceiver<PreCheckoutQuery>? = null,
|
||||
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 {
|
||||
@ -83,16 +132,21 @@ fun RequestsExecutor.startGettingOfUpdates(
|
||||
preCheckoutQueryCallback ?.let { UPDATE_PRE_CHECKOUT_QUERY }
|
||||
)
|
||||
) {
|
||||
when (this) {
|
||||
is MessageUpdate -> messageCallback ?.invoke(this)
|
||||
is EditMessageUpdate -> editedMessageCallback ?.invoke(this)
|
||||
is ChannelPostUpdate -> channelPostCallback ?.invoke(this)
|
||||
is EditChannelPostUpdate -> editedChannelPostCallback ?.invoke(this)
|
||||
is ChosenInlineResultUpdate -> chosenInlineResultCallback ?.invoke(this)
|
||||
is InlineQueryUpdate -> inlineQueryCallback ?.invoke(this)
|
||||
is CallbackQueryUpdate -> callbackQueryCallback ?.invoke(this)
|
||||
is ShippingQueryUpdate -> shippingQueryCallback ?.invoke(this)
|
||||
is PreCheckoutQueryUpdate -> preCheckoutQueryCallback ?.invoke(this)
|
||||
when (it) {
|
||||
is MessageUpdate -> messageCallback ?.invoke(it)
|
||||
is List<*> -> mediaGroupCallback ?.invoke(
|
||||
it.mapNotNull {
|
||||
it as? BaseMessageUpdate
|
||||
}
|
||||
)
|
||||
is EditMessageUpdate -> editedMessageCallback ?.invoke(it)
|
||||
is ChannelPostUpdate -> channelPostCallback ?.invoke(it)
|
||||
is EditChannelPostUpdate -> editedChannelPostCallback ?.invoke(it)
|
||||
is ChosenInlineResultUpdate -> chosenInlineResultCallback ?.invoke(it)
|
||||
is InlineQueryUpdate -> inlineQueryCallback ?.invoke(it)
|
||||
is CallbackQueryUpdate -> callbackQueryCallback ?.invoke(it)
|
||||
is ShippingQueryUpdate -> shippingQueryCallback ?.invoke(it)
|
||||
is PreCheckoutQueryUpdate -> preCheckoutQueryCallback ?.invoke(it)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user