Merge pull request #104 from InsanusMokrassar/9.3.0

9.3.0
This commit is contained in:
InsanusMokrassar 2024-09-14 21:21:52 +06:00 committed by GitHub
commit 26b34a470b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 40 additions and 12 deletions

View File

@ -1,5 +1,13 @@
# Changelog # Changelog
## 9.3.0
* `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 ## 9.2.0
* `Versions`: * `Versions`:

View File

@ -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.fsm.common.managers.*
import dev.inmo.micro_utils.koin.getAllDistinct import dev.inmo.micro_utils.koin.getAllDistinct
import dev.inmo.plagubot.config.* import dev.inmo.plagubot.config.*
import dev.inmo.tgbotapi.bot.TelegramBot
import dev.inmo.tgbotapi.bot.ktor.KtorRequestsExecutorBuilder import dev.inmo.tgbotapi.bot.ktor.KtorRequestsExecutorBuilder
import dev.inmo.tgbotapi.bot.ktor.telegramBot import dev.inmo.tgbotapi.bot.ktor.telegramBot
import dev.inmo.tgbotapi.extensions.api.webhook.deleteWebhook 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 dev.inmo.tgbotapi.extensions.utils.updates.retrieving.startGettingOfUpdatesByLongPolling
import kotlinx.coroutines.* import kotlinx.coroutines.*
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient
import kotlinx.serialization.json.JsonObject import kotlinx.serialization.json.JsonObject
import org.jetbrains.exposed.sql.Database import org.jetbrains.exposed.sql.Database
import org.koin.core.Koin import org.koin.core.Koin
@ -37,14 +37,13 @@ data class PlaguBot(
private val json: JsonObject, private val json: JsonObject,
private val config: Config private val config: Config
) : Plugin { ) : Plugin {
@Transient override fun KtorRequestsExecutorBuilder.setupBotClient(scope: Scope, params: JsonObject) {
private val bot = telegramBot( config.botPlugins.forEach {
token = config.botToken, with(it) {
apiUrl = config.botApiServer setupBotClient(scope, params)
) { }
setupBotClient() }
} }
override fun KtorRequestsExecutorBuilder.setupBotClient() { override fun KtorRequestsExecutorBuilder.setupBotClient() {
config.botPlugins.forEach { config.botPlugins.forEach {
with(it) { with(it) {
@ -60,7 +59,16 @@ data class PlaguBot(
single { this@PlaguBot.config.databaseConfig.database } single { this@PlaguBot.config.databaseConfig.database }
single { defaultJsonFormat } single { defaultJsonFormat }
single { this@PlaguBot } 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) { override fun Module.setupDI(database: Database, params: JsonObject) {
@ -117,7 +125,7 @@ data class PlaguBot(
* This method will create an [Job] which will be the main [Job] of ran instance * This method will create an [Job] which will be the main [Job] of ran instance
*/ */
suspend fun start( suspend fun start(
scope: CoroutineScope = CoroutineScope(Dispatchers.IO) scope: CoroutineScope = CoroutineScope(Dispatchers.Default)
): Job { ): Job {
logger.i("Start initialization") logger.i("Start initialization")
val koinApp = KoinApplication.init() val koinApp = KoinApplication.init()
@ -134,6 +142,7 @@ data class PlaguBot(
lateinit var behaviourContext: BehaviourContext lateinit var behaviourContext: BehaviourContext
val onStartContextsConflictResolver by lazy { koinApp.koin.getAllDistinct<OnStartContextsConflictResolver>() } val onStartContextsConflictResolver by lazy { koinApp.koin.getAllDistinct<OnStartContextsConflictResolver>() }
val onUpdateContextsConflictResolver by lazy { koinApp.koin.getAllDistinct<OnUpdateContextsConflictResolver>() } val onUpdateContextsConflictResolver by lazy { koinApp.koin.getAllDistinct<OnUpdateContextsConflictResolver>() }
val bot = koinApp.koin.get<TelegramBot>()
bot.buildBehaviourWithFSM( bot.buildBehaviourWithFSM(
scope = scope, scope = scope,
defaultExceptionsHandler = { defaultExceptionsHandler = {

View File

@ -14,7 +14,8 @@ data class Config(
val plugins: List<StartPlugin>, val plugins: List<StartPlugin>,
@SerialName("database") @SerialName("database")
val databaseConfig: DatabaseConfig = DatabaseConfig(), val databaseConfig: DatabaseConfig = DatabaseConfig(),
val botApiServer: String = telegramBotAPIDefaultUrl val botApiServer: String = telegramBotAPIDefaultUrl,
val testServer: Boolean = false
) { ) {
val botPlugins = plugins.filterIsInstance<Plugin>() val botPlugins = plugins.filterIsInstance<Plugin>()
} }

View File

@ -5,4 +5,4 @@ kotlin.js.generate.externals=true
kotlin.incremental=true kotlin.incremental=true
group=dev.inmo group=dev.inmo
version=9.2.0 version=9.3.0

View File

@ -10,6 +10,7 @@ import kotlinx.serialization.json.JsonObject
import org.jetbrains.exposed.sql.Database import org.jetbrains.exposed.sql.Database
import org.koin.core.Koin import org.koin.core.Koin
import org.koin.core.module.Module import org.koin.core.module.Module
import org.koin.core.scope.Scope
/** /**
* **ANY REALIZATION OF [Plugin] MUST HAVE CONSTRUCTOR WITH ABSENCE OF INCOMING PARAMETERS** * **ANY REALIZATION OF [Plugin] MUST HAVE CONSTRUCTOR WITH ABSENCE OF INCOMING PARAMETERS**
@ -20,8 +21,17 @@ import org.koin.core.module.Module
*/ */
@Serializable(PluginSerializer::class) @Serializable(PluginSerializer::class)
interface Plugin : StartPlugin { interface Plugin : StartPlugin {
@Deprecated("Deprecated in favor to setupBotClient with arguments")
fun KtorRequestsExecutorBuilder.setupBotClient() {} fun KtorRequestsExecutorBuilder.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 * This method will be called when this plugin should configure di module based on the incoming params
*/ */