2023-03-11 15:39:08 +00:00
|
|
|
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
|
2021-08-25 09:58:27 +00:00
|
|
|
import dev.inmo.tgbotapi.extensions.api.files.downloadFile
|
2023-03-11 15:39:08 +00:00
|
|
|
import dev.inmo.tgbotapi.extensions.api.files.downloadFileToTemp
|
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
|
2023-03-11 15:39:08 +00:00
|
|
|
import dev.inmo.tgbotapi.extensions.api.send.replyWithAnimation
|
|
|
|
import dev.inmo.tgbotapi.extensions.api.send.replyWithAudio
|
|
|
|
import dev.inmo.tgbotapi.extensions.api.send.replyWithDocument
|
|
|
|
import dev.inmo.tgbotapi.extensions.api.send.replyWithMediaGroup
|
|
|
|
import dev.inmo.tgbotapi.extensions.api.send.replyWithPhoto
|
|
|
|
import dev.inmo.tgbotapi.extensions.api.send.replyWithSticker
|
|
|
|
import dev.inmo.tgbotapi.extensions.api.send.replyWithVideo
|
|
|
|
import dev.inmo.tgbotapi.extensions.api.send.replyWithVideoNote
|
|
|
|
import dev.inmo.tgbotapi.extensions.api.send.replyWithVoice
|
|
|
|
import dev.inmo.tgbotapi.extensions.api.send.withAction
|
2021-11-11 06:34:52 +00:00
|
|
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviourAndLongPolling
|
2023-02-06 08:07:44 +00:00
|
|
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand
|
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
|
2023-03-11 15:39:08 +00:00
|
|
|
import dev.inmo.tgbotapi.requests.abstracts.asMultipartFile
|
|
|
|
import dev.inmo.tgbotapi.requests.send.SendAction
|
|
|
|
import dev.inmo.tgbotapi.types.actions.BotAction
|
|
|
|
import dev.inmo.tgbotapi.types.actions.TypingAction
|
|
|
|
import dev.inmo.tgbotapi.types.media.TelegramMediaAudio
|
|
|
|
import dev.inmo.tgbotapi.types.media.TelegramMediaDocument
|
|
|
|
import dev.inmo.tgbotapi.types.media.TelegramMediaPhoto
|
|
|
|
import dev.inmo.tgbotapi.types.media.TelegramMediaVideo
|
|
|
|
import dev.inmo.tgbotapi.types.message.content.AnimationContent
|
|
|
|
import dev.inmo.tgbotapi.types.message.content.AudioContent
|
|
|
|
import dev.inmo.tgbotapi.types.message.content.DocumentContent
|
|
|
|
import dev.inmo.tgbotapi.types.message.content.MediaGroupContent
|
|
|
|
import dev.inmo.tgbotapi.types.message.content.PhotoContent
|
|
|
|
import dev.inmo.tgbotapi.types.message.content.StickerContent
|
|
|
|
import dev.inmo.tgbotapi.types.message.content.VideoContent
|
|
|
|
import dev.inmo.tgbotapi.types.message.content.VideoNoteContent
|
|
|
|
import dev.inmo.tgbotapi.types.message.content.VoiceContent
|
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)) {
|
2023-02-06 08:07:44 +00:00
|
|
|
onCommand("start") {
|
|
|
|
reply(it, "Send me any media (like photo or video) to download it")
|
|
|
|
}
|
2021-09-21 17:22:13 +00:00
|
|
|
onMedia(initialFilter = null) {
|
2023-03-11 15:39:08 +00:00
|
|
|
val content = it.content
|
|
|
|
val pathedFile = bot.getFileAdditionalInfo(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 {
|
2023-03-11 15:39:08 +00:00
|
|
|
bot.downloadFile(content.media, outFile)
|
2022-08-04 16:32:36 +00:00
|
|
|
}.onFailure {
|
|
|
|
it.printStackTrace()
|
2023-03-11 15:39:08 +00:00
|
|
|
}.onSuccess { _ ->
|
|
|
|
reply(it, "Saved to ${outFile.absolutePath}")
|
|
|
|
withAction(it.chat.id, TypingAction) {
|
|
|
|
when (content) {
|
|
|
|
is PhotoContent -> replyWithPhoto(
|
|
|
|
it,
|
|
|
|
outFile.asMultipartFile()
|
|
|
|
)
|
|
|
|
is AnimationContent -> replyWithAnimation(
|
|
|
|
it,
|
|
|
|
outFile.asMultipartFile()
|
|
|
|
)
|
|
|
|
is VideoContent -> replyWithVideo(
|
|
|
|
it,
|
|
|
|
outFile.asMultipartFile()
|
|
|
|
)
|
|
|
|
is StickerContent -> replyWithSticker(
|
|
|
|
it,
|
|
|
|
outFile.asMultipartFile()
|
|
|
|
)
|
|
|
|
is MediaGroupContent<*> -> replyWithMediaGroup(
|
|
|
|
it,
|
|
|
|
content.group.map {
|
|
|
|
when (val innerContent = it.content) {
|
|
|
|
is AudioContent -> TelegramMediaAudio(
|
|
|
|
downloadFileToTemp(innerContent.media).asMultipartFile()
|
|
|
|
)
|
|
|
|
is DocumentContent -> TelegramMediaDocument(
|
|
|
|
downloadFileToTemp(innerContent.media).asMultipartFile()
|
|
|
|
)
|
|
|
|
is PhotoContent -> TelegramMediaPhoto(
|
|
|
|
downloadFileToTemp(innerContent.media).asMultipartFile()
|
|
|
|
)
|
|
|
|
is VideoContent -> TelegramMediaVideo(
|
|
|
|
downloadFileToTemp(innerContent.media).asMultipartFile()
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
is AudioContent -> replyWithAudio(
|
|
|
|
it,
|
|
|
|
outFile.asMultipartFile()
|
|
|
|
)
|
|
|
|
is DocumentContent -> replyWithDocument(
|
|
|
|
it,
|
|
|
|
outFile.asMultipartFile()
|
|
|
|
)
|
|
|
|
is VoiceContent -> replyWithVoice(
|
|
|
|
it,
|
|
|
|
outFile.asMultipartFile()
|
|
|
|
)
|
|
|
|
is VideoNoteContent -> replyWithVideoNote(
|
|
|
|
it,
|
|
|
|
outFile.asMultipartFile()
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2022-08-04 16:32:36 +00:00
|
|
|
}
|
2021-06-27 19:14:16 +00:00
|
|
|
}
|
2023-03-11 15:39:08 +00:00
|
|
|
allUpdatesFlow.subscribeSafelyWithoutExceptions(this) { println(it) }
|
2021-06-27 19:14:16 +00:00
|
|
|
}.second.join()
|
2020-08-23 15:52:13 +00:00
|
|
|
}
|