From aee0943a5e3db1e15d49ceb20ecdfff13d4e1ec0 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 8 Sep 2021 23:00:19 +0600 Subject: [PATCH] update dependencies and RandomFileSenderBot example --- .../src/main/kotlin/RandomFileSenderBot.kt | 65 +++++++++++++++---- gradle.properties | 4 +- 2 files changed, 56 insertions(+), 13 deletions(-) diff --git a/RandomFileSenderBot/src/main/kotlin/RandomFileSenderBot.kt b/RandomFileSenderBot/src/main/kotlin/RandomFileSenderBot.kt index acfffd8..5e51b12 100644 --- a/RandomFileSenderBot/src/main/kotlin/RandomFileSenderBot.kt +++ b/RandomFileSenderBot/src/main/kotlin/RandomFileSenderBot.kt @@ -1,12 +1,21 @@ import dev.inmo.tgbotapi.bot.Ktor.telegramBot +import dev.inmo.tgbotapi.bot.TelegramBot import dev.inmo.tgbotapi.extensions.api.bot.getMe import dev.inmo.tgbotapi.extensions.api.bot.setMyCommands import dev.inmo.tgbotapi.extensions.api.send.media.sendDocument +import dev.inmo.tgbotapi.extensions.api.send.media.sendDocumentsGroup import dev.inmo.tgbotapi.extensions.api.send.reply +import dev.inmo.tgbotapi.extensions.api.send.withUploadDocumentAction import dev.inmo.tgbotapi.extensions.behaviour_builder.buildBehaviour import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommandWithArgs +import dev.inmo.tgbotapi.requests.abstracts.asMultipartFile import dev.inmo.tgbotapi.requests.abstracts.toInputFile import dev.inmo.tgbotapi.types.BotCommand +import dev.inmo.tgbotapi.types.InputMedia.DocumentMediaGroupMemberInputMedia +import dev.inmo.tgbotapi.types.InputMedia.InputMediaDocument +import dev.inmo.tgbotapi.types.chat.abstracts.Chat +import dev.inmo.tgbotapi.types.mediaCountInMediaGroup import kotlinx.coroutines.* import java.io.File @@ -27,21 +36,55 @@ suspend fun main(args: Array) { } } - val bot = telegramBot(botToken) - val scope = CoroutineScope(Dispatchers.Default) - - bot.buildBehaviour(scope) { - onCommand(command.toRegex()) { message -> - pickFile() ?.let { - bot.sendDocument( - message.chat.id, - it.toInputFile() - ) - } ?: bot.reply(message, "Nothing selected :(") + suspend fun TelegramBot.sendFiles(chat: Chat, files: List) { + when (files.size) { + 1 -> sendDocument( + chat.id, + files.first().asMultipartFile() + ) + else -> sendDocumentsGroup( + chat, + files.map { InputMediaDocument(it.asMultipartFile()) } + ) } + } + + val bot = telegramBot(botToken) + + bot.buildBehaviour(defaultExceptionsHandler = { it.printStackTrace() }) { + onCommandWithArgs(command) { message, args -> + + withUploadDocumentAction(message.chat) { + val count = args.firstOrNull() ?.toIntOrNull() ?: 1 + var sent = false + + var left = count + val chosen = mutableListOf() + + while (left > 0) { + left-- + val picked = pickFile() ?: continue + chosen.add(picked) + if (chosen.size >= mediaCountInMediaGroup.last) { + sendFiles(message.chat, chosen) + chosen.clear() + sent = true + } + } + + sendFiles(message.chat, chosen) + sent || chosen.isNotEmpty() + + if (!sent) { + bot.reply(message, "Nothing selected :(") + } + } + } + setMyCommands( BotCommand(command, "Send some random file in picker directory") ) + println(getMe()) }.join() } diff --git a/gradle.properties b/gradle.properties index b11bb80..650ef28 100644 --- a/gradle.properties +++ b/gradle.properties @@ -3,5 +3,5 @@ org.gradle.parallel=true kotlin_version=1.5.30 -telegram_bot_api_version=0.35.7 -micro_utils_version=0.5.24 +telegram_bot_api_version=0.35.8 +micro_utils_version=0.5.25