From 94253800029cc7a9d831fda9e990f5a37cabc3c6 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 9 Jan 2024 17:03:47 +0600 Subject: [PATCH] imrovements in copy/delete/forward messages --- .../tgbotapi/extensions/api/DeleteMessages.kt | 16 ++- .../extensions/api/ForwardMessages.kt | 40 ++++++- .../extensions/api/send/CopyMessages.kt | 111 +++++++++++++++++- 3 files changed, 158 insertions(+), 9 deletions(-) diff --git a/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/DeleteMessages.kt b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/DeleteMessages.kt index c44d86a107..5ce39e0d10 100644 --- a/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/DeleteMessages.kt +++ b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/DeleteMessages.kt @@ -4,6 +4,7 @@ import dev.inmo.tgbotapi.bot.TelegramBot import dev.inmo.tgbotapi.requests.DeleteMessages import dev.inmo.tgbotapi.types.* import dev.inmo.tgbotapi.types.message.abstracts.AccessibleMessage +import dev.inmo.tgbotapi.types.message.abstracts.Message suspend fun TelegramBot.deleteMessages( chatId: ChatIdentifier, @@ -35,14 +36,18 @@ suspend fun TelegramBot.deleteMessages( ) suspend fun TelegramBot.deleteMessages( - messages: List -) = messages.groupBy { it.chat }.map { (chat, messages) -> + messagesMetas: List +) = messagesMetas.groupBy { it.chatId }.map { (chatId, messages) -> deleteMessages( - chatId = chat.id, + chatId = chatId, messageIds = messages.map { it.messageId } ) }.all { it } +suspend fun TelegramBot.deleteMessages( + messages: List +) = deleteMessages(messages.map { it.metaInfo }) + suspend fun TelegramBot.delete( chatId: ChatIdentifier, messageIds: List @@ -60,6 +65,11 @@ suspend fun TelegramBot.delete( vararg messageIds: MessageId ) = deleteMessages(chatId = chatId, messageIds = (listOf(firstMessageId, secondMessageId) + messageIds.toList())) + +suspend fun TelegramBot.delete( + messagesMetas: List +) = deleteMessages(messagesMetas) + suspend fun TelegramBot.delete( messages: List ) = deleteMessages(messages) diff --git a/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/ForwardMessages.kt b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/ForwardMessages.kt index edbb1bd4c0..1bbbd5a966 100644 --- a/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/ForwardMessages.kt +++ b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/ForwardMessages.kt @@ -1,9 +1,11 @@ package dev.inmo.tgbotapi.extensions.api import dev.inmo.tgbotapi.bot.TelegramBot +import dev.inmo.tgbotapi.extensions.api.send.copyMessages import dev.inmo.tgbotapi.requests.ForwardMessages import dev.inmo.tgbotapi.types.* import dev.inmo.tgbotapi.types.message.abstracts.AccessibleMessage +import dev.inmo.tgbotapi.types.message.abstracts.Message suspend fun TelegramBot.forwardMessages( toChatId: ChatIdentifier, @@ -66,15 +68,15 @@ suspend fun TelegramBot.forwardMessages( suspend fun TelegramBot.forwardMessages( toChatId: ChatIdentifier, - messages: List, + messagesMetas: List, threadId: MessageThreadId? = toChatId.threadId, disableNotification: Boolean = false, protectContent: Boolean = false, removeCaption: Boolean = false -) = messages.groupBy { it.chat }.flatMap { (chat, messages) -> +) = messagesMetas.groupBy { it.chatId }.flatMap { (chatId, messages) -> forwardMessages( toChatId = toChatId, - fromChatId = chat.id, + fromChatId = chatId, messageIds = messages.map { it.messageId }, threadId = threadId, disableNotification = disableNotification, @@ -83,6 +85,22 @@ suspend fun TelegramBot.forwardMessages( ) } +suspend fun TelegramBot.forwardMessages( + toChatId: ChatIdentifier, + messages: List, + threadId: MessageThreadId? = toChatId.threadId, + disableNotification: Boolean = false, + protectContent: Boolean = false, + removeCaption: Boolean = false +) = forwardMessages( + toChatId = toChatId, + messagesMetas = messages.map { it.metaInfo }, + threadId = threadId, + disableNotification = disableNotification, + protectContent = protectContent, + removeCaption = removeCaption +) + suspend fun TelegramBot.forward( toChatId: ChatIdentifier, fromChatId: ChatIdentifier, @@ -139,6 +157,22 @@ suspend fun TelegramBot.forward( removeCaption = removeCaption ) +suspend fun TelegramBot.forward( + toChatId: ChatIdentifier, + messagesMetas: List, + threadId: MessageThreadId? = toChatId.threadId, + disableNotification: Boolean = false, + protectContent: Boolean = false, + removeCaption: Boolean = false +) = forwardMessages( + toChatId = toChatId, + messagesMetas = messagesMetas, + threadId = threadId, + disableNotification = disableNotification, + protectContent = protectContent, + removeCaption = removeCaption +) + suspend fun TelegramBot.forward( toChatId: ChatIdentifier, messages: List, diff --git a/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/send/CopyMessages.kt b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/send/CopyMessages.kt index 3fabdeb877..7dbdc51103 100644 --- a/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/send/CopyMessages.kt +++ b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/send/CopyMessages.kt @@ -4,6 +4,7 @@ import dev.inmo.tgbotapi.bot.TelegramBot import dev.inmo.tgbotapi.requests.send.CopyMessages import dev.inmo.tgbotapi.types.* import dev.inmo.tgbotapi.types.message.abstracts.AccessibleMessage +import dev.inmo.tgbotapi.types.message.abstracts.Message suspend fun TelegramBot.copyMessages( toChatId: ChatIdentifier, @@ -66,15 +67,15 @@ suspend fun TelegramBot.copyMessages( suspend fun TelegramBot.copyMessages( toChatId: ChatIdentifier, - messages: List, + messagesMetas: List, threadId: MessageThreadId? = toChatId.threadId, disableNotification: Boolean = false, protectContent: Boolean = false, removeCaption: Boolean = false -) = messages.groupBy { it.chat }.flatMap { (chat, messages) -> +) = messagesMetas.groupBy { it.chatId }.flatMap { (chatId, messages) -> copyMessages( toChatId = toChatId, - fromChatId = chat.id, + fromChatId = chatId, messageIds = messages.map { it.messageId }, threadId = threadId, disableNotification = disableNotification, @@ -82,3 +83,107 @@ suspend fun TelegramBot.copyMessages( removeCaption = removeCaption ) } + +suspend fun TelegramBot.copyMessages( + toChatId: ChatIdentifier, + messages: List, + threadId: MessageThreadId? = toChatId.threadId, + disableNotification: Boolean = false, + protectContent: Boolean = false, + removeCaption: Boolean = false +) = copyMessages( + toChatId = toChatId, + messagesMetas = messages.map { it.metaInfo }, + threadId = threadId, + disableNotification = disableNotification, + protectContent = protectContent, + removeCaption = removeCaption +) + +suspend fun TelegramBot.copy( + toChatId: ChatIdentifier, + fromChatId: ChatIdentifier, + messageIds: List, + threadId: MessageThreadId? = toChatId.threadId, + disableNotification: Boolean = false, + protectContent: Boolean = false, + removeCaption: Boolean = false +) = copyMessages( + toChatId = toChatId, + fromChatId = fromChatId, + messageIds = messageIds, + threadId = threadId, + disableNotification = disableNotification, + protectContent = protectContent, + removeCaption = removeCaption +) + +suspend fun TelegramBot.copy( + toChatId: ChatIdentifier, + fromChatId: ChatIdentifier, + messageIds: Array, + threadId: MessageThreadId? = toChatId.threadId, + disableNotification: Boolean = false, + protectContent: Boolean = false, + removeCaption: Boolean = false +) = copyMessages( + toChatId = toChatId, + fromChatId = fromChatId, + messageIds = messageIds, + threadId = threadId, + disableNotification = disableNotification, + protectContent = protectContent, + removeCaption = removeCaption +) + +suspend fun TelegramBot.copy( + toChatId: ChatIdentifier, + fromChatId: ChatIdentifier, + firstMessageId: MessageId, + vararg messageIds: MessageId, + threadId: MessageThreadId? = toChatId.threadId, + disableNotification: Boolean = false, + protectContent: Boolean = false, + removeCaption: Boolean = false +) = copyMessages( + toChatId = toChatId, + fromChatId = fromChatId, + firstMessageId = firstMessageId, + messageIds = messageIds, + threadId = threadId, + disableNotification = disableNotification, + protectContent = protectContent, + removeCaption = removeCaption +) + +suspend fun TelegramBot.copy( + toChatId: ChatIdentifier, + messagesMetas: List, + threadId: MessageThreadId? = toChatId.threadId, + disableNotification: Boolean = false, + protectContent: Boolean = false, + removeCaption: Boolean = false +) = copyMessages( + toChatId = toChatId, + messagesMetas = messagesMetas, + threadId = threadId, + disableNotification = disableNotification, + protectContent = protectContent, + removeCaption = removeCaption +) + +suspend fun TelegramBot.copy( + toChatId: ChatIdentifier, + messages: List, + threadId: MessageThreadId? = toChatId.threadId, + disableNotification: Boolean = false, + protectContent: Boolean = false, + removeCaption: Boolean = false +) = copyMessages( + toChatId = toChatId, + messages = messages, + threadId = threadId, + disableNotification = disableNotification, + protectContent = protectContent, + removeCaption = removeCaption +)