|
| 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.subscribeSafelyWithoutExceptions |
| 6 | +import dev.inmo.tgbotapi.extensions.api.bot.getMe |
| 7 | +import dev.inmo.tgbotapi.extensions.api.chat.get.getChat |
| 8 | +import dev.inmo.tgbotapi.extensions.api.send.reply |
| 9 | +import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContextData |
| 10 | +import dev.inmo.tgbotapi.extensions.behaviour_builder.buildSubcontextInitialAction |
| 11 | +import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviourAndLongPolling |
| 12 | +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand |
| 13 | +import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage |
| 14 | +import dev.inmo.tgbotapi.types.update.abstracts.Update |
| 15 | +import kotlinx.coroutines.CoroutineScope |
| 16 | +import kotlinx.coroutines.Dispatchers |
| 17 | + |
| 18 | +private var BehaviourContextData.update: Update? |
| 19 | + get() = get("update") as? Update |
| 20 | + set(value) = set("update", value) |
| 21 | + |
| 22 | +private var BehaviourContextData.commonMessage: CommonMessage<*>? |
| 23 | + get() = get("commonMessage") as? CommonMessage<*> |
| 24 | + set(value) = set("commonMessage", value) |
| 25 | + |
| 26 | +/** |
| 27 | + * This place can be the playground for your code. |
| 28 | + */ |
| 29 | +suspend fun main(vararg args: String) { |
| 30 | + val botToken = args.first() |
| 31 | + |
| 32 | + val isDebug = args.any { it == "debug" } |
| 33 | + val isTestServer = args.any { it == "testServer" } |
| 34 | + |
| 35 | + if (isDebug) { |
| 36 | + setDefaultKSLog( |
| 37 | + KSLog { level: LogLevel, tag: String?, message: Any, throwable: Throwable? -> |
| 38 | + println(defaultMessageFormatter(level, tag, message, throwable)) |
| 39 | + } |
| 40 | + ) |
| 41 | + } |
| 42 | + |
| 43 | + telegramBotWithBehaviourAndLongPolling( |
| 44 | + botToken, |
| 45 | + CoroutineScope(Dispatchers.IO), |
| 46 | + testServer = isTestServer, |
| 47 | + builder = { |
| 48 | + includeMiddlewares { |
| 49 | + addMiddleware { |
| 50 | + doOnRequestReturnResult { result, request, _ -> |
| 51 | + println("Result of $request:\n\n$result") |
| 52 | + null |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + }, |
| 57 | + subcontextInitialAction = buildSubcontextInitialAction { |
| 58 | + add { |
| 59 | + data.update = it |
| 60 | + } |
| 61 | + } |
| 62 | + ) { |
| 63 | + // start here!! |
| 64 | + val me = getMe() |
| 65 | + println(me) |
| 66 | + |
| 67 | + onCommand("start") { |
| 68 | + println(data.update) |
| 69 | + println(data.commonMessage) |
| 70 | + println(getChat(it.chat)) |
| 71 | + } |
| 72 | + |
| 73 | + onCommand("canManageBots") { |
| 74 | + val me = getMe() |
| 75 | + reply(it, if (me.canManageBots) "Yes" else "No") |
| 76 | + } |
| 77 | + |
| 78 | + allUpdatesFlow.subscribeSafelyWithoutExceptions(this) { |
| 79 | + println(it) |
| 80 | + } |
| 81 | + }.second.join() |
| 82 | +} |
0 commit comments