mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI-examples.git
synced 2024-11-22 16:23:54 +00:00
37 lines
1.3 KiB
Kotlin
37 lines
1.3 KiB
Kotlin
import dev.inmo.micro_utils.coroutines.runCatchingSafely
|
|
import dev.inmo.tgbotapi.bot.ktor.telegramBot
|
|
import dev.inmo.tgbotapi.extensions.api.chat.modify.setChatPhoto
|
|
import dev.inmo.tgbotapi.extensions.api.files.downloadFile
|
|
import dev.inmo.tgbotapi.extensions.api.send.reply
|
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.buildBehaviourWithLongPolling
|
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onPhoto
|
|
import dev.inmo.tgbotapi.extensions.utils.*
|
|
import dev.inmo.tgbotapi.requests.abstracts.asMultipartFile
|
|
import kotlinx.coroutines.*
|
|
|
|
suspend fun main(args: Array<String>) {
|
|
val bot = telegramBot(args.first())
|
|
|
|
bot.buildBehaviourWithLongPolling(scope = CoroutineScope(Dispatchers.IO)) {
|
|
onPhoto {
|
|
val bytes = downloadFile(it.content)
|
|
runCatchingSafely {
|
|
setChatPhoto(
|
|
it.chat.id,
|
|
bytes.asMultipartFile("sample.jpg")
|
|
)
|
|
}.onSuccess { b ->
|
|
if (b) {
|
|
reply(it, "Done")
|
|
} else {
|
|
reply(it, "Something went wrong")
|
|
}
|
|
}.onFailure { e ->
|
|
e.printStackTrace()
|
|
|
|
reply(it, "Something went wrong (see logs)")
|
|
}
|
|
}
|
|
}.join()
|
|
}
|