1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-22 08:13:47 +00:00

Merge pull request #17 from InsanusMokrassar/0.10.1

0.10.1
This commit is contained in:
InsanusMokrassar 2019-02-17 19:43:35 +08:00 committed by GitHub
commit 12c8449327
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 138 additions and 36 deletions

View File

@ -11,6 +11,15 @@
* Fixes in edition of inline messages and their result types
* 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
* Old extension `OkHttpClient.Builder#useWith` now deprecated and must be replaced by the same in

View File

@ -1,4 +1,4 @@
project.version = "0.10.0"
project.version = "0.10.1"
project.group = "com.github.insanusmokrassar"
buildscript {

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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) {

View File

@ -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<MediaGroupContent> {
val mediaGroupId: MediaGroupIdentifier
val content: MediaGroupContent
}

View File

@ -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

View File

@ -99,10 +99,13 @@ fun RequestsExecutor.startGettingOfUpdates(
fun RequestsExecutor.startGettingOfUpdates(
messageCallback: UpdateReceiver<MessageUpdate>? = null,
mediaGroupCallback: UpdateReceiver<List<BaseMessageUpdate>>? = null,
messageMediaGroupCallback: UpdateReceiver<List<MessageUpdate>>? = null,
editedMessageCallback: UpdateReceiver<EditMessageUpdate>? = null,
editedMessageMediaGroupCallback: UpdateReceiver<List<EditMessageUpdate>>? = null,
channelPostCallback: UpdateReceiver<ChannelPostUpdate>? = null,
channelPostMediaGroupCallback: UpdateReceiver<List<ChannelPostUpdate>>? = null,
editedChannelPostCallback: UpdateReceiver<EditChannelPostUpdate>? = null,
editedChannelPostMediaGroupCallback: UpdateReceiver<List<EditChannelPostUpdate>>? = null,
chosenInlineResultCallback: UpdateReceiver<ChosenInlineResultUpdate>? = null,
inlineQueryCallback: UpdateReceiver<InlineQueryUpdate>? = null,
callbackQueryCallback: UpdateReceiver<CallbackQueryUpdate>? = null,
@ -115,35 +118,91 @@ 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 -> 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<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
)
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)
}
}
}
fun <T: Any> RequestsExecutor.executeAsync(
request: Request<T>,
@ -173,14 +232,17 @@ suspend fun <T: Any> RequestsExecutor.executeUnsafe(
retries: Int = 0,
retriesDelay: Long = 1000L
): T? {
return try {
execute(request)
var leftRetries = retries
while(true) {
try {
return execute(request)
} catch (e: RequestException) {
if (retries > 0) {
if (leftRetries > 0) {
leftRetries--
delay(retriesDelay)
executeUnsafe(request, retries - 1, retriesDelay)
} else {
null
return null
}
}
}
}