mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI-examples.git
synced 2024-11-24 02:58:49 +00:00
add files loader bot
This commit is contained in:
parent
426f8ffc61
commit
c2788224e7
9
FilesLoaderBot/README.md
Normal file
9
FilesLoaderBot/README.md
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
# FilesLoaderBot
|
||||||
|
|
||||||
|
This bot will download incoming files
|
||||||
|
|
||||||
|
## Launch
|
||||||
|
|
||||||
|
```bash
|
||||||
|
../gradlew run --args="BOT_TOKEN[ optional/folder/path]"
|
||||||
|
```
|
24
FilesLoaderBot/build.gradle
Normal file
24
FilesLoaderBot/build.gradle
Normal 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"
|
||||||
|
}
|
42
FilesLoaderBot/src/main/kotlin/FilesLoaderBot.kt
Normal file
42
FilesLoaderBot/src/main/kotlin/FilesLoaderBot.kt
Normal 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()
|
||||||
|
}
|
@ -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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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
|
||||||
|
@ -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"
|
||||||
|
Loading…
Reference in New Issue
Block a user