TelegramBotAPI-examples/RandomFileSenderBot/src/main/kotlin/RandomFileSenderBot.kt

96 lines
3.5 KiB
Kotlin
Raw Normal View History

import dev.inmo.micro_utils.common.filesize
2022-05-10 18:23:14 +00:00
import dev.inmo.tgbotapi.bot.ktor.telegramBot
import dev.inmo.tgbotapi.bot.TelegramBot
2020-10-04 11:32:50 +00:00
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
2021-01-18 17:32:47 +00:00
import dev.inmo.tgbotapi.extensions.api.send.reply
import dev.inmo.tgbotapi.extensions.api.send.withUploadDocumentAction
2021-11-11 06:34:52 +00:00
import dev.inmo.tgbotapi.extensions.behaviour_builder.buildBehaviourWithLongPolling
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommandWithArgs
import dev.inmo.tgbotapi.requests.abstracts.asMultipartFile
2020-10-04 11:32:50 +00:00
import dev.inmo.tgbotapi.types.BotCommand
2022-05-10 18:23:14 +00:00
import dev.inmo.tgbotapi.types.chat.Chat
import dev.inmo.tgbotapi.types.media.TelegramMediaDocument
import dev.inmo.tgbotapi.types.mediaCountInMediaGroup
2020-06-02 14:24:41 +00:00
import java.io.File
private const val command = "send_file"
/**
2021-09-09 03:20:17 +00:00
* This bot will send files inside of working directory OR from directory in the second argument.
* You may send /send_file command to this bot to get random file from the directory OR
* `/send_file $number` when you want to receive required number of files. For example,
* /send_file and `/send_file 1` will have the same effect - bot will send one random file.
* But if you will send `/send_file 5` it will choose 5 random files and send them as group
2020-06-02 14:24:41 +00:00
*/
suspend fun main(args: Array<String>) {
val botToken = args.first()
val directoryOrFile = args.getOrNull(1) ?.let { File(it) } ?: File("")
fun pickFile(currentRoot: File = directoryOrFile): File? {
if (currentRoot.isFile) {
return currentRoot
} else {
2021-09-21 17:22:13 +00:00
return pickFile(currentRoot.listFiles() ?.takeIf { it.isNotEmpty() } ?.random() ?: return null)
2020-06-02 14:24:41 +00:00
}
}
suspend fun TelegramBot.sendFiles(chat: Chat, files: List<File>) {
when (files.size) {
1 -> sendDocument(
chat.id,
2022-01-02 06:06:32 +00:00
files.first().asMultipartFile(),
protectContent = true
)
else -> sendDocumentsGroup(
chat,
2022-05-10 18:23:14 +00:00
files.map { TelegramMediaDocument(it.asMultipartFile()) },
2022-01-02 06:06:32 +00:00
protectContent = true
)
}
}
2020-06-02 14:24:41 +00:00
val bot = telegramBot(botToken)
2021-11-11 06:34:52 +00:00
bot.buildBehaviourWithLongPolling (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<File>()
while (left > 0) {
val picked = pickFile() ?.takeIf { it.filesize > 0 } ?: continue
chosen.add(picked)
left--
if (chosen.size >= mediaCountInMediaGroup.last) {
sendFiles(message.chat, chosen)
chosen.clear()
sent = true
}
}
2021-09-21 17:22:13 +00:00
if (chosen.isNotEmpty()) {
sendFiles(message.chat, chosen)
sent = true
}
if (!sent) {
bot.reply(message, "Nothing selected :(")
}
}
2021-01-18 17:32:47 +00:00
}
2021-01-18 17:32:47 +00:00
setMyCommands(
2020-06-02 14:24:41 +00:00
BotCommand(command, "Send some random file in picker directory")
)
2021-01-18 17:32:47 +00:00
println(getMe())
}.join()
2020-06-02 14:24:41 +00:00
}