PlaguBot/bot/src/main/kotlin/dev/inmo/plagubot/HelloPlugin.kt

66 lines
2.4 KiB
Kotlin
Raw Normal View History

2020-11-08 13:23:01 +00:00
package dev.inmo.plagubot
2022-06-11 13:19:19 +00:00
import dev.inmo.kslog.common.*
2022-08-13 14:30:10 +00:00
import dev.inmo.micro_utils.fsm.common.State
import dev.inmo.plagubot.HelloPlugin.setupBotPlugin
2022-05-16 17:58:16 +00:00
import dev.inmo.tgbotapi.extensions.api.bot.getMe
import dev.inmo.tgbotapi.extensions.api.send.reply
2022-08-18 06:42:34 +00:00
import dev.inmo.tgbotapi.extensions.api.send.sendMessage
import dev.inmo.tgbotapi.extensions.behaviour_builder.*
import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitText
import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitTextMessage
2022-05-16 17:58:16 +00:00
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand
2022-08-13 14:30:10 +00:00
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onUnhandledCommand
2022-08-18 06:42:34 +00:00
import dev.inmo.tgbotapi.types.ChatId
import kotlinx.coroutines.flow.first
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
2022-05-16 14:07:57 +00:00
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonObject
2020-11-11 17:05:21 +00:00
import org.jetbrains.exposed.sql.Database
2022-05-16 14:07:57 +00:00
import org.koin.core.Koin
2022-05-16 17:58:16 +00:00
import org.koin.core.module.Module
2020-11-11 17:05:21 +00:00
2020-11-12 05:52:09 +00:00
@Serializable
@SerialName("Hello")
object HelloPlugin : Plugin {
2022-05-16 14:07:57 +00:00
@Serializable
data class HelloPluginConfig(
val print: String
)
2022-05-16 17:58:16 +00:00
override fun Module.setupDI(database: Database, params: JsonObject) {
single {
get<Json>().decodeFromJsonElement(HelloPluginConfig.serializer(), params["helloPlugin"] ?: return@single null)
2022-08-18 06:42:34 +00:00
2022-05-16 14:07:57 +00:00
}
2022-05-16 17:58:16 +00:00
}
2022-05-16 14:07:57 +00:00
2022-08-18 06:42:34 +00:00
private sealed interface InternalFSMState : State {
override val context: ChatId
data class DidntSaidHello(override val context: ChatId) : InternalFSMState
data class SaidHelloOnce(override val context: ChatId) : InternalFSMState
}
2022-08-18 06:27:11 +00:00
override suspend fun BehaviourContextWithFSM<State>.setupBotPlugin(koin: Koin) {
val toPrint = koin.getOrNull<HelloPluginConfig>() ?.print ?: "Hello :)"
logger.d { toPrint }
2022-06-11 13:19:19 +00:00
logger.dS { getMe().toString() }
2022-05-16 17:58:16 +00:00
onCommand("hello_world") {
2022-08-18 06:42:34 +00:00
startChain(InternalFSMState.DidntSaidHello(it.chat.id))
}
strictlyOn { state: InternalFSMState.DidntSaidHello ->
sendMessage(state.context, toPrint)
InternalFSMState.SaidHelloOnce(state.context)
}
strictlyOn { state: InternalFSMState.SaidHelloOnce ->
val message = waitTextMessage().first()
reply(message, "Sorry, I can answer only this: $toPrint")
InternalFSMState.SaidHelloOnce(state.context)
2022-05-16 17:58:16 +00:00
}
2020-11-12 05:52:09 +00:00
}
2020-11-11 17:05:21 +00:00
}