add files loader bot

This commit is contained in:
InsanusMokrassar 2020-08-23 21:52:13 +06:00
parent 426f8ffc61
commit c2788224e7
6 changed files with 78 additions and 6 deletions

9
FilesLoaderBot/README.md Normal file
View File

@ -0,0 +1,9 @@
# FilesLoaderBot
This bot will download incoming files
## Launch
```bash
../gradlew run --args="BOT_TOKEN[ optional/folder/path]"
```

View File

@ -0,0 +1,24 @@
buildscript {
repositories {
jcenter()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
apply plugin: 'kotlin'
apply plugin: 'application'
mainClassName="FilesLoaderBotKt"
repositories {
jcenter()
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation "com.github.insanusmokrassar:TelegramBotAPI:$telegram_bot_api_version"
}

View File

@ -0,0 +1,42 @@
import com.github.insanusmokrassar.TelegramBotAPI.extensions.api.downloadFile
import com.github.insanusmokrassar.TelegramBotAPI.extensions.api.get.getFileAdditionalInfo
import com.github.insanusmokrassar.TelegramBotAPI.extensions.api.telegramBot
import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.flatMap
import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.safely
import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.shortcuts.*
import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.updates.retrieving.startGettingFlowsUpdatesByLongPolling
import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.abstracts.MediaContent
import com.github.insanusmokrassar.TelegramBotAPI.utils.filenameFromUrl
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
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()
val bot = telegramBot(botToken)
val scope = CoroutineScope(Dispatchers.Default)
bot.startGettingFlowsUpdatesByLongPolling(scope = scope) {
val flow = merge (
filterContentMessages<MediaContent>(),
mediaGroupMessages().flatMap()
)
flow.onEach {
safely({ it.printStackTrace() }) {
val pathedFile = bot.getFileAdditionalInfo(it.content.media)
File(directoryOrFile, pathedFile.filePath.filenameFromUrl).apply {
createNewFile()
writeBytes(bot.downloadFile(pathedFile))
}
}
}.launchIn(scope)
}
scope.coroutineContext[Job]!!.join()
}

View File

@ -30,11 +30,7 @@ suspend fun activateResenderBot(
}.launchIn(scope) }.launchIn(scope)
filterMediaGroupMessages<MediaGroupContent>(scope).onEach { filterMediaGroupMessages<MediaGroupContent>(scope).onEach {
safely { safely {
bot.sendMediaGroup( bot.executeUnsafe(it.createResend(it.chat ?: return@safely, replyTo = it.first().messageId)).also {
it.first().chat,
it.map { it.content.toMediaGroupMemberInputMedia() },
replyToMessageId = it.first().messageId
).also {
print(it) print(it)
} }
} }

View File

@ -2,4 +2,4 @@ kotlin.code.style=official
org.gradle.parallel=true org.gradle.parallel=true
kotlin_version=1.4.0 kotlin_version=1.4.0
telegram_bot_api_version=0.28.0-rc telegram_bot_api_version=0.28.0

View File

@ -2,5 +2,6 @@ include ":ForwarderBot"
include ":RandomFileSenderBot" include ":RandomFileSenderBot"
include ":HelloBot" include ":HelloBot"
include ":GetMeBot" include ":GetMeBot"
include ":FilesLoaderBot"
include ":ResenderBot:ResenderBotLib" include ":ResenderBot:ResenderBotLib"
include ":ResenderBot:jvm_launcher" include ":ResenderBot:jvm_launcher"