|
| 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.telegramBotWithBehaviourAndLongPolling |
| 18 | +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onChannelDirectMessagesConfigurationChanged |
| 19 | +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand |
| 20 | +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onContentMessage |
| 21 | +import dev.inmo.tgbotapi.extensions.utils.channelDirectMessagesContentMessageOrNull |
| 22 | +import dev.inmo.tgbotapi.extensions.utils.previewChannelDirectMessagesChatOrNull |
| 23 | +import dev.inmo.tgbotapi.extensions.utils.suggestedChannelDirectMessagesContentMessageOrNull |
| 24 | +import dev.inmo.tgbotapi.types.message.SuggestedPostParameters |
| 25 | +import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage |
| 26 | +import dev.inmo.tgbotapi.types.update.abstracts.Update |
| 27 | +import kotlinx.coroutines.CoroutineScope |
| 28 | +import kotlinx.coroutines.Dispatchers |
| 29 | +import kotlinx.coroutines.delay |
| 30 | + |
| 31 | +/** |
| 32 | + * This place can be the playground for your code. |
| 33 | + */ |
| 34 | +suspend fun main(vararg args: String) { |
| 35 | + val botToken = args.first() |
| 36 | + |
| 37 | + val isDebug = args.any { it == "debug" } |
| 38 | + val isTestServer = args.any { it == "testServer" } |
| 39 | + |
| 40 | + if (isDebug) { |
| 41 | + setDefaultKSLog( |
| 42 | + KSLog { level: LogLevel, tag: String?, message: Any, throwable: Throwable? -> |
| 43 | + println(defaultMessageFormatter(level, tag, message, throwable)) |
| 44 | + } |
| 45 | + ) |
| 46 | + } |
| 47 | + |
| 48 | + telegramBotWithBehaviourAndLongPolling( |
| 49 | + botToken, |
| 50 | + CoroutineScope(Dispatchers.Default), |
| 51 | + testServer = isTestServer, |
| 52 | + ) { |
| 53 | + // start here!! |
| 54 | + val me = getMe() |
| 55 | + println(me) |
| 56 | + |
| 57 | + onCommand("start") { |
| 58 | + println(getChat(it.chat)) |
| 59 | + } |
| 60 | + |
| 61 | + onContentMessage { |
| 62 | + val message = it.channelDirectMessagesContentMessageOrNull() ?: return@onContentMessage |
| 63 | + |
| 64 | + resend( |
| 65 | + message.chat.id, |
| 66 | + message.content, |
| 67 | + suggestedPostParameters = SuggestedPostParameters() |
| 68 | + ) |
| 69 | + } |
| 70 | + |
| 71 | + onContentMessage { |
| 72 | + val suggestedPost = it.suggestedChannelDirectMessagesContentMessageOrNull() ?: return@onContentMessage |
| 73 | + |
| 74 | + for (i in 0 until 3) { |
| 75 | + delay(1000L) |
| 76 | + send(suggestedPost.chat, "${3 - i}") |
| 77 | + } |
| 78 | + approveSuggestedPost(suggestedPost) |
| 79 | + } |
| 80 | + |
| 81 | + allUpdatesFlow.subscribeLoggingDropExceptions(this) { |
| 82 | + println(it) |
| 83 | + } |
| 84 | + }.second.join() |
| 85 | +} |
0 commit comments