From 990614e25755b6d29d5c2d02e2c7acf340097c56 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 23 Sep 2025 15:43:58 +0600 Subject: [PATCH] add checklists tasks --- ChecklistsBot/build.gradle | 21 +++ .../src/main/kotlin/ChecklistsBot.kt | 120 ++++++++++++++++++ StarTransactionsBot/README.md | 2 +- gradle.properties | 2 +- settings.gradle | 2 + 5 files changed, 145 insertions(+), 2 deletions(-) create mode 100644 ChecklistsBot/build.gradle create mode 100644 ChecklistsBot/src/main/kotlin/ChecklistsBot.kt diff --git a/ChecklistsBot/build.gradle b/ChecklistsBot/build.gradle new file mode 100644 index 0000000..a5fc55b --- /dev/null +++ b/ChecklistsBot/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="ChecklistsBotKt" + + +dependencies { + implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" + + implementation "dev.inmo:tgbotapi:$telegram_bot_api_version" +} diff --git a/ChecklistsBot/src/main/kotlin/ChecklistsBot.kt b/ChecklistsBot/src/main/kotlin/ChecklistsBot.kt new file mode 100644 index 0000000..bc69334 --- /dev/null +++ b/ChecklistsBot/src/main/kotlin/ChecklistsBot.kt @@ -0,0 +1,120 @@ +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.runCatchingLogging +import dev.inmo.micro_utils.coroutines.subscribeLoggingDropExceptions +import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions +import dev.inmo.tgbotapi.extensions.api.bot.getMe +import dev.inmo.tgbotapi.extensions.api.bot.getMyStarBalance +import dev.inmo.tgbotapi.extensions.api.chat.get.getChat +import dev.inmo.tgbotapi.extensions.api.send.reply +import dev.inmo.tgbotapi.extensions.api.send.resend +import dev.inmo.tgbotapi.extensions.api.send.send +import dev.inmo.tgbotapi.extensions.api.suggested.approveSuggestedPost +import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContextData +import dev.inmo.tgbotapi.extensions.behaviour_builder.buildSubcontextInitialAction +import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitSuggestedPostApproved +import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitSuggestedPostDeclined +import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviourAndLongPolling +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onChannelDirectMessagesConfigurationChanged +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onChecklistContent +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onChecklistTasksAdded +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onChecklistTasksDone +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onContentMessage +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onSuggestedPostApprovalFailed +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onSuggestedPostApproved +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onSuggestedPostDeclined +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onSuggestedPostPaid +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onSuggestedPostRefunded +import dev.inmo.tgbotapi.extensions.utils.channelDirectMessagesContentMessageOrNull +import dev.inmo.tgbotapi.extensions.utils.previewChannelDirectMessagesChatOrNull +import dev.inmo.tgbotapi.extensions.utils.suggestedChannelDirectMessagesContentMessageOrNull +import dev.inmo.tgbotapi.types.checklists.ChecklistTaskId +import dev.inmo.tgbotapi.types.message.SuggestedPostParameters +import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage +import dev.inmo.tgbotapi.types.message.content.ChecklistContent +import dev.inmo.tgbotapi.types.message.textsources.TextSourcesList +import dev.inmo.tgbotapi.types.update.abstracts.Update +import dev.inmo.tgbotapi.utils.bold +import dev.inmo.tgbotapi.utils.buildEntities +import dev.inmo.tgbotapi.utils.code +import dev.inmo.tgbotapi.utils.firstOf +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.delay +import kotlinx.coroutines.flow.filter +import kotlinx.coroutines.flow.first + +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.Default), + testServer = isTestServer, + ) { + // start here!! + val me = getMe() + println(me) + + fun ChecklistContent.textBuilderTextSources(): TextSourcesList { + return buildEntities { + +checklist.textSources + "\n\n" + checklist.tasks.forEach { task -> + +"• " + code( + if (task.completionDate != null) { + "[x] " + } else { + "[ ] " + } + ) + + bold(task.textSources) + "\n" + } + } + } + + onChecklistContent { messageWithContent -> + reply(messageWithContent) { + +messageWithContent.content.textBuilderTextSources() + } + } + + onChecklistTasksDone { eventMessage -> + reply( + eventMessage, + checklistTaskId = eventMessage.chatEvent.markedAsDone ?.firstOrNull() + ) { + eventMessage.chatEvent.checklistMessage.content.checklist + +eventMessage.chatEvent.checklistMessage.content.textBuilderTextSources() + } + } + + onChecklistTasksAdded { messageWithContent -> + reply( + messageWithContent.chatEvent.checklistMessage, + checklistTaskId = messageWithContent.chatEvent.tasks.firstOrNull() ?.id + ) { + +messageWithContent.chatEvent.checklistMessage.content.textBuilderTextSources() + } + } + + allUpdatesFlow.subscribeLoggingDropExceptions(this) { + println(it) + } + }.second.join() +} diff --git a/StarTransactionsBot/README.md b/StarTransactionsBot/README.md index 13b72ef..8790a9d 100644 --- a/StarTransactionsBot/README.md +++ b/StarTransactionsBot/README.md @@ -1,4 +1,4 @@ -# CustomBot +# StarTransactionsBot This bot basically have no any useful behaviour, but you may customize it as a playground diff --git a/gradle.properties b/gradle.properties index 9982181..f78ada0 100644 --- a/gradle.properties +++ b/gradle.properties @@ -6,7 +6,7 @@ kotlin.daemon.jvmargs=-Xmx3g -Xms500m kotlin_version=2.2.10 -telegram_bot_api_version=29.0.0 +telegram_bot_api_version=29.0.0-branch_29.0.0-build2978 micro_utils_version=0.26.3 serialization_version=1.9.0 ktor_version=3.2.3 diff --git a/settings.gradle b/settings.gradle index c8813ac..63cf651 100644 --- a/settings.gradle +++ b/settings.gradle @@ -61,3 +61,5 @@ include ":MemberUpdatedWatcherBot" include ":WebHooks" include ":SuggestedPosts" + +include ":ChecklistsBot"