mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2024-11-22 08:13:47 +00:00
commit
12c8449327
@ -11,6 +11,15 @@
|
|||||||
* Fixes in edition of inline messages and their result types
|
* Fixes in edition of inline messages and their result types
|
||||||
* Replace basic exception and add `ReplyMessageNotFound` exception
|
* Replace basic exception and add `ReplyMessageNotFound` exception
|
||||||
|
|
||||||
|
### 0.10.1
|
||||||
|
|
||||||
|
* Change algorithm of `executeUnsafe`: now it use loop instead of recursive calling
|
||||||
|
* Add additional `startGettingUpdates` with better management of received updates for media groups
|
||||||
|
* Now `MediaGroupMessage` is `CommonMessage` with `MediaGroupContent` content
|
||||||
|
* Added extensions `replyTo`, `forwarded` and `chat` for `List<BaseMessageUpdated>` for comfortable
|
||||||
|
work with media groups lists
|
||||||
|
* Fix `parseMode` of `InputTextMessageContent`
|
||||||
|
|
||||||
## 0.9.0
|
## 0.9.0
|
||||||
|
|
||||||
* Old extension `OkHttpClient.Builder#useWith` now deprecated and must be replaced by the same in
|
* Old extension `OkHttpClient.Builder#useWith` now deprecated and must be replaced by the same in
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
project.version = "0.10.0"
|
project.version = "0.10.1"
|
||||||
project.group = "com.github.insanusmokrassar"
|
project.group = "com.github.insanusmokrassar"
|
||||||
|
|
||||||
buildscript {
|
buildscript {
|
||||||
|
@ -14,7 +14,8 @@ data class InputTextMessageContent(
|
|||||||
@SerialName(messageTextField)
|
@SerialName(messageTextField)
|
||||||
override val caption: String,
|
override val caption: String,
|
||||||
@SerialName(parseModeField)
|
@SerialName(parseModeField)
|
||||||
override val parseMode: ParseMode,
|
@Optional
|
||||||
|
override val parseMode: ParseMode? = null,
|
||||||
@SerialName(disableWebPagePreviewField)
|
@SerialName(disableWebPagePreviewField)
|
||||||
@Optional
|
@Optional
|
||||||
override val disableWebPagePreview: Boolean? = null
|
override val disableWebPagePreview: Boolean? = null
|
||||||
|
@ -13,5 +13,8 @@ data class CommonMediaGroupMessage(
|
|||||||
override val chat: Chat,
|
override val chat: Chat,
|
||||||
override val date: DateTime,
|
override val date: DateTime,
|
||||||
override val mediaGroupId: MediaGroupIdentifier,
|
override val mediaGroupId: MediaGroupIdentifier,
|
||||||
override val content: MediaGroupContent
|
override val content: MediaGroupContent,
|
||||||
|
override val editDate: DateTime?,
|
||||||
|
override val forwarded: ForwardedMessage?,
|
||||||
|
override val replyTo: Message?
|
||||||
) : Message, MediaGroupMessage, FromUserMessage
|
) : Message, MediaGroupMessage, FromUserMessage
|
||||||
|
@ -13,5 +13,8 @@ data class MediaGroupMessage(
|
|||||||
override val chat: Chat,
|
override val chat: Chat,
|
||||||
override val date: DateTime,
|
override val date: DateTime,
|
||||||
override val mediaGroupId: MediaGroupIdentifier,
|
override val mediaGroupId: MediaGroupIdentifier,
|
||||||
override val content: MediaGroupContent
|
override val content: MediaGroupContent,
|
||||||
|
override val editDate: DateTime?,
|
||||||
|
override val forwarded: ForwardedMessage?,
|
||||||
|
override val replyTo: Message?
|
||||||
) : Message, MediaGroupMessage
|
) : Message, MediaGroupMessage
|
||||||
|
@ -219,7 +219,10 @@ data class RawMessage(
|
|||||||
is PhotoContent -> content
|
is PhotoContent -> content
|
||||||
is VideoContent -> content
|
is VideoContent -> content
|
||||||
else -> throw IllegalStateException("Unsupported content for media group")
|
else -> throw IllegalStateException("Unsupported content for media group")
|
||||||
}
|
},
|
||||||
|
edit_date ?.asDate,
|
||||||
|
forwarded,
|
||||||
|
reply_to_message ?.asMessage
|
||||||
)
|
)
|
||||||
else -> CommonMediaGroupMessage(
|
else -> CommonMediaGroupMessage(
|
||||||
messageId,
|
messageId,
|
||||||
@ -231,7 +234,10 @@ data class RawMessage(
|
|||||||
is PhotoContent -> content
|
is PhotoContent -> content
|
||||||
is VideoContent -> content
|
is VideoContent -> content
|
||||||
else -> throw IllegalStateException("Unsupported content for media group")
|
else -> throw IllegalStateException("Unsupported content for media group")
|
||||||
}
|
},
|
||||||
|
edit_date ?.asDate,
|
||||||
|
forwarded,
|
||||||
|
reply_to_message ?.asMessage
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
} ?: when (chat) {
|
} ?: when (chat) {
|
||||||
|
@ -3,7 +3,6 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts
|
|||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.MediaGroupIdentifier
|
import com.github.insanusmokrassar.TelegramBotAPI.types.MediaGroupIdentifier
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.abstracts.MediaGroupContent
|
import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.abstracts.MediaGroupContent
|
||||||
|
|
||||||
interface MediaGroupMessage : Message {
|
interface MediaGroupMessage : CommonMessage<MediaGroupContent> {
|
||||||
val mediaGroupId: MediaGroupIdentifier
|
val mediaGroupId: MediaGroupIdentifier
|
||||||
val content: MediaGroupContent
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,19 @@
|
|||||||
|
package com.github.insanusmokrassar.TelegramBotAPI.utils
|
||||||
|
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.Chat
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.types.message.ForwardedMessage
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.*
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.types.update.abstracts.BaseMessageUpdate
|
||||||
|
|
||||||
|
val List<BaseMessageUpdate>.forwarded: ForwardedMessage?
|
||||||
|
get() = first().let {
|
||||||
|
(it as? AbleToBeForwardedMessage) ?.forwarded
|
||||||
|
}
|
||||||
|
|
||||||
|
val List<BaseMessageUpdate>.replyTo: Message?
|
||||||
|
get() = first().let {
|
||||||
|
(it as? AbleToReplyMessage) ?.replyTo
|
||||||
|
}
|
||||||
|
|
||||||
|
val List<BaseMessageUpdate>.chat: Chat?
|
||||||
|
get() = first().data.chat
|
@ -99,10 +99,13 @@ fun RequestsExecutor.startGettingOfUpdates(
|
|||||||
|
|
||||||
fun RequestsExecutor.startGettingOfUpdates(
|
fun RequestsExecutor.startGettingOfUpdates(
|
||||||
messageCallback: UpdateReceiver<MessageUpdate>? = null,
|
messageCallback: UpdateReceiver<MessageUpdate>? = null,
|
||||||
mediaGroupCallback: UpdateReceiver<List<BaseMessageUpdate>>? = null,
|
messageMediaGroupCallback: UpdateReceiver<List<MessageUpdate>>? = null,
|
||||||
editedMessageCallback: UpdateReceiver<EditMessageUpdate>? = null,
|
editedMessageCallback: UpdateReceiver<EditMessageUpdate>? = null,
|
||||||
|
editedMessageMediaGroupCallback: UpdateReceiver<List<EditMessageUpdate>>? = null,
|
||||||
channelPostCallback: UpdateReceiver<ChannelPostUpdate>? = null,
|
channelPostCallback: UpdateReceiver<ChannelPostUpdate>? = null,
|
||||||
|
channelPostMediaGroupCallback: UpdateReceiver<List<ChannelPostUpdate>>? = null,
|
||||||
editedChannelPostCallback: UpdateReceiver<EditChannelPostUpdate>? = null,
|
editedChannelPostCallback: UpdateReceiver<EditChannelPostUpdate>? = null,
|
||||||
|
editedChannelPostMediaGroupCallback: UpdateReceiver<List<EditChannelPostUpdate>>? = 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,
|
||||||
@ -115,36 +118,92 @@ fun RequestsExecutor.startGettingOfUpdates(
|
|||||||
requestsDelayMillis,
|
requestsDelayMillis,
|
||||||
scope,
|
scope,
|
||||||
listOfNotNull(
|
listOfNotNull(
|
||||||
messageCallback ?.let { UPDATE_MESSAGE },
|
(messageCallback ?: messageMediaGroupCallback) ?.let { UPDATE_MESSAGE },
|
||||||
editedMessageCallback ?.let { UPDATE_EDITED_MESSAGE },
|
(editedMessageCallback ?: editedMessageMediaGroupCallback) ?.let { UPDATE_EDITED_MESSAGE },
|
||||||
channelPostCallback ?.let { UPDATE_CHANNEL_POST },
|
(channelPostCallback ?: channelPostMediaGroupCallback) ?.let { UPDATE_CHANNEL_POST },
|
||||||
editedChannelPostCallback ?.let { UPDATE_EDITED_CHANNEL_POST },
|
(editedChannelPostCallback ?: editedChannelPostMediaGroupCallback) ?.let { UPDATE_EDITED_CHANNEL_POST },
|
||||||
chosenInlineResultCallback ?.let { UPDATE_CHOSEN_INLINE_RESULT },
|
chosenInlineResultCallback ?.let { UPDATE_CHOSEN_INLINE_RESULT },
|
||||||
inlineQueryCallback ?.let { UPDATE_INLINE_QUERY },
|
inlineQueryCallback ?.let { UPDATE_INLINE_QUERY },
|
||||||
callbackQueryCallback ?.let { UPDATE_CALLBACK_QUERY },
|
callbackQueryCallback ?.let { UPDATE_CALLBACK_QUERY },
|
||||||
shippingQueryCallback ?.let { UPDATE_SHIPPING_QUERY },
|
shippingQueryCallback ?.let { UPDATE_SHIPPING_QUERY },
|
||||||
preCheckoutQueryCallback ?.let { UPDATE_PRE_CHECKOUT_QUERY }
|
preCheckoutQueryCallback ?.let { UPDATE_PRE_CHECKOUT_QUERY }
|
||||||
)
|
)
|
||||||
) {
|
) { update ->
|
||||||
when (it) {
|
when (update) {
|
||||||
is MessageUpdate -> messageCallback ?.invoke(it)
|
is MessageUpdate -> messageCallback ?.invoke(update)
|
||||||
is List<*> -> mediaGroupCallback ?.invoke(
|
is List<*> -> when (update.firstOrNull()) {
|
||||||
it.mapNotNull {
|
is MessageUpdate -> update.mapNotNull { it as? MessageUpdate }.let { mappedList ->
|
||||||
it as? BaseMessageUpdate
|
messageMediaGroupCallback ?.also { receiver ->
|
||||||
|
receiver(mappedList)
|
||||||
|
} ?: messageCallback ?.also { receiver ->
|
||||||
|
mappedList.forEach { receiver(it) }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
)
|
is EditMessageUpdate -> update.mapNotNull { it as? EditMessageUpdate }.let { mappedList ->
|
||||||
is EditMessageUpdate -> editedMessageCallback ?.invoke(it)
|
editedMessageMediaGroupCallback ?.also { receiver ->
|
||||||
is ChannelPostUpdate -> channelPostCallback ?.invoke(it)
|
receiver(mappedList)
|
||||||
is EditChannelPostUpdate -> editedChannelPostCallback ?.invoke(it)
|
} ?: editedMessageCallback ?.also { receiver ->
|
||||||
is ChosenInlineResultUpdate -> chosenInlineResultCallback ?.invoke(it)
|
mappedList.forEach { receiver(it) }
|
||||||
is InlineQueryUpdate -> inlineQueryCallback ?.invoke(it)
|
}
|
||||||
is CallbackQueryUpdate -> callbackQueryCallback ?.invoke(it)
|
}
|
||||||
is ShippingQueryUpdate -> shippingQueryCallback ?.invoke(it)
|
is ChannelPostUpdate -> update.mapNotNull { it as? ChannelPostUpdate }.let { mappedList ->
|
||||||
is PreCheckoutQueryUpdate -> preCheckoutQueryCallback ?.invoke(it)
|
channelPostMediaGroupCallback ?.also { receiver ->
|
||||||
|
receiver(mappedList)
|
||||||
|
} ?: channelPostCallback ?.also { receiver ->
|
||||||
|
mappedList.forEach { receiver(it) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
is EditChannelPostUpdate -> update.mapNotNull { it as? EditChannelPostUpdate }.let { mappedList ->
|
||||||
|
editedChannelPostMediaGroupCallback ?.also { receiver ->
|
||||||
|
receiver(mappedList)
|
||||||
|
} ?: editedChannelPostCallback ?.also { receiver ->
|
||||||
|
mappedList.forEach { receiver(it) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
is EditMessageUpdate -> editedMessageCallback ?.invoke(update)
|
||||||
|
is ChannelPostUpdate -> channelPostCallback ?.invoke(update)
|
||||||
|
is EditChannelPostUpdate -> editedChannelPostCallback ?.invoke(update)
|
||||||
|
is ChosenInlineResultUpdate -> chosenInlineResultCallback ?.invoke(update)
|
||||||
|
is InlineQueryUpdate -> inlineQueryCallback ?.invoke(update)
|
||||||
|
is CallbackQueryUpdate -> callbackQueryCallback ?.invoke(update)
|
||||||
|
is ShippingQueryUpdate -> shippingQueryCallback ?.invoke(update)
|
||||||
|
is PreCheckoutQueryUpdate -> preCheckoutQueryCallback ?.invoke(update)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun RequestsExecutor.startGettingOfUpdates(
|
||||||
|
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 = startGettingOfUpdates(
|
||||||
|
messageCallback = messageCallback,
|
||||||
|
messageMediaGroupCallback = mediaGroupCallback,
|
||||||
|
editedMessageCallback = editedMessageCallback,
|
||||||
|
editedMessageMediaGroupCallback = mediaGroupCallback,
|
||||||
|
channelPostCallback = channelPostCallback,
|
||||||
|
channelPostMediaGroupCallback = mediaGroupCallback,
|
||||||
|
editedChannelPostCallback = editedChannelPostCallback,
|
||||||
|
editedChannelPostMediaGroupCallback = mediaGroupCallback,
|
||||||
|
chosenInlineResultCallback = chosenInlineResultCallback,
|
||||||
|
inlineQueryCallback = inlineQueryCallback,
|
||||||
|
callbackQueryCallback = callbackQueryCallback,
|
||||||
|
shippingQueryCallback = shippingQueryCallback,
|
||||||
|
preCheckoutQueryCallback = preCheckoutQueryCallback,
|
||||||
|
requestsDelayMillis = requestsDelayMillis,
|
||||||
|
scope = scope
|
||||||
|
)
|
||||||
|
|
||||||
fun <T: Any> RequestsExecutor.executeAsync(
|
fun <T: Any> RequestsExecutor.executeAsync(
|
||||||
request: Request<T>,
|
request: Request<T>,
|
||||||
onFail: (suspend (Response<*>) -> Unit)? = null,
|
onFail: (suspend (Response<*>) -> Unit)? = null,
|
||||||
@ -173,14 +232,17 @@ suspend fun <T: Any> RequestsExecutor.executeUnsafe(
|
|||||||
retries: Int = 0,
|
retries: Int = 0,
|
||||||
retriesDelay: Long = 1000L
|
retriesDelay: Long = 1000L
|
||||||
): T? {
|
): T? {
|
||||||
return try {
|
var leftRetries = retries
|
||||||
execute(request)
|
while(true) {
|
||||||
} catch (e: RequestException) {
|
try {
|
||||||
if (retries > 0) {
|
return execute(request)
|
||||||
delay(retriesDelay)
|
} catch (e: RequestException) {
|
||||||
executeUnsafe(request, retries - 1, retriesDelay)
|
if (leftRetries > 0) {
|
||||||
} else {
|
leftRetries--
|
||||||
null
|
delay(retriesDelay)
|
||||||
|
} else {
|
||||||
|
return null
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user