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

32 lines
1.3 KiB
Kotlin
Raw Normal View History

2021-08-25 09:58:27 +00:00
import dev.inmo.tgbotapi.extensions.api.files.downloadFile
2020-10-04 11:32:50 +00:00
import dev.inmo.tgbotapi.extensions.api.get.getFileAdditionalInfo
2021-06-27 19:14:16 +00:00
import dev.inmo.tgbotapi.extensions.api.send.reply
2021-11-11 06:34:52 +00:00
import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviourAndLongPolling
2021-06-27 19:14:16 +00:00
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onContentMessage
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onMedia
2020-10-04 11:32:50 +00:00
import dev.inmo.tgbotapi.utils.filenameFromUrl
2021-06-27 19:14:16 +00:00
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
2020-08-23 15:52:13 +00:00
import java.io.File
/**
* This bot will download incoming files
*/
suspend fun main(args: Array<String>) {
val botToken = args.first()
val directoryOrFile = args.getOrNull(1) ?.let { File(it) } ?: File("")
directoryOrFile.mkdirs()
2021-11-11 06:34:52 +00:00
telegramBotWithBehaviourAndLongPolling(botToken, CoroutineScope(Dispatchers.IO)) {
2021-09-21 17:22:13 +00:00
onMedia(initialFilter = null) {
2021-06-27 19:14:16 +00:00
val pathedFile = bot.getFileAdditionalInfo(it.content.media)
val file = File(directoryOrFile, pathedFile.filePath.filenameFromUrl).apply {
createNewFile()
writeBytes(bot.downloadFile(pathedFile))
2020-08-23 15:52:13 +00:00
}
2021-06-27 19:14:16 +00:00
reply(it, "Saved to ${file.absolutePath}")
}
onContentMessage { println(it) }
}.second.join()
2020-08-23 15:52:13 +00:00
}