TelegramBotTutorial/introduction/src/main/kotlin/IntroductionPlugin.kt

66 lines
2.5 KiB
Kotlin
Raw Normal View History

2022-06-17 16:24:44 +00:00
import dev.inmo.kslog.common.logger
import dev.inmo.kslog.common.w
import dev.inmo.plagubot.Plugin
import dev.inmo.tgbotapi.extensions.api.send.reply
import dev.inmo.tgbotapi.extensions.api.send.sendMessage
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onMyChatMemberUpdated
import dev.inmo.tgbotapi.types.chat.PrivateChat
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.*
import org.jetbrains.exposed.sql.Database
import org.koin.core.Koin
import org.koin.core.module.Module
2022-06-18 13:51:31 +00:00
/**
* This plugin represents simple plugin which realize all necessary functionality of plugin you may need in context of simple plugins.
* In context of this plugin we will decode plugin configuration and setup reaction on `/start` command. In `introduction`
* section of config you should put field `onStartCommandMessage` which will be sent to use on `/start` command
*/
2022-06-17 16:24:44 +00:00
@Serializable
class IntroductionPlugin : Plugin {
2022-06-18 13:51:31 +00:00
/**
* Default logger of [IntroductionPlugin] got with [logger]
*/
2022-06-17 16:24:44 +00:00
private val log = logger
2022-06-18 13:51:31 +00:00
/**
* Configuration class for current plugin
*
* See realization of [setupDI] to get know how this class will be deserialized from global config
*
* See realization of [setupBotPlugin] to get know how to get access to this class
*/
2022-06-17 16:24:44 +00:00
@Serializable
private class Config(
val onStartCommandMessage: String
)
2022-06-18 13:51:31 +00:00
/**
* DI configuration of current plugin. Here we are decoding [Config] and put it into [Module] receiver
*/
2022-06-17 16:24:44 +00:00
override fun Module.setupDI(database: Database, params: JsonObject) {
single { get<Json>().decodeFromJsonElement(Config.serializer(), params["introduction"] ?: return@single null) }
}
2022-06-18 13:51:31 +00:00
/**
* Final configuration of bot. Here we are getting [Config] from [koin] and configure reaction on `/start` command
*/
2022-06-17 16:24:44 +00:00
override suspend fun BehaviourContext.setupBotPlugin(koin: Koin) {
val config = koin.getOrNull<Config>()
if (config == null) {
log.w("Plugin has been disabled due to absence of \"introduction\" field in config or some error during configuration loading")
return
}
onCommand("start", initialFilter = { it.chat is PrivateChat }) {
sendMessage(
it.chat,
config.onStartCommandMessage
)
}
}
}