From fda366d820bc32138a882ca436dd38853025e1b9 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 12 Jan 2024 00:49:59 +0600 Subject: [PATCH] add boosts sample --- BoostsInfoBot/README.md | 9 +++ BoostsInfoBot/build.gradle | 21 +++++++ BoostsInfoBot/src/main/kotlin/BoostsInfo.kt | 65 +++++++++++++++++++++ settings.gradle | 2 + 4 files changed, 97 insertions(+) create mode 100644 BoostsInfoBot/README.md create mode 100644 BoostsInfoBot/build.gradle create mode 100644 BoostsInfoBot/src/main/kotlin/BoostsInfo.kt diff --git a/BoostsInfoBot/README.md b/BoostsInfoBot/README.md new file mode 100644 index 0000000..d03cbab --- /dev/null +++ b/BoostsInfoBot/README.md @@ -0,0 +1,9 @@ +# UserChatShared + +Showing info about boosts + +## Launch + +```bash +../gradlew run --args="BOT_TOKEN" +``` diff --git a/BoostsInfoBot/build.gradle b/BoostsInfoBot/build.gradle new file mode 100644 index 0000000..abef9c9 --- /dev/null +++ b/BoostsInfoBot/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="BoostsInfoKt" + + +dependencies { + implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" + + implementation "dev.inmo:tgbotapi:$telegram_bot_api_version" +} diff --git a/BoostsInfoBot/src/main/kotlin/BoostsInfo.kt b/BoostsInfoBot/src/main/kotlin/BoostsInfo.kt new file mode 100644 index 0000000..fd7f058 --- /dev/null +++ b/BoostsInfoBot/src/main/kotlin/BoostsInfo.kt @@ -0,0 +1,65 @@ +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.tgbotapi.bot.ktor.telegramBot +import dev.inmo.tgbotapi.extensions.api.get.getUserChatBoosts +import dev.inmo.tgbotapi.extensions.api.send.* +import dev.inmo.tgbotapi.extensions.behaviour_builder.buildBehaviourWithLongPolling +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onChatBoostUpdated +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onChatShared +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand +import dev.inmo.tgbotapi.extensions.utils.types.buttons.* +import dev.inmo.tgbotapi.types.chat.member.ChatCommonAdministratorRights +import dev.inmo.tgbotapi.types.request.RequestId +import dev.inmo.tgbotapi.utils.regular +import korlibs.time.DateFormat +import korlibs.time.format + +suspend fun main(args: Array) { + val isDebug = args.getOrNull(1) == "debug" + + if (isDebug) { + setDefaultKSLog( + KSLog { level: LogLevel, tag: String?, message: Any, throwable: Throwable? -> + println(defaultMessageFormatter(level, tag, message, throwable)) + } + ) + } + + val requestChatId = RequestId(1) + + val bot = telegramBot(args.first()) + + bot.buildBehaviourWithLongPolling (defaultExceptionsHandler = { it.printStackTrace() }) { + onChatBoostUpdated { + println(it) + } + + onCommand("start") { + reply( + it, + replyMarkup = flatReplyKeyboard { + requestChannelButton( + "Click me :)", + requestChatId, + botIsMember = true + ) + } + ) { + regular("Select chat to get know about your boosts") + } + } + + onChatShared(initialFilter = { it.chatEvent.requestId == requestChatId }) { + val boosts = getUserChatBoosts(it.chatEvent.chatId, it.chat.id) + reply( + it + ) { + boosts.boosts.forEach { + regular("Boost added: ${DateFormat.FORMAT1.format(it.addDate.asDate)}; Boost expire: ${DateFormat.FORMAT1.format(it.expirationDate.asDate)}; Unformatted: $it") + "\n" + } + } + } + }.join() +} diff --git a/settings.gradle b/settings.gradle index a12a93e..a5231ca 100644 --- a/settings.gradle +++ b/settings.gradle @@ -45,3 +45,5 @@ include ":InlineQueriesBot" include ":ReactionsInfoBot" include ":LinkPreviewsBot" + +include ":BoostsInfoBot"