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

91 lines
3.2 KiB
Kotlin
Raw Normal View History

2021-01-18 17:32:47 +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-01-18 17:32:47 +00:00
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
2020-10-04 11:32:50 +00:00
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
2020-06-02 14:24:41 +00:00
import kotlinx.coroutines.*
import java.io.File
private const val command = "send_file"
/**
* This bot will send files inside of working directory OR from directory in the second argument
*/
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 {
return pickFile(currentRoot.listFiles() ?.random() ?: return null)
}
}
suspend fun TelegramBot.sendFiles(chat: Chat, files: List<File>) {
when (files.size) {
1 -> sendDocument(
chat.id,
files.first().asMultipartFile()
)
else -> sendDocumentsGroup(
chat,
files.map { InputMediaDocument(it.asMultipartFile()) }
)
}
}
2020-06-02 14:24:41 +00:00
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<File>()
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 :(")
}
}
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
}