diff --git a/ChatAvatarSetter/README.md b/ChatAvatarSetter/README.md new file mode 100644 index 0000000..a833ab2 --- /dev/null +++ b/ChatAvatarSetter/README.md @@ -0,0 +1,9 @@ +# ChatAvatarSetter + +This bot will set the chat avatar based on the image sent to bot + +## Launch + +```bash +../gradlew run --args="BOT_TOKEN" +``` diff --git a/ChatAvatarSetter/build.gradle b/ChatAvatarSetter/build.gradle new file mode 100644 index 0000000..c6928eb --- /dev/null +++ b/ChatAvatarSetter/build.gradle @@ -0,0 +1,21 @@ +buildscript { + repositories { + mavenCentral() + } + + dependencies { + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" + } +} + +apply plugin: 'kotlin' +apply plugin: 'application' + +mainClassName="ChatAvatarSetterKt" + + +dependencies { + implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" + + implementation "dev.inmo:tgbotapi:$telegram_bot_api_version" +} diff --git a/ChatAvatarSetter/src/main/kotlin/ChatAvatarSetter.kt b/ChatAvatarSetter/src/main/kotlin/ChatAvatarSetter.kt new file mode 100644 index 0000000..8fcf9dd --- /dev/null +++ b/ChatAvatarSetter/src/main/kotlin/ChatAvatarSetter.kt @@ -0,0 +1,36 @@ +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) { + 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() +} diff --git a/gradle.properties b/gradle.properties index a6d41c4..a5ec16a 100644 --- a/gradle.properties +++ b/gradle.properties @@ -5,7 +5,7 @@ org.gradle.jvmargs=-Xmx768m kotlin_version=1.7.20 -telegram_bot_api_version=3.3.0 -micro_utils_version=0.13.1 +telegram_bot_api_version=3.3.1 +micro_utils_version=0.13.2 serialization_version=1.4.1 -ktor_version=2.1.2 +ktor_version=2.1.3 diff --git a/settings.gradle b/settings.gradle index 529e18e..4d94aff 100644 --- a/settings.gradle +++ b/settings.gradle @@ -21,6 +21,8 @@ include ":StickerInfoBot:jvm_launcher" include ":SlotMachineDetectorBot" +include ":ChatAvatarSetter" + include ":WebApp" include ":FSMBot"