diff --git a/FilesLoaderBot/README.md b/FilesLoaderBot/README.md new file mode 100644 index 0000000..09390a2 --- /dev/null +++ b/FilesLoaderBot/README.md @@ -0,0 +1,9 @@ +# FilesLoaderBot + +This bot will download incoming files + +## Launch + +```bash +../gradlew run --args="BOT_TOKEN[ optional/folder/path]" +``` diff --git a/FilesLoaderBot/build.gradle b/FilesLoaderBot/build.gradle new file mode 100644 index 0000000..ab6837b --- /dev/null +++ b/FilesLoaderBot/build.gradle @@ -0,0 +1,24 @@ +buildscript { + repositories { + jcenter() + } + + dependencies { + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" + } +} + +apply plugin: 'kotlin' +apply plugin: 'application' + +mainClassName="FilesLoaderBotKt" + +repositories { + jcenter() +} + +dependencies { + implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" + + implementation "com.github.insanusmokrassar:TelegramBotAPI:$telegram_bot_api_version" +} diff --git a/FilesLoaderBot/src/main/kotlin/FilesLoaderBot.kt b/FilesLoaderBot/src/main/kotlin/FilesLoaderBot.kt new file mode 100644 index 0000000..6ca6e48 --- /dev/null +++ b/FilesLoaderBot/src/main/kotlin/FilesLoaderBot.kt @@ -0,0 +1,42 @@ +import com.github.insanusmokrassar.TelegramBotAPI.extensions.api.downloadFile +import com.github.insanusmokrassar.TelegramBotAPI.extensions.api.get.getFileAdditionalInfo +import com.github.insanusmokrassar.TelegramBotAPI.extensions.api.telegramBot +import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.flatMap +import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.safely +import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.shortcuts.* +import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.updates.retrieving.startGettingFlowsUpdatesByLongPolling +import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.abstracts.MediaContent +import com.github.insanusmokrassar.TelegramBotAPI.utils.filenameFromUrl +import kotlinx.coroutines.* +import kotlinx.coroutines.flow.* +import java.io.File + +/** + * This bot will download incoming files + */ +suspend fun main(args: Array) { + val botToken = args.first() + val directoryOrFile = args.getOrNull(1) ?.let { File(it) } ?: File("") + directoryOrFile.mkdirs() + + val bot = telegramBot(botToken) + val scope = CoroutineScope(Dispatchers.Default) + + bot.startGettingFlowsUpdatesByLongPolling(scope = scope) { + val flow = merge ( + filterContentMessages(), + mediaGroupMessages().flatMap() + ) + flow.onEach { + safely({ it.printStackTrace() }) { + val pathedFile = bot.getFileAdditionalInfo(it.content.media) + File(directoryOrFile, pathedFile.filePath.filenameFromUrl).apply { + createNewFile() + writeBytes(bot.downloadFile(pathedFile)) + } + } + }.launchIn(scope) + } + + scope.coroutineContext[Job]!!.join() +} diff --git a/ResenderBot/ResenderBotLib/src/commonMain/kotlin/ResenderBot.kt b/ResenderBot/ResenderBotLib/src/commonMain/kotlin/ResenderBot.kt index fc363d6..2b67f58 100644 --- a/ResenderBot/ResenderBotLib/src/commonMain/kotlin/ResenderBot.kt +++ b/ResenderBot/ResenderBotLib/src/commonMain/kotlin/ResenderBot.kt @@ -30,11 +30,7 @@ suspend fun activateResenderBot( }.launchIn(scope) filterMediaGroupMessages(scope).onEach { safely { - bot.sendMediaGroup( - it.first().chat, - it.map { it.content.toMediaGroupMemberInputMedia() }, - replyToMessageId = it.first().messageId - ).also { + bot.executeUnsafe(it.createResend(it.chat ?: return@safely, replyTo = it.first().messageId)).also { print(it) } } diff --git a/gradle.properties b/gradle.properties index d647706..f566939 100644 --- a/gradle.properties +++ b/gradle.properties @@ -2,4 +2,4 @@ kotlin.code.style=official org.gradle.parallel=true kotlin_version=1.4.0 -telegram_bot_api_version=0.28.0-rc +telegram_bot_api_version=0.28.0 diff --git a/settings.gradle b/settings.gradle index 216a18d..0506b87 100644 --- a/settings.gradle +++ b/settings.gradle @@ -2,5 +2,6 @@ include ":ForwarderBot" include ":RandomFileSenderBot" include ":HelloBot" include ":GetMeBot" +include ":FilesLoaderBot" include ":ResenderBot:ResenderBotLib" include ":ResenderBot:jvm_launcher"