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()
|
2022-08-04 16:32:36 +00:00
|
|
|
val directoryOrFile = args.getOrNull(1) ?.let { File(it) } ?: File("/tmp/")
|
2020-08-23 15:52:13 +00:00
|
|
|
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)
|
2022-06-21 13:45:24 +00:00
|
|
|
val outFile = File(directoryOrFile, pathedFile.filePath.filenameFromUrl)
|
2022-08-04 16:32:36 +00:00
|
|
|
runCatching {
|
|
|
|
bot.downloadFile(it.content.media, outFile)
|
|
|
|
}.onFailure {
|
|
|
|
it.printStackTrace()
|
|
|
|
}
|
2022-06-21 13:45:24 +00:00
|
|
|
reply(it, "Saved to ${outFile.absolutePath}")
|
2021-06-27 19:14:16 +00:00
|
|
|
}
|
|
|
|
onContentMessage { println(it) }
|
|
|
|
}.second.join()
|
2020-08-23 15:52:13 +00:00
|
|
|
}
|