|
| 1 | +import dev.inmo.kslog.common.KSLog |
| 2 | +import dev.inmo.kslog.common.LogLevel |
| 3 | +import dev.inmo.kslog.common.defaultMessageFormatter |
| 4 | +import dev.inmo.kslog.common.setDefaultKSLog |
| 5 | +import dev.inmo.micro_utils.coroutines.runCatchingLogging |
| 6 | +import dev.inmo.micro_utils.coroutines.subscribeLoggingDropExceptions |
| 7 | +import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions |
| 8 | +import dev.inmo.tgbotapi.extensions.api.bot.getMe |
| 9 | +import dev.inmo.tgbotapi.extensions.api.bot.getMyStarBalance |
| 10 | +import dev.inmo.tgbotapi.extensions.api.chat.get.getChat |
| 11 | +import dev.inmo.tgbotapi.extensions.api.send.reply |
| 12 | +import dev.inmo.tgbotapi.extensions.api.send.resend |
| 13 | +import dev.inmo.tgbotapi.extensions.api.send.send |
| 14 | +import dev.inmo.tgbotapi.extensions.api.suggested.approveSuggestedPost |
| 15 | +import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContextData |
| 16 | +import dev.inmo.tgbotapi.extensions.behaviour_builder.buildSubcontextInitialAction |
| 17 | +import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitSuggestedPostApproved |
| 18 | +import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitSuggestedPostDeclined |
| 19 | +import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviourAndLongPolling |
| 20 | +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onChannelDirectMessagesConfigurationChanged |
| 21 | +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onChecklistContent |
| 22 | +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onChecklistTasksAdded |
| 23 | +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onChecklistTasksDone |
| 24 | +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand |
| 25 | +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onContentMessage |
| 26 | +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onSuggestedPostApprovalFailed |
| 27 | +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onSuggestedPostApproved |
| 28 | +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onSuggestedPostDeclined |
| 29 | +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onSuggestedPostPaid |
| 30 | +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onSuggestedPostRefunded |
| 31 | +import dev.inmo.tgbotapi.extensions.utils.channelDirectMessagesContentMessageOrNull |
| 32 | +import dev.inmo.tgbotapi.extensions.utils.previewChannelDirectMessagesChatOrNull |
| 33 | +import dev.inmo.tgbotapi.extensions.utils.suggestedChannelDirectMessagesContentMessageOrNull |
| 34 | +import dev.inmo.tgbotapi.types.checklists.ChecklistTaskId |
| 35 | +import dev.inmo.tgbotapi.types.message.SuggestedPostParameters |
| 36 | +import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage |
| 37 | +import dev.inmo.tgbotapi.types.message.content.ChecklistContent |
| 38 | +import dev.inmo.tgbotapi.types.message.textsources.TextSourcesList |
| 39 | +import dev.inmo.tgbotapi.types.update.abstracts.Update |
| 40 | +import dev.inmo.tgbotapi.utils.bold |
| 41 | +import dev.inmo.tgbotapi.utils.buildEntities |
| 42 | +import dev.inmo.tgbotapi.utils.code |
| 43 | +import dev.inmo.tgbotapi.utils.firstOf |
| 44 | +import kotlinx.coroutines.CoroutineScope |
| 45 | +import kotlinx.coroutines.Dispatchers |
| 46 | +import kotlinx.coroutines.delay |
| 47 | +import kotlinx.coroutines.flow.filter |
| 48 | +import kotlinx.coroutines.flow.first |
| 49 | + |
| 50 | +suspend fun main(vararg args: String) { |
| 51 | + val botToken = args.first() |
| 52 | + |
| 53 | + val isDebug = args.any { it == "debug" } |
| 54 | + val isTestServer = args.any { it == "testServer" } |
| 55 | + |
| 56 | + if (isDebug) { |
| 57 | + setDefaultKSLog( |
| 58 | + KSLog { level: LogLevel, tag: String?, message: Any, throwable: Throwable? -> |
| 59 | + println(defaultMessageFormatter(level, tag, message, throwable)) |
| 60 | + } |
| 61 | + ) |
| 62 | + } |
| 63 | + |
| 64 | + telegramBotWithBehaviourAndLongPolling( |
| 65 | + botToken, |
| 66 | + CoroutineScope(Dispatchers.Default), |
| 67 | + testServer = isTestServer, |
| 68 | + ) { |
| 69 | + // start here!! |
| 70 | + val me = getMe() |
| 71 | + println(me) |
| 72 | + |
| 73 | + fun ChecklistContent.textBuilderTextSources(): TextSourcesList { |
| 74 | + return buildEntities { |
| 75 | + +checklist.textSources + "\n\n" |
| 76 | + checklist.tasks.forEach { task -> |
| 77 | + +"• " |
| 78 | + code( |
| 79 | + if (task.completionDate != null) { |
| 80 | + "[x] " |
| 81 | + } else { |
| 82 | + "[ ] " |
| 83 | + } |
| 84 | + ) |
| 85 | + |
| 86 | + bold(task.textSources) + "\n" |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + onChecklistContent { messageWithContent -> |
| 92 | + reply(messageWithContent) { |
| 93 | + +messageWithContent.content.textBuilderTextSources() |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + onChecklistTasksDone { eventMessage -> |
| 98 | + reply( |
| 99 | + eventMessage, |
| 100 | + checklistTaskId = eventMessage.chatEvent.markedAsDone ?.firstOrNull() |
| 101 | + ) { |
| 102 | + eventMessage.chatEvent.checklistMessage.content.checklist |
| 103 | + +eventMessage.chatEvent.checklistMessage.content.textBuilderTextSources() |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + onChecklistTasksAdded { messageWithContent -> |
| 108 | + reply( |
| 109 | + messageWithContent.chatEvent.checklistMessage, |
| 110 | + checklistTaskId = messageWithContent.chatEvent.tasks.firstOrNull() ?.id |
| 111 | + ) { |
| 112 | + +messageWithContent.chatEvent.checklistMessage.content.textBuilderTextSources() |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + allUpdatesFlow.subscribeLoggingDropExceptions(this) { |
| 117 | + println(it) |
| 118 | + } |
| 119 | + }.second.join() |
| 120 | +} |
0 commit comments