mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI-examples.git
synced 2025-10-07 08:20:08 +00:00
complete sample with native
This commit is contained in:
@@ -1,23 +1,25 @@
|
||||
import dev.inmo.micro_utils.common.MPPFile
|
||||
import dev.inmo.micro_utils.common.filesize
|
||||
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.*
|
||||
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.api.telegramBot
|
||||
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
|
||||
import dev.inmo.tgbotapi.types.BotCommand
|
||||
import dev.inmo.tgbotapi.types.chat.Chat
|
||||
import dev.inmo.tgbotapi.types.files.DocumentFile
|
||||
import dev.inmo.tgbotapi.types.media.TelegramMediaDocument
|
||||
import dev.inmo.tgbotapi.types.mediaCountInMediaGroup
|
||||
import java.io.File
|
||||
|
||||
private const val command = "send_file"
|
||||
|
||||
expect fun pickFile(currentRoot: MPPFile): MPPFile?
|
||||
|
||||
/**
|
||||
* 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
|
||||
@@ -25,19 +27,10 @@ private const val command = "send_file"
|
||||
* /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
|
||||
*/
|
||||
suspend fun main(args: Array<String>) {
|
||||
val botToken = args.first()
|
||||
val directoryOrFile = args.getOrNull(1) ?.let { File(it) } ?: File("")
|
||||
suspend fun doRandomFileSenderBot(token: String, folder: MPPFile) {
|
||||
val bot = telegramBot(token)
|
||||
|
||||
fun pickFile(currentRoot: File = directoryOrFile): File? {
|
||||
if (currentRoot.isFile) {
|
||||
return currentRoot
|
||||
} else {
|
||||
return pickFile(currentRoot.listFiles() ?.takeIf { it.isNotEmpty() } ?.random() ?: return null)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun TelegramBot.sendFiles(chat: Chat, files: List<File>) {
|
||||
suspend fun TelegramBot.sendFiles(chat: Chat, files: List<MPPFile>) {
|
||||
when (files.size) {
|
||||
1 -> sendDocument(
|
||||
chat.id,
|
||||
@@ -52,8 +45,6 @@ suspend fun main(args: Array<String>) {
|
||||
}
|
||||
}
|
||||
|
||||
val bot = telegramBot(botToken)
|
||||
|
||||
bot.buildBehaviourWithLongPolling (defaultExceptionsHandler = { it.printStackTrace() }) {
|
||||
onCommandWithArgs(command) { message, args ->
|
||||
|
||||
@@ -62,10 +53,10 @@ suspend fun main(args: Array<String>) {
|
||||
var sent = false
|
||||
|
||||
var left = count
|
||||
val chosen = mutableListOf<File>()
|
||||
val chosen = mutableListOf<MPPFile>()
|
||||
|
||||
while (left > 0) {
|
||||
val picked = pickFile() ?.takeIf { it.filesize > 0 } ?: continue
|
||||
val picked = pickFile(folder) ?.takeIf { it.filesize > 0 } ?: continue
|
||||
chosen.add(picked)
|
||||
left--
|
||||
if (chosen.size >= mediaCountInMediaGroup.last) {
|
10
RandomFileSenderBot/src/jvmMain/kotlin/ActualPickFile.kt
Normal file
10
RandomFileSenderBot/src/jvmMain/kotlin/ActualPickFile.kt
Normal file
@@ -0,0 +1,10 @@
|
||||
import dev.inmo.micro_utils.common.MPPFile
|
||||
import java.io.File
|
||||
|
||||
actual fun pickFile(currentRoot: MPPFile): File? {
|
||||
if (currentRoot.isFile) {
|
||||
return currentRoot
|
||||
} else {
|
||||
return pickFile(currentRoot.listFiles() ?.takeIf { it.isNotEmpty() } ?.random() ?: return null)
|
||||
}
|
||||
}
|
@@ -0,0 +1,5 @@
|
||||
import dev.inmo.micro_utils.common.MPPFile
|
||||
|
||||
suspend fun main(args: Array<String>) {
|
||||
doRandomFileSenderBot(args.first(), MPPFile(args.getOrNull(1) ?: ""))
|
||||
}
|
10
RandomFileSenderBot/src/nativeMain/kotlin/ActualPickFile.kt
Normal file
10
RandomFileSenderBot/src/nativeMain/kotlin/ActualPickFile.kt
Normal file
@@ -0,0 +1,10 @@
|
||||
import dev.inmo.micro_utils.common.MPPFile
|
||||
import okio.FileSystem
|
||||
|
||||
actual fun pickFile(currentRoot: MPPFile): MPPFile? {
|
||||
if (FileSystem.SYSTEM.exists(currentRoot) && FileSystem.SYSTEM.listOrNull(currentRoot) == null) {
|
||||
return currentRoot
|
||||
} else {
|
||||
return pickFile(FileSystem.SYSTEM.list(currentRoot).takeIf { it.isNotEmpty() } ?.random() ?: return null)
|
||||
}
|
||||
}
|
@@ -0,0 +1,8 @@
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import okio.Path.Companion.toPath
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
runBlocking {
|
||||
doRandomFileSenderBot(args.first(), args.getOrNull(1) ?.toPath() ?: "".toPath())
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user