|
| 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.subscribeLoggingDropExceptions |
| 6 | +import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions |
| 7 | +import dev.inmo.tgbotapi.extensions.api.bot.getMe |
| 8 | +import dev.inmo.tgbotapi.extensions.api.chat.get.getChat |
| 9 | +import dev.inmo.tgbotapi.extensions.api.managed_bots.getManagedBotToken |
| 10 | +import dev.inmo.tgbotapi.extensions.api.managed_bots.replaceManagedBotToken |
| 11 | +import dev.inmo.tgbotapi.extensions.api.send.reply |
| 12 | +import dev.inmo.tgbotapi.extensions.api.send.send |
| 13 | +import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContextData |
| 14 | +import dev.inmo.tgbotapi.extensions.behaviour_builder.buildSubcontextInitialAction |
| 15 | +import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviourAndLongPolling |
| 16 | +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand |
| 17 | +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onManagedBotCreated |
| 18 | +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onManagedBotUpdated |
| 19 | +import dev.inmo.tgbotapi.extensions.utils.chatEventMessageOrNull |
| 20 | +import dev.inmo.tgbotapi.extensions.utils.groupContentMessageOrNull |
| 21 | +import dev.inmo.tgbotapi.extensions.utils.managedBotCreatedOrNull |
| 22 | +import dev.inmo.tgbotapi.extensions.utils.types.buttons.flatReplyKeyboard |
| 23 | +import dev.inmo.tgbotapi.extensions.utils.types.buttons.replyKeyboard |
| 24 | +import dev.inmo.tgbotapi.extensions.utils.types.buttons.requestManagedBotButton |
| 25 | +import dev.inmo.tgbotapi.types.Username |
| 26 | +import dev.inmo.tgbotapi.types.buttons.KeyboardButtonRequestManagedBot |
| 27 | +import dev.inmo.tgbotapi.types.buttons.PreparedKeyboardButtonId |
| 28 | +import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage |
| 29 | +import dev.inmo.tgbotapi.types.request.RequestId |
| 30 | +import dev.inmo.tgbotapi.types.toChatId |
| 31 | +import dev.inmo.tgbotapi.types.update.abstracts.Update |
| 32 | +import kotlinx.coroutines.CoroutineScope |
| 33 | +import kotlinx.coroutines.Dispatchers |
| 34 | + |
| 35 | +private var BehaviourContextData.update: Update? |
| 36 | + get() = get("update") as? Update |
| 37 | + set(value) = set("update", value) |
| 38 | + |
| 39 | +private var BehaviourContextData.commonMessage: CommonMessage<*>? |
| 40 | + get() = get("commonMessage") as? CommonMessage<*> |
| 41 | + set(value) = set("commonMessage", value) |
| 42 | + |
| 43 | +/** |
| 44 | + * This place can be the playground for your code. |
| 45 | + */ |
| 46 | +suspend fun main(vararg args: String) { |
| 47 | + val botToken = args.first() |
| 48 | + |
| 49 | + val isDebug = args.any { it == "debug" } |
| 50 | + val isTestServer = args.any { it == "testServer" } |
| 51 | + |
| 52 | + if (isDebug) { |
| 53 | + setDefaultKSLog( |
| 54 | + KSLog { level: LogLevel, tag: String?, message: Any, throwable: Throwable? -> |
| 55 | + println(defaultMessageFormatter(level, tag, message, throwable)) |
| 56 | + } |
| 57 | + ) |
| 58 | + } |
| 59 | + |
| 60 | + telegramBotWithBehaviourAndLongPolling( |
| 61 | + botToken, |
| 62 | + CoroutineScope(Dispatchers.IO), |
| 63 | + testServer = isTestServer, |
| 64 | + builder = { |
| 65 | + includeMiddlewares { |
| 66 | + addMiddleware { |
| 67 | + doOnRequestReturnResult { result, request, _ -> |
| 68 | + println("Result of $request:\n\n$result") |
| 69 | + null |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + }, |
| 74 | + subcontextInitialAction = buildSubcontextInitialAction { |
| 75 | + add { |
| 76 | + data.update = it |
| 77 | + } |
| 78 | + } |
| 79 | + ) { |
| 80 | + // start here!! |
| 81 | + val me = getMe() |
| 82 | + println(me) |
| 83 | + |
| 84 | + onCommand("start") { |
| 85 | + println(data.update) |
| 86 | + println(data.commonMessage) |
| 87 | + println(getChat(it.chat)) |
| 88 | + } |
| 89 | + |
| 90 | + onCommand("canManageBots") { |
| 91 | + val me = getMe() |
| 92 | + reply(it, if (me.canManageBots) "Yes" else "No") |
| 93 | + } |
| 94 | + |
| 95 | + val requestId = RequestId(0) |
| 96 | + onCommand("keyboard") { |
| 97 | + reply( |
| 98 | + it, |
| 99 | + "Keyboard", |
| 100 | + replyMarkup = flatReplyKeyboard( |
| 101 | + resizeKeyboard = true, |
| 102 | + oneTimeKeyboard = true, |
| 103 | + ) { |
| 104 | + requestManagedBotButton( |
| 105 | + "Add managed bot", |
| 106 | + KeyboardButtonRequestManagedBot( |
| 107 | + requestId = requestId, |
| 108 | + suggestedName = "SampleName", |
| 109 | + suggestedUsername = Username("@some_sample_bot") |
| 110 | + ) |
| 111 | + ) |
| 112 | + } |
| 113 | + ) |
| 114 | + } |
| 115 | + |
| 116 | + onManagedBotCreated { |
| 117 | + reply(it, "Managed bot created successfully: ${it.chatEvent.bot}") |
| 118 | + val token = getManagedBotToken( |
| 119 | + it.chatEvent.bot.id.toChatId() |
| 120 | + ) |
| 121 | + reply(it, "Token: $token") |
| 122 | + } |
| 123 | + |
| 124 | + onManagedBotUpdated { |
| 125 | + send(it.user, "Managed bot has been updated: ${it.bot}") |
| 126 | + val token = getManagedBotToken( |
| 127 | + it.bot.id.toChatId() |
| 128 | + ) |
| 129 | + send(it.user, "Token: $token") |
| 130 | + } |
| 131 | + |
| 132 | + onCommand("replaceToken") { |
| 133 | + val reply = it.replyTo ?.chatEventMessageOrNull() ?: return@onCommand |
| 134 | + val managedBotCreated = reply.chatEvent.managedBotCreatedOrNull() ?: return@onCommand |
| 135 | + |
| 136 | + reply(it, "Token in replace update: ${replaceManagedBotToken(managedBotCreated.bot.id.toChatId())}") |
| 137 | + } |
| 138 | + |
| 139 | + allUpdatesFlow.subscribeLoggingDropExceptions(this) { |
| 140 | + println(it) |
| 141 | + } |
| 142 | + }.second.join() |
| 143 | +} |
0 commit comments