diff --git a/telegram-bot/src/main/kotlin/eu/vendeli/tgbot/TelegramBot.kt b/telegram-bot/src/main/kotlin/eu/vendeli/tgbot/TelegramBot.kt index 99084c1f10..759946c81a 100644 --- a/telegram-bot/src/main/kotlin/eu/vendeli/tgbot/TelegramBot.kt +++ b/telegram-bot/src/main/kotlin/eu/vendeli/tgbot/TelegramBot.kt @@ -15,6 +15,8 @@ import eu.vendeli.tgbot.types.media.File import eu.vendeli.tgbot.utils.BotConfigurator import eu.vendeli.tgbot.utils.ManualHandlingBlock import eu.vendeli.tgbot.utils.RESPONSE_UPDATES_LIST_TYPEREF +import eu.vendeli.tgbot.utils.asClass +import eu.vendeli.tgbot.utils.getActions import eu.vendeli.tgbot.utils.getConfiguredHttpClient import eu.vendeli.tgbot.utils.getConfiguredMapper import eu.vendeli.tgbot.utils.level @@ -41,8 +43,10 @@ class TelegramBot( /** * Constructor to build through configuration loader. */ - constructor(configLoader: ConfigLoader = EnvConfigLoaderImpl) : - this(configLoader.token, configLoader.commandsPackage) { + constructor(configLoader: ConfigLoader = EnvConfigLoaderImpl) : this( + configLoader.token, + configLoader.commandsPackage, + ) { config.apply(configLoader.load()) } @@ -74,9 +78,8 @@ class TelegramBot( */ val update by lazy { val postFix = commandsPackage?.let { "_$it".replace(".", "_") } ?: "" - val codegenActions = runCatching { - Class.forName("eu.vendeli.tgbot.ActionsData${postFix}Kt") - }.getOrNull()?.let { it.getMethod("get\$ACTIONS$postFix").invoke(null) as? List<*> } + val codegenActions = "eu.vendeli.tgbot.ActionsDataKt".asClass().getActions() + ?: "eu.vendeli.tgbot.ActionsData${postFix}Kt".asClass().getActions(postFix) if (codegenActions != null) CodegenUpdateHandler( codegenActions, diff --git a/telegram-bot/src/main/kotlin/eu/vendeli/tgbot/core/CodegenUpdateHandler.kt b/telegram-bot/src/main/kotlin/eu/vendeli/tgbot/core/CodegenUpdateHandler.kt index f80626fdb9..37d999fc59 100644 --- a/telegram-bot/src/main/kotlin/eu/vendeli/tgbot/core/CodegenUpdateHandler.kt +++ b/telegram-bot/src/main/kotlin/eu/vendeli/tgbot/core/CodegenUpdateHandler.kt @@ -23,6 +23,7 @@ class CodegenUpdateHandler( private val unprocessedHandler = actions[4] as InvocationLambda? override suspend fun handle(update: Update): Unit = update.processUpdate().run { + logger.debug { "Handling update: $update" } // check general user limits if (checkIsLimited(bot.config.rateLimiter.limits, userOrNull?.id)) return@run @@ -49,6 +50,8 @@ class CodegenUpdateHandler( actionId = it.key.pattern }?.value + logger.debug { "Result of finding action - ${invocation?.second}" } + // if we found any action > check for its limits if (invocation != null && checkIsLimited(invocation.second.rateLimits, userOrNull?.id, actionId)) return@run @@ -82,7 +85,7 @@ class CodegenUpdateHandler( ) { "Method ${second.qualifier} > ${second.function} invocation error at handling update: $pUpdate" } caughtExceptions.send((it.cause ?: it) to pUpdate.update) }.onSuccess { - logger.info { "Handled update#${pUpdate.updateId} to method ${second.function}" } + logger.info { "Handled update#${pUpdate.updateId} to method ${second.qualifier + "::" + second.function}" } } } diff --git a/telegram-bot/src/main/kotlin/eu/vendeli/tgbot/utils/BotUtils.kt b/telegram-bot/src/main/kotlin/eu/vendeli/tgbot/utils/BotUtils.kt index 811ba33624..52cf060557 100644 --- a/telegram-bot/src/main/kotlin/eu/vendeli/tgbot/utils/BotUtils.kt +++ b/telegram-bot/src/main/kotlin/eu/vendeli/tgbot/utils/BotUtils.kt @@ -136,4 +136,11 @@ internal suspend inline fun asyncAction(crossinline block: suspend () -> T): async { block() } } +@Suppress("NOTHING_TO_INLINE") +internal inline fun String.asClass(): Class<*>? = kotlin.runCatching { Class.forName(this) }.getOrNull() + +@Suppress("NOTHING_TO_INLINE") +internal inline fun Class<*>?.getActions(postFix: String? = null) = + this?.getMethod("get\$ACTIONS".also { if (postFix != null) it + postFix })?.invoke(null) as? List<*> + fun InputListener.setChain(user: User, firstLink: T) = set(user, firstLink::class.qualifiedName!!)