mirror of
				https://github.com/InsanusMokrassar/TelegramBotApiLibraries.git
				synced 2025-10-29 11:10:20 +00:00 
			
		
		
		
	Compare commits
	
		
			26 Commits
		
	
	
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 1bf9a1570f | |||
| f24825bcbc | |||
| 5b560118c0 | |||
| c27b5647d7 | |||
| 6a5cd2f469 | |||
| 69c819162d | |||
| 7a603c21b8 | |||
| bfebd1de50 | |||
| cbbe283305 | |||
| a2dab361cf | |||
| ac89551ac4 | |||
| 0b0d1e4ea5 | |||
| 9b056656e3 | |||
| 9023cc5acc | |||
| 617b8091db | |||
| 88a89ff1e7 | |||
| c211002ac1 | |||
| 50f592f52c | |||
| efeb15b971 | |||
| d33ca67c7c | |||
| f59ff6dd3c | |||
| 8f643a7d5b | |||
| 980badd275 | |||
| a2e65f7b24 | |||
| f86cef7118 | |||
| 2278572cb2 | 
							
								
								
									
										3
									
								
								.github/workflows/commit-publish.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										3
									
								
								.github/workflows/commit-publish.yml
									
									
									
									
										vendored
									
									
								
							| @@ -8,9 +8,6 @@ jobs: | ||||
|       - uses: actions/setup-java@v1 | ||||
|         with: | ||||
|           java-version: 11 | ||||
|       - name: Fix android 32.0.0 dx | ||||
|         continue-on-error: true | ||||
|         run: cd /usr/local/lib/android/sdk/build-tools/32.0.0/ && mv d8 dx && cd lib  && mv d8.jar dx.jar | ||||
|       - name: Update version | ||||
|         run: | | ||||
|           branch="`echo "${{ github.ref }}" | grep -o "[^/]*$"`" | ||||
|   | ||||
| @@ -4,19 +4,17 @@ org.gradle.parallel=true | ||||
| kotlin.js.generate.externals=true | ||||
| kotlin.incremental=true | ||||
| kotlin.incremental.js=true | ||||
| android.useAndroidX=true | ||||
| android.enableJetifier=true | ||||
|  | ||||
|  | ||||
| kotlin_version=1.7.22 | ||||
| kotlin_serialisation_core_version=1.4.1 | ||||
| kotlin_version=1.8.10 | ||||
| kotlin_serialisation_core_version=1.5.0 | ||||
|  | ||||
| github_release_plugin_version=2.4.1 | ||||
|  | ||||
| tgbotapi_version=4.2.1 | ||||
| micro_utils_version=0.16.0 | ||||
| tgbotapi_version=6.0.1 | ||||
| micro_utils_version=0.17.1 | ||||
| exposed_version=0.41.1 | ||||
| plagubot_version=3.2.1 | ||||
| plagubot_version=4.0.1 | ||||
|  | ||||
| # Dokka | ||||
|  | ||||
| @@ -25,4 +23,4 @@ dokka_version=1.7.20 | ||||
| # Project data | ||||
|  | ||||
| group=dev.inmo | ||||
| version=0.6.6 | ||||
| version=0.9.1 | ||||
|   | ||||
							
								
								
									
										2
									
								
								gradle/wrapper/gradle-wrapper.properties
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								gradle/wrapper/gradle-wrapper.properties
									
									
									
									
										vendored
									
									
								
							| @@ -1,5 +1,5 @@ | ||||
