From ed2922045cd8548ec1676fbb8d80c28c382583ad Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sun, 8 Sep 2024 19:33:43 +0600 Subject: [PATCH] add giveaways bot --- GiveawaysBot/README.md | 9 ++++ GiveawaysBot/build.gradle | 21 ++++++++ GiveawaysBot/src/main/kotlin/GiveawaysBot.kt | 55 ++++++++++++++++++++ settings.gradle | 2 + 4 files changed, 87 insertions(+) create mode 100644 GiveawaysBot/README.md create mode 100644 GiveawaysBot/build.gradle create mode 100644 GiveawaysBot/src/main/kotlin/GiveawaysBot.kt diff --git a/GiveawaysBot/README.md b/GiveawaysBot/README.md new file mode 100644 index 0000000..edc7264 --- /dev/null +++ b/GiveawaysBot/README.md @@ -0,0 +1,9 @@ +# CustomBot + +Printing giveaways + +## Launch + +```bash +../gradlew run --args="BOT_TOKEN" +``` diff --git a/GiveawaysBot/build.gradle b/GiveawaysBot/build.gradle new file mode 100644 index 0000000..d78aa5d --- /dev/null +++ b/GiveawaysBot/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="GiveawaysBotKt" + + +dependencies { + implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" + + implementation "dev.inmo:tgbotapi:$telegram_bot_api_version" +} diff --git a/GiveawaysBot/src/main/kotlin/GiveawaysBot.kt b/GiveawaysBot/src/main/kotlin/GiveawaysBot.kt new file mode 100644 index 0000000..7912879 --- /dev/null +++ b/GiveawaysBot/src/main/kotlin/GiveawaysBot.kt @@ -0,0 +1,55 @@ +import dev.inmo.kslog.common.KSLog +import dev.inmo.kslog.common.LogLevel +import dev.inmo.kslog.common.defaultMessageFormatter +import dev.inmo.kslog.common.setDefaultKSLog +import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions +import dev.inmo.tgbotapi.extensions.api.bot.getMe +import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviourAndLongPolling +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onGiveawayCompleted +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onGiveawayContent +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onGiveawayCreated +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onGiveawayWinners +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers + +/** + * This place can be the playground for your code. + */ +suspend fun main(vararg args: String) { + val botToken = args.first() + + val isDebug = args.any { it == "debug" } + val isTestServer = args.any { it == "testServer" } + + if (isDebug) { + setDefaultKSLog( + KSLog { level: LogLevel, tag: String?, message: Any, throwable: Throwable? -> + println(defaultMessageFormatter(level, tag, message, throwable)) + } + ) + } + + telegramBotWithBehaviourAndLongPolling(botToken, CoroutineScope(Dispatchers.IO), testServer = isTestServer) { + // start here!! + val me = getMe() + println(me) + + onGiveawayCreated { + println(it) + } + + onGiveawayCompleted { + println(it) + } + + onGiveawayWinners { + println(it) + } + + onGiveawayContent { } + + allUpdatesFlow.subscribeSafelyWithoutExceptions(this) { + println(it) + } + }.second.join() +} diff --git a/settings.gradle b/settings.gradle index 698fbac..69b62eb 100644 --- a/settings.gradle +++ b/settings.gradle @@ -52,6 +52,8 @@ include ":BusinessConnectionsBot" include ":StarTransactionsBot" +include ":GiveawaysBot" + include ":CustomBot" include ":MemberUpdatedWatcherBot"