From d772d275b3e6c09357ef9d733457358908be25af Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 14 Sep 2024 20:17:10 +0600 Subject: [PATCH 1/3] start 9.3.0 --- CHANGELOG.md | 2 ++ gradle.properties | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 48e54f5..7fcf776 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ # Changelog +## 9.3.0 + ## 9.2.0 * `Versions`: diff --git a/gradle.properties b/gradle.properties index de3e337..68d24b4 100644 --- a/gradle.properties +++ b/gradle.properties @@ -5,4 +5,4 @@ kotlin.js.generate.externals=true kotlin.incremental=true group=dev.inmo -version=9.2.0 +version=9.3.0 From bb06ba1ca7304d885e56b41a46f5a5cd9bc6f3d7 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 14 Sep 2024 20:17:38 +0600 Subject: [PATCH 2/3] Add opportunity to use config in setup bot client --- CHANGELOG.md | 2 ++ bot/src/main/kotlin/dev/inmo/plagubot/PlaguBot.kt | 9 ++++++++- plugin/src/main/kotlin/dev/inmo/plagubot/Plugin.kt | 1 + 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7fcf776..fdbad96 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## 9.3.0 +* Add opportunity to use config in setup bot client + ## 9.2.0 * `Versions`: diff --git a/bot/src/main/kotlin/dev/inmo/plagubot/PlaguBot.kt b/bot/src/main/kotlin/dev/inmo/plagubot/PlaguBot.kt index 53312f8..7616453 100644 --- a/bot/src/main/kotlin/dev/inmo/plagubot/PlaguBot.kt +++ b/bot/src/main/kotlin/dev/inmo/plagubot/PlaguBot.kt @@ -42,9 +42,16 @@ data class PlaguBot( token = config.botToken, apiUrl = config.botApiServer ) { - setupBotClient() + setupBotClient(json) } + override fun KtorRequestsExecutorBuilder.setupBotClient(params: JsonObject) { + config.botPlugins.forEach { + with(it) { + setupBotClient(params) + } + } + } override fun KtorRequestsExecutorBuilder.setupBotClient() { config.botPlugins.forEach { with(it) { diff --git a/plugin/src/main/kotlin/dev/inmo/plagubot/Plugin.kt b/plugin/src/main/kotlin/dev/inmo/plagubot/Plugin.kt index bc5af6c..99f0ab0 100644 --- a/plugin/src/main/kotlin/dev/inmo/plagubot/Plugin.kt +++ b/plugin/src/main/kotlin/dev/inmo/plagubot/Plugin.kt @@ -21,6 +21,7 @@ import org.koin.core.module.Module @Serializable(PluginSerializer::class) interface Plugin : StartPlugin { fun KtorRequestsExecutorBuilder.setupBotClient() {} + fun KtorRequestsExecutorBuilder.setupBotClient(params: JsonObject) = setupBotClient() /** * This method will be called when this plugin should configure di module based on the incoming params From 3b507a56681f40da84a97beeeab5cf10f183557f Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 14 Sep 2024 21:06:06 +0600 Subject: [PATCH 3/3] a lot of improvements --- CHANGELOG.md | 6 +++- .../main/kotlin/dev/inmo/plagubot/PlaguBot.kt | 28 ++++++++++--------- .../kotlin/dev/inmo/plagubot/config/Config.kt | 3 +- .../main/kotlin/dev/inmo/plagubot/Plugin.kt | 11 +++++++- 4 files changed, 32 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fdbad96..f3fbbe7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,11 @@ ## 9.3.0 -* Add opportunity to use config in setup bot client +* `Bot`: + * Now bot is not built-in into `PlaguBot` and setted up as all other `Koin` dependencies + * Now it is possible to use `testServer` parameter for bots out of the box +* `Plugin`: + * New method `setupBotClient` with arguments to let plugin setup bot more freely ## 9.2.0 diff --git a/bot/src/main/kotlin/dev/inmo/plagubot/PlaguBot.kt b/bot/src/main/kotlin/dev/inmo/plagubot/PlaguBot.kt index 7616453..1258264 100644 --- a/bot/src/main/kotlin/dev/inmo/plagubot/PlaguBot.kt +++ b/bot/src/main/kotlin/dev/inmo/plagubot/PlaguBot.kt @@ -8,6 +8,7 @@ import dev.inmo.micro_utils.fsm.common.StatesManager import dev.inmo.micro_utils.fsm.common.managers.* import dev.inmo.micro_utils.koin.getAllDistinct import dev.inmo.plagubot.config.* +import dev.inmo.tgbotapi.bot.TelegramBot import dev.inmo.tgbotapi.bot.ktor.KtorRequestsExecutorBuilder import dev.inmo.tgbotapi.bot.ktor.telegramBot import dev.inmo.tgbotapi.extensions.api.webhook.deleteWebhook @@ -15,7 +16,6 @@ import dev.inmo.tgbotapi.extensions.behaviour_builder.* import dev.inmo.tgbotapi.extensions.utils.updates.retrieving.startGettingOfUpdatesByLongPolling import kotlinx.coroutines.* import kotlinx.serialization.Serializable -import kotlinx.serialization.Transient import kotlinx.serialization.json.JsonObject import org.jetbrains.exposed.sql.Database import org.koin.core.Koin @@ -37,18 +37,10 @@ data class PlaguBot( private val json: JsonObject, private val config: Config ) : Plugin { - @Transient - private val bot = telegramBot( - token = config.botToken, - apiUrl = config.botApiServer - ) { - setupBotClient(json) - } - - override fun KtorRequestsExecutorBuilder.setupBotClient(params: JsonObject) { + override fun KtorRequestsExecutorBuilder.setupBotClient(scope: Scope, params: JsonObject) { config.botPlugins.forEach { with(it) { - setupBotClient(params) + setupBotClient(scope, params) } } } @@ -67,7 +59,16 @@ data class PlaguBot( single { this@PlaguBot.config.databaseConfig.database } single { defaultJsonFormat } single { this@PlaguBot } - single { bot } + single { + val config = get() + telegramBot( + token = config.botToken, + testServer = config.testServer, + apiUrl = config.botApiServer + ) { + setupBotClient(this@single, json) + } + } } override fun Module.setupDI(database: Database, params: JsonObject) { @@ -124,7 +125,7 @@ data class PlaguBot( * This method will create an [Job] which will be the main [Job] of ran instance */ suspend fun start( - scope: CoroutineScope = CoroutineScope(Dispatchers.IO) + scope: CoroutineScope = CoroutineScope(Dispatchers.Default) ): Job { logger.i("Start initialization") val koinApp = KoinApplication.init() @@ -141,6 +142,7 @@ data class PlaguBot( lateinit var behaviourContext: BehaviourContext val onStartContextsConflictResolver by lazy { koinApp.koin.getAllDistinct() } val onUpdateContextsConflictResolver by lazy { koinApp.koin.getAllDistinct() } + val bot = koinApp.koin.get() bot.buildBehaviourWithFSM( scope = scope, defaultExceptionsHandler = { diff --git a/bot/src/main/kotlin/dev/inmo/plagubot/config/Config.kt b/bot/src/main/kotlin/dev/inmo/plagubot/config/Config.kt index a38ffd2..2344e20 100644 --- a/bot/src/main/kotlin/dev/inmo/plagubot/config/Config.kt +++ b/bot/src/main/kotlin/dev/inmo/plagubot/config/Config.kt @@ -14,7 +14,8 @@ data class Config( val plugins: List, @SerialName("database") val databaseConfig: DatabaseConfig = DatabaseConfig(), - val botApiServer: String = telegramBotAPIDefaultUrl + val botApiServer: String = telegramBotAPIDefaultUrl, + val testServer: Boolean = false ) { val botPlugins = plugins.filterIsInstance() } diff --git a/plugin/src/main/kotlin/dev/inmo/plagubot/Plugin.kt b/plugin/src/main/kotlin/dev/inmo/plagubot/Plugin.kt index 99f0ab0..621e151 100644 --- a/plugin/src/main/kotlin/dev/inmo/plagubot/Plugin.kt +++ b/plugin/src/main/kotlin/dev/inmo/plagubot/Plugin.kt @@ -10,6 +10,7 @@ import kotlinx.serialization.json.JsonObject import org.jetbrains.exposed.sql.Database import org.koin.core.Koin import org.koin.core.module.Module +import org.koin.core.scope.Scope /** * **ANY REALIZATION OF [Plugin] MUST HAVE CONSTRUCTOR WITH ABSENCE OF INCOMING PARAMETERS** @@ -20,8 +21,16 @@ import org.koin.core.module.Module */ @Serializable(PluginSerializer::class) interface Plugin : StartPlugin { + @Deprecated("Deprecated in favor to setupBotClient with arguments") fun KtorRequestsExecutorBuilder.setupBotClient() {} - fun KtorRequestsExecutorBuilder.setupBotClient(params: JsonObject) = setupBotClient() + + /** + * Will be called on stage of bot setup + * + * @param scope The scope of [org.koin.core.module.Module.single] of bot definition + * @param params Params (in fact, the whole bot config) + */ + fun KtorRequestsExecutorBuilder.setupBotClient(scope: Scope, params: JsonObject) = setupBotClient() /** * This method will be called when this plugin should configure di module based on the incoming params