| distributionBase=GRADLE_USER_HOME | ||||
| distributionPath=wrapper/dists | ||||
| distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-bin.zip | ||||
| distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip | ||||
| zipStoreBase=GRADLE_USER_HOME | ||||
| zipStorePath=wrapper/dists | ||||
|   | ||||
							
								
								
									
										17
									
								
								resender/build.gradle
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								resender/build.gradle
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,17 @@ | ||||
| plugins { | ||||
|     id "org.jetbrains.kotlin.multiplatform" | ||||
|     id "org.jetbrains.kotlin.plugin.serialization" | ||||
| } | ||||
|  | ||||
| apply from: "$mppJavaWithJsProjectPath" | ||||
|  | ||||
| kotlin { | ||||
|     sourceSets { | ||||
|         commonMain { | ||||
|             dependencies { | ||||
|                 api "dev.inmo:tgbotapi.core:$tgbotapi_version" | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
							
								
								
									
										20
									
								
								resender/src/commonMain/kotlin/MessageMetaInfo.kt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								resender/src/commonMain/kotlin/MessageMetaInfo.kt
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,20 @@ | ||||
| package dev.inmo.tgbotapi.libraries.resender | ||||
|  | ||||
| import dev.inmo.tgbotapi.types.FullChatIdentifierSerializer | ||||
| import dev.inmo.tgbotapi.types.IdChatIdentifier | ||||
| import dev.inmo.tgbotapi.types.MessageId | ||||
| import dev.inmo.tgbotapi.types.message.abstracts.Message | ||||
| import dev.inmo.tgbotapi.types.message.abstracts.PossiblyMediaGroupMessage | ||||
| import kotlinx.serialization.Serializable | ||||
|  | ||||
| @Serializable | ||||
| data class MessageMetaInfo( | ||||
|     @Serializable(FullChatIdentifierSerializer::class) | ||||
|     val chatId: IdChatIdentifier, | ||||
|     val messageId: MessageId, | ||||
|     val group: String? = null | ||||
| ) | ||||
|  | ||||
| operator fun MessageMetaInfo.Companion.invoke( | ||||
|     message: Message | ||||
| ) = MessageMetaInfo(message.chat.id, message.messageId, (message as? PossiblyMediaGroupMessage<*>) ?.mediaGroupId) | ||||
							
								
								
									
										144
									
								
								resender/src/commonMain/kotlin/MessagesResender.kt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										144
									
								
								resender/src/commonMain/kotlin/MessagesResender.kt
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,144 @@ | ||||
| package dev.inmo.tgbotapi.libraries.resender | ||||
|  | ||||
| import dev.inmo.tgbotapi.bot.TelegramBot | ||||
| import dev.inmo.tgbotapi.requests.ForwardMessage | ||||
| import dev.inmo.tgbotapi.requests.send.CopyMessage | ||||
| import dev.inmo.tgbotapi.requests.send.media.SendMediaGroup | ||||
| import dev.inmo.tgbotapi.types.ChatIdentifier | ||||
| import dev.inmo.tgbotapi.types.IdChatIdentifier | ||||
| import dev.inmo.tgbotapi.types.mediaCountInMediaGroup | ||||
| import dev.inmo.tgbotapi.types.message.abstracts.ContentMessage | ||||
| import dev.inmo.tgbotapi.types.message.content.MediaGroupPartContent | ||||
|  | ||||
| class MessagesResender( | ||||
|     private val bot: TelegramBot, | ||||
|     private val cacheChatId: ChatIdentifier | ||||
| ) { | ||||
|     suspend fun resend( | ||||
|         targetChatId: IdChatIdentifier, | ||||
|         messagesInfo: List<MessageMetaInfo> | ||||
|     ): List<Pair<MessageMetaInfo, MessageMetaInfo>> { | ||||
|         val messagesWithOrders = messagesInfo.mapIndexed { i, messageInfo -> messageInfo to i }.toMap() | ||||
|         val ordersWithMessagesGroups = messagesInfo.groupBy { it.group }.flatMap { (group, list) -> | ||||
|             if (group == null) { | ||||
|                 list.map { | ||||
|                     messagesWithOrders.getValue(it) to listOf(it) | ||||
|                 } | ||||
|             } else { | ||||
|                 listOf(messagesWithOrders.getValue(list.first()) to list) | ||||
|             } | ||||
|         }.sortedBy { it.first } | ||||
|  | ||||
|         return ordersWithMessagesGroups.flatMap { (_, contents) -> | ||||
|             val sourceMessagesToSentMessages = mutableListOf<Pair<MessageMetaInfo, MessageMetaInfo>>() | ||||
|  | ||||
|             when { | ||||
|                 contents.size == 1 -> { | ||||
|                     val messageInfo = contents.first() | ||||
|                     runCatching { | ||||
|                         MessageMetaInfo( | ||||
|                             targetChatId, | ||||
|                             bot.execute( | ||||
|                                 CopyMessage( | ||||
|                                     targetChatId, | ||||
|                                     fromChatId = messageInfo.chatId, | ||||
|                                     messageId = messageInfo.messageId | ||||
|                                 ) | ||||
|                             ) | ||||
|                         ) | ||||
|                     }.onFailure { _ -> | ||||
|                         runCatching { | ||||
|                             bot.execute( | ||||
|                                 ForwardMessage( | ||||
|                                     toChatId = targetChatId, | ||||
|                                     fromChatId = messageInfo.chatId, | ||||
|                                     messageId = messageInfo.messageId | ||||
|                                 ) | ||||
|                             ) | ||||
|                         }.onSuccess { | ||||
|                             MessageMetaInfo( | ||||
|                                 targetChatId, | ||||
|                                 bot.execute( | ||||
|                                     CopyMessage( | ||||
|                                         targetChatId, | ||||
|                                         fromChatId = it.chat.id, | ||||
|                                         messageId = it.messageId | ||||
|                                     ) | ||||
|                                 ) | ||||
|                             ) | ||||
|                         } | ||||
|                     }.getOrNull() ?.let { | ||||
|                         sourceMessagesToSentMessages.add(messageInfo to it) | ||||
|                     } | ||||
|                 } | ||||
|                 else -> { | ||||
|                     val resultContents = contents.mapNotNull { | ||||
|                         it to ( | ||||
|                             bot.execute( | ||||
|                                 ForwardMessage( | ||||
|                                     toChatId = cacheChatId, | ||||
|                                     fromChatId = it.chatId, | ||||
|                                     messageId = it.messageId | ||||
|                                 ) | ||||
|                             ) as? ContentMessage<*> ?: return@mapNotNull null) | ||||
|                     }.mapNotNull { (src, forwardedMessage) -> | ||||
|                         val forwardedMessageAsMediaPartMessage = forwardedMessage.takeIf { | ||||
|                             it.content is MediaGroupPartContent | ||||
|                         } ?.let { | ||||
|                             it as ContentMessage<MediaGroupPartContent> | ||||
|                         } | ||||
|                         src to (forwardedMessageAsMediaPartMessage ?: null.also { _ -> | ||||
|                             sourceMessagesToSentMessages.add( | ||||
|                                 src to MessageMetaInfo( | ||||
|                                     targetChatId, | ||||
|                                     bot.execute( | ||||
|                                         CopyMessage( | ||||
|                                             targetChatId, | ||||
|                                             fromChatId = forwardedMessage.chat.id, | ||||
|                                             messageId = forwardedMessage.messageId | ||||
|                                         ) | ||||
|                                     ) | ||||
|                                 ) | ||||
|                             ) | ||||
|                         } ?: return@mapNotNull null) | ||||
|                     } | ||||
|  | ||||
|                     resultContents.singleOrNull() ?.also { (src, it) -> | ||||
|                         sourceMessagesToSentMessages.add( | ||||
|                             src to MessageMetaInfo( | ||||
|                                 targetChatId, | ||||
|                                 bot.execute( | ||||
|                                     CopyMessage( | ||||
|                                         targetChatId, | ||||
|                                         it.chat.id, | ||||
|                                         it.messageId | ||||
|                                     ) | ||||
|                                 ) | ||||
|                             ) | ||||
|                         ) | ||||
|                     } ?: resultContents.chunked(mediaCountInMediaGroup.last).forEach { | ||||
|                         bot.execute( | ||||
|                             SendMediaGroup<MediaGroupPartContent>( | ||||
|                                 targetChatId, | ||||
|                                 it.map { it.second.content.toMediaGroupMemberTelegramMedia() } | ||||
|                             ) | ||||
|                         ).content.group.mapIndexed { i, partWrapper -> | ||||
|                             it.getOrNull(i) ?.let { | ||||
|                                 sourceMessagesToSentMessages.add( | ||||
|                                     it.first to MessageMetaInfo( | ||||
|                                         partWrapper.sourceMessage.chat.id, | ||||
|                                         partWrapper.sourceMessage.messageId, | ||||
|                                         partWrapper.sourceMessage.mediaGroupId | ||||
|                                     ) | ||||
|                                 ) | ||||
|                             } | ||||
|                         } | ||||
|                     } | ||||
|                 } | ||||
|             } | ||||
|  | ||||
|             sourceMessagesToSentMessages.toList() | ||||
|         } | ||||
|  | ||||
|     } | ||||
| } | ||||
| @@ -7,6 +7,8 @@ String[] includes = [ | ||||
|  | ||||
|     ":cache:content:common", | ||||
|     ":cache:content:micro_utils", | ||||
|  | ||||
|     ":resender", | ||||
| ] | ||||
|  | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user