From 90c3cd1b235a6b45b1389a21a2509256fe1c2119 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 14 Feb 2019 16:39:55 +0800 Subject: [PATCH 1/7] start 0.10.1 --- CHANGELOG.md | 2 ++ build.gradle | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e75892d38..eaa5d4c2aa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ * Fixes in edition of inline messages and their result types * Replace basic exception and add `ReplyMessageNotFound` exception +### 0.10.1 + ## 0.9.0 * Old extension `OkHttpClient.Builder#useWith` now deprecated and must be replaced by the same in diff --git a/build.gradle b/build.gradle index 810ac6ef59..6688104501 100644 --- a/build.gradle +++ b/build.gradle @@ -1,4 +1,4 @@ -project.version = "0.10.0" +project.version = "0.10.1" project.group = "com.github.insanusmokrassar" buildscript { From 4003fdbcd1dc5ba325723e3778e89e4961d68e07 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 14 Feb 2019 16:45:25 +0800 Subject: [PATCH 2/7] executeUnsafe to loop --- CHANGELOG.md | 2 ++ .../utils/extensions/RequestsExecutor.kt | 19 +++++++++++-------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eaa5d4c2aa..a7aaf9caed 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,8 @@ ### 0.10.1 +* Change algorithm of `executeUnsafe`: now it use loop instead of recursive calling + ## 0.9.0 * Old extension `OkHttpClient.Builder#useWith` now deprecated and must be replaced by the same in diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/RequestsExecutor.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/RequestsExecutor.kt index 1c621db8b0..6f0c2e0767 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/RequestsExecutor.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/RequestsExecutor.kt @@ -173,14 +173,17 @@ suspend fun RequestsExecutor.executeUnsafe( retries: Int = 0, retriesDelay: Long = 1000L ): T? { - return try { - execute(request) - } catch (e: RequestException) { - if (retries > 0) { - delay(retriesDelay) - executeUnsafe(request, retries - 1, retriesDelay) - } else { - null + var leftRetries = retries + while(true) { + try { + return execute(request) + } catch (e: RequestException) { + if (leftRetries > 0) { + leftRetries-- + delay(retriesDelay) + } else { + return null + } } } } From 03e7ca532ddf34aa09a0f88ffd59946ee0bb66a2 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 15 Feb 2019 09:54:18 +0800 Subject: [PATCH 3/7] startGettingUpdates with better media groups callbacks --- CHANGELOG.md | 1 + .../utils/extensions/RequestsExecutor.kt | 99 +++++++++++++++---- 2 files changed, 80 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a7aaf9caed..b58183d6e0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ ### 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 ## 0.9.0 diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/RequestsExecutor.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/RequestsExecutor.kt index 6f0c2e0767..d44cdb5871 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/RequestsExecutor.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/extensions/RequestsExecutor.kt @@ -99,10 +99,13 @@ fun RequestsExecutor.startGettingOfUpdates( fun RequestsExecutor.startGettingOfUpdates( messageCallback: UpdateReceiver? = null, - mediaGroupCallback: UpdateReceiver>? = null, + messageMediaGroupCallback: UpdateReceiver>? = null, editedMessageCallback: UpdateReceiver? = null, + editedMessageMediaGroupCallback: UpdateReceiver>? = null, channelPostCallback: UpdateReceiver? = null, + channelPostMediaGroupCallback: UpdateReceiver>? = null, editedChannelPostCallback: UpdateReceiver? = null, + editedChannelPostMediaGroupCallback: UpdateReceiver>? = null, chosenInlineResultCallback: UpdateReceiver? = null, inlineQueryCallback: UpdateReceiver? = null, callbackQueryCallback: UpdateReceiver? = null, @@ -115,36 +118,92 @@ fun RequestsExecutor.startGettingOfUpdates( requestsDelayMillis, scope, listOfNotNull( - messageCallback ?.let { UPDATE_MESSAGE }, - editedMessageCallback ?.let { UPDATE_EDITED_MESSAGE }, - channelPostCallback ?.let { UPDATE_CHANNEL_POST }, - editedChannelPostCallback ?.let { UPDATE_EDITED_CHANNEL_POST }, + (messageCallback ?: messageMediaGroupCallback) ?.let { UPDATE_MESSAGE }, + (editedMessageCallback ?: editedMessageMediaGroupCallback) ?.let { UPDATE_EDITED_MESSAGE }, + (channelPostCallback ?: channelPostMediaGroupCallback) ?.let { UPDATE_CHANNEL_POST }, + (editedChannelPostCallback ?: editedChannelPostMediaGroupCallback) ?.let { UPDATE_EDITED_CHANNEL_POST }, chosenInlineResultCallback ?.let { UPDATE_CHOSEN_INLINE_RESULT }, inlineQueryCallback ?.let { UPDATE_INLINE_QUERY }, callbackQueryCallback ?.let { UPDATE_CALLBACK_QUERY }, shippingQueryCallback ?.let { UPDATE_SHIPPING_QUERY }, preCheckoutQueryCallback ?.let { UPDATE_PRE_CHECKOUT_QUERY } ) - ) { - when (it) { - is MessageUpdate -> messageCallback ?.invoke(it) - is List<*> -> mediaGroupCallback ?.invoke( - it.mapNotNull { - it as? BaseMessageUpdate + ) { 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) + } ?: messageCallback ?.also { receiver -> + mappedList.forEach { receiver(it) } + } } - ) - 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) + is EditMessageUpdate -> update.mapNotNull { it as? EditMessageUpdate }.let { mappedList -> + editedMessageMediaGroupCallback ?.also { receiver -> + receiver(mappedList) + } ?: editedMessageCallback ?.also { receiver -> + mappedList.forEach { receiver(it) } + } + } + is ChannelPostUpdate -> update.mapNotNull { it as? ChannelPostUpdate }.let { mappedList -> + 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? = null, + mediaGroupCallback: UpdateReceiver>? = null, + editedMessageCallback: UpdateReceiver? = null, + channelPostCallback: UpdateReceiver? = null, + editedChannelPostCallback: UpdateReceiver? = null, + chosenInlineResultCallback: UpdateReceiver? = null, + inlineQueryCallback: UpdateReceiver? = null, + callbackQueryCallback: UpdateReceiver? = null, + shippingQueryCallback: UpdateReceiver? = null, + preCheckoutQueryCallback: UpdateReceiver? = 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 RequestsExecutor.executeAsync( request: Request, onFail: (suspend (Response<*>) -> Unit)? = null, From e774f958af71c63e3516051b7751d410312476fd Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sun, 17 Feb 2019 14:29:17 +0800 Subject: [PATCH 4/7] MediaGroupMessage is CommonMessage --- CHANGELOG.md | 1 + .../types/message/CommonMediaGroupMessage.kt | 5 ++++- .../TelegramBotAPI/types/message/MediaGroupMessage.kt | 5 ++++- .../TelegramBotAPI/types/message/RawMessage.kt | 10 ++++++++-- .../types/message/abstracts/MediaGroupMessage.kt | 3 +-- 5 files changed, 18 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b58183d6e0..f045e67d05 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ * 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 ## 0.9.0 diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/CommonMediaGroupMessage.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/CommonMediaGroupMessage.kt index d91fc439a3..878dc837c4 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/CommonMediaGroupMessage.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/CommonMediaGroupMessage.kt @@ -13,5 +13,8 @@ data class CommonMediaGroupMessage( override val chat: Chat, override val date: DateTime, 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 diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/MediaGroupMessage.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/MediaGroupMessage.kt index b20c3eaf3a..904cda00d6 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/MediaGroupMessage.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/MediaGroupMessage.kt @@ -13,5 +13,8 @@ data class MediaGroupMessage( override val chat: Chat, override val date: DateTime, 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 diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/RawMessage.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/RawMessage.kt index 1806b20c20..d42a935102 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/RawMessage.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/RawMessage.kt @@ -219,7 +219,10 @@ data class RawMessage( is PhotoContent -> content is VideoContent -> content else -> throw IllegalStateException("Unsupported content for media group") - } + }, + edit_date ?.asDate, + forwarded, + reply_to_message ?.asMessage ) else -> CommonMediaGroupMessage( messageId, @@ -231,7 +234,10 @@ data class RawMessage( is PhotoContent -> content is VideoContent -> content else -> throw IllegalStateException("Unsupported content for media group") - } + }, + edit_date ?.asDate, + forwarded, + reply_to_message ?.asMessage ) } } ?: when (chat) { diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/abstracts/MediaGroupMessage.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/abstracts/MediaGroupMessage.kt index 29b53ca253..56d76252b1 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/abstracts/MediaGroupMessage.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/abstracts/MediaGroupMessage.kt @@ -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.message.content.abstracts.MediaGroupContent -interface MediaGroupMessage : Message { +interface MediaGroupMessage : CommonMessage { val mediaGroupId: MediaGroupIdentifier - val content: MediaGroupContent } From 1d1ac9040c665b68f99ef14de0b9c3ea1e41a167 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sun, 17 Feb 2019 14:40:43 +0800 Subject: [PATCH 5/7] MediaGroupList extensions --- CHANGELOG.md | 2 ++ .../TelegramBotAPI/utils/MediaGroupList.kt | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/MediaGroupList.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index f045e67d05..6450cf3b2b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,8 @@ * 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.forwarded: ForwardedMessage? + get() = first().let { + (it as? AbleToBeForwardedMessage) ?.forwarded + } + +val List.replyTo: Message? + get() = first().let { + (it as? AbleToReplyMessage) ?.replyTo + } + +val List.chat: Chat? + get() = first().data.chat From d610a16cbd59a4de8e6bc156fcbcc8eb8cd908a2 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sun, 17 Feb 2019 16:39:04 +0800 Subject: [PATCH 6/7] Update CHANGELOG.md --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6450cf3b2b..05b780911a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,8 +16,8 @@ * 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` for comfortable +work with media groups lists ## 0.9.0 From 3cd1f3c3d4cc76a98d26b008b0b7c860e81e14ca Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sun, 17 Feb 2019 18:53:00 +0800 Subject: [PATCH 7/7] fix parseMode of InputTextMessageContent --- CHANGELOG.md | 1 + .../InputMessageContent/InputTextMessageContent.kt | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 05b780911a..2d7f8fb055 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ * Now `MediaGroupMessage` is `CommonMessage` with `MediaGroupContent` content * Added extensions `replyTo`, `forwarded` and `chat` for `List` for comfortable work with media groups lists +* Fix `parseMode` of `InputTextMessageContent` ## 0.9.0 diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InlineQueries/InputMessageContent/InputTextMessageContent.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InlineQueries/InputMessageContent/InputTextMessageContent.kt index a6b8012e64..b93ae41b47 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InlineQueries/InputMessageContent/InputTextMessageContent.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/InlineQueries/InputMessageContent/InputTextMessageContent.kt @@ -14,7 +14,8 @@ data class InputTextMessageContent( @SerialName(messageTextField) override val caption: String, @SerialName(parseModeField) - override val parseMode: ParseMode, + @Optional + override val parseMode: ParseMode? = null, @SerialName(disableWebPagePreviewField) @Optional override val disableWebPagePreview: Boolean? = null