Skip to content

Commit

Permalink
a lot of improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
InsanusMokrassar committed Sep 14, 2024
1 parent bb06ba1 commit 3b507a5
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 16 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
28 changes: 15 additions & 13 deletions bot/src/main/kotlin/dev/inmo/plagubot/PlaguBot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ 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
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
Expand All @@ -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)
}
}
}
Expand All @@ -67,7 +59,16 @@ data class PlaguBot(
single { this@PlaguBot.config.databaseConfig.database }
single { defaultJsonFormat }
single { this@PlaguBot }
single { bot }
single {
val config = get<Config>()
telegramBot(
token = config.botToken,
testServer = config.testServer,
apiUrl = config.botApiServer
) {
setupBotClient(this@single, json)
}
}
}

override fun Module.setupDI(database: Database, params: JsonObject) {
Expand Down Expand Up @@ -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()
Expand All @@ -141,6 +142,7 @@ data class PlaguBot(
lateinit var behaviourContext: BehaviourContext
val onStartContextsConflictResolver by lazy { koinApp.koin.getAllDistinct<OnStartContextsConflictResolver>() }
val onUpdateContextsConflictResolver by lazy { koinApp.koin.getAllDistinct<OnUpdateContextsConflictResolver>() }
val bot = koinApp.koin.get<TelegramBot>()
bot.buildBehaviourWithFSM(
scope = scope,
defaultExceptionsHandler = {
Expand Down
3 changes: 2 additions & 1 deletion bot/src/main/kotlin/dev/inmo/plagubot/config/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ data class Config(
val plugins: List<StartPlugin>,
@SerialName("database")
val databaseConfig: DatabaseConfig = DatabaseConfig(),
val botApiServer: String = telegramBotAPIDefaultUrl
val botApiServer: String = telegramBotAPIDefaultUrl,
val testServer: Boolean = false
) {
val botPlugins = plugins.filterIsInstance<Plugin>()
}
11 changes: 10 additions & 1 deletion plugin/src/main/kotlin/dev/inmo/plagubot/Plugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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**
Expand All @@ -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
Expand Down

0 comments on commit 3b507a5

Please sign in to comment.