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

change signature of Update + mediaGroupCallback

This commit is contained in:
InsanusMokrassar 2019-01-21 14:13:31 +08:00
parent 09cb429a59
commit 116344c0a6
10 changed files with 92 additions and 33 deletions

View File

@ -32,3 +32,6 @@
* Now all media sending factories which contains `thumb` have default `null` value * Now all media sending factories which contains `thumb` have default `null` value
* `ChatIdentifier` classes now are `data` classes * `ChatIdentifier` classes now are `data` classes
* Now `MediaGroupContent` interface contains `toMediaGroupMemberInputMedia` method for easily creating mirror input media * 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

View File

@ -7,4 +7,4 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update
data class CallbackQueryUpdate( data class CallbackQueryUpdate(
override val updateId: UpdateIdentifier, override val updateId: UpdateIdentifier,
override val data: CallbackQuery override val data: CallbackQuery
) : Update<CallbackQuery> ) : Update

View File

@ -7,4 +7,4 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update
data class ChosenInlineResultUpdate( data class ChosenInlineResultUpdate(
override val updateId: UpdateIdentifier, override val updateId: UpdateIdentifier,
override val data: ChosenInlineResult override val data: ChosenInlineResult
) : Update<ChosenInlineResult> ) : Update

View File

@ -7,4 +7,4 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update
data class InlineQueryUpdate( data class InlineQueryUpdate(
override val updateId: UpdateIdentifier, override val updateId: UpdateIdentifier,
override val data: InlineQuery override val data: InlineQuery
) : Update<InlineQuery> ) : Update

View File

@ -7,4 +7,4 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update
data class PreCheckoutQueryUpdate( data class PreCheckoutQueryUpdate(
override val updateId: UpdateIdentifier, override val updateId: UpdateIdentifier,
override val data: PreCheckoutQuery override val data: PreCheckoutQuery
) : Update<PreCheckoutQuery> ) : Update

View File

@ -28,7 +28,7 @@ data class RawUpdate constructor(
@Optional private val pre_checkout_query: PreCheckoutQuery? = null @Optional private val pre_checkout_query: PreCheckoutQuery? = null
) { ) {
@Transient @Transient
val asUpdate: Update<*> by lazy { val asUpdate: Update by lazy {
when { when {
message != null -> MessageUpdate(updateId, message.asMessage) message != null -> MessageUpdate(updateId, message.asMessage)
edited_message != null -> EditMessageUpdate(updateId, edited_message.asMessage) edited_message != null -> EditMessageUpdate(updateId, edited_message.asMessage)

View File

@ -7,4 +7,4 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update
data class ShippingQueryUpdate( data class ShippingQueryUpdate(
override val updateId: UpdateIdentifier, override val updateId: UpdateIdentifier,
override val data: ShippingQuery override val data: ShippingQuery
) : Update<ShippingQuery> ) : Update

View File

@ -2,4 +2,6 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.Message import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.Message
interface BaseMessageUpdate : Update<Message> interface BaseMessageUpdate : Update {
override val data: Message
}

View File

@ -2,7 +2,7 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts
import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier import com.github.insanusmokrassar.TelegramBotAPI.types.UpdateIdentifier
interface Update<T: Any> { interface Update {
val updateId: UpdateIdentifier val updateId: UpdateIdentifier
val data: T val data: Any
} }

View File

@ -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.InlineQueries.abstracts.InlineQuery
import com.github.insanusmokrassar.TelegramBotAPI.types.ResponseParameters import com.github.insanusmokrassar.TelegramBotAPI.types.ResponseParameters
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.Message import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.Message
import com.github.insanusmokrassar.TelegramBotAPI.types.payments.PreCheckoutQuery import com.github.insanusmokrassar.TelegramBotAPI.types.payments.PreCheckoutQuery
import com.github.insanusmokrassar.TelegramBotAPI.types.payments.ShippingQuery import com.github.insanusmokrassar.TelegramBotAPI.types.payments.ShippingQuery
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.Update import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.Update
import kotlinx.coroutines.* import kotlinx.coroutines.*
typealias UpdateReceiver<T> = suspend Update<T>.() -> Unit typealias UpdateReceiver<T> = suspend (T) -> Unit
fun RequestsExecutor.startGettingOfUpdates( fun RequestsExecutor.startGettingOfUpdates(
requestsDelayMillis: Long = 1000, requestsDelayMillis: Long = 1000,
scope: CoroutineScope = GlobalScope, scope: CoroutineScope = GlobalScope,
allowedUpdates: List<String>? = null, allowedUpdates: List<String>? = null,
block: UpdateReceiver<*> block: UpdateReceiver<Any>
): Job { ): Job {
return scope.launch { return scope.launch {
var lastHandledUpdate: UpdateIdentifier = 0L 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 { try {
val update = rawUpdate.asUpdate
block(update) 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) { } catch (e: Exception) {
// TODO:: add exception handling // TODO:: add exception handling
e.printStackTrace() e.printStackTrace()
@ -56,15 +104,16 @@ fun RequestsExecutor.startGettingOfUpdates(
} }
fun RequestsExecutor.startGettingOfUpdates( fun RequestsExecutor.startGettingOfUpdates(
messageCallback: UpdateReceiver<Message>? = null, messageCallback: UpdateReceiver<MessageUpdate>? = null,
editedMessageCallback: UpdateReceiver<Message>? = null, mediaGroupCallback: UpdateReceiver<List<BaseMessageUpdate>>? = null,
channelPostCallback: UpdateReceiver<Message>? = null, editedMessageCallback: UpdateReceiver<EditMessageUpdate>? = null,
editedChannelPostCallback: UpdateReceiver<Message>? = null, channelPostCallback: UpdateReceiver<ChannelPostUpdate>? = null,
chosenInlineResultCallback: UpdateReceiver<ChosenInlineResult>? = null, editedChannelPostCallback: UpdateReceiver<EditChannelPostUpdate>? = null,
inlineQueryCallback: UpdateReceiver<InlineQuery>? = null, chosenInlineResultCallback: UpdateReceiver<ChosenInlineResultUpdate>? = null,
callbackQueryCallback: UpdateReceiver<CallbackQuery>? = null, inlineQueryCallback: UpdateReceiver<InlineQueryUpdate>? = null,
shippingQueryCallback: UpdateReceiver<ShippingQuery>? = null, callbackQueryCallback: UpdateReceiver<CallbackQueryUpdate>? = null,
preCheckoutQueryCallback: UpdateReceiver<PreCheckoutQuery>? = null, shippingQueryCallback: UpdateReceiver<ShippingQueryUpdate>? = null,
preCheckoutQueryCallback: UpdateReceiver<PreCheckoutQueryUpdate>? = null,
requestsDelayMillis: Long = 1000, requestsDelayMillis: Long = 1000,
scope: CoroutineScope = GlobalScope scope: CoroutineScope = GlobalScope
): Job { ): Job {
@ -83,16 +132,21 @@ fun RequestsExecutor.startGettingOfUpdates(
preCheckoutQueryCallback ?.let { UPDATE_PRE_CHECKOUT_QUERY } preCheckoutQueryCallback ?.let { UPDATE_PRE_CHECKOUT_QUERY }
) )
) { ) {
when (this) { when (it) {
is MessageUpdate -> messageCallback ?.invoke(this) is MessageUpdate -> messageCallback ?.invoke(it)
is EditMessageUpdate -> editedMessageCallback ?.invoke(this) is List<*> -> mediaGroupCallback ?.invoke(
is ChannelPostUpdate -> channelPostCallback ?.invoke(this) it.mapNotNull {
is EditChannelPostUpdate -> editedChannelPostCallback ?.invoke(this) it as? BaseMessageUpdate
is ChosenInlineResultUpdate -> chosenInlineResultCallback ?.invoke(this) }
is InlineQueryUpdate -> inlineQueryCallback ?.invoke(this) )
is CallbackQueryUpdate -> callbackQueryCallback ?.invoke(this) is EditMessageUpdate -> editedMessageCallback ?.invoke(it)
is ShippingQueryUpdate -> shippingQueryCallback ?.invoke(this) is ChannelPostUpdate -> channelPostCallback ?.invoke(it)
is PreCheckoutQueryUpdate -> preCheckoutQueryCallback ?.invoke(this) 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)
} }
} }
} }