mirror of
https://github.com/InsanusMokrassar/TelegramBotTutorial.git
synced 2024-12-22 14:17:12 +00:00
introduction plugin
This commit is contained in:
parent
85f59d8745
commit
0ecf6cbf59
@ -23,6 +23,8 @@ repositories {
|
|||||||
dependencies {
|
dependencies {
|
||||||
implementation libs.kotlin
|
implementation libs.kotlin
|
||||||
implementation libs.plagubot
|
implementation libs.plagubot
|
||||||
|
|
||||||
|
implementation project(":introduction")
|
||||||
}
|
}
|
||||||
|
|
||||||
application {
|
application {
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
{
|
{
|
||||||
"botToken": "1234567890:ABCDEFGHIJKLMNOP_qrstuvwxyz12345678",
|
"botToken": "1234567890:ABCDEFGHIJKLMNOP_qrstuvwxyz12345678",
|
||||||
"plugins": [
|
"plugins": [
|
||||||
"dev.inmo.plagubot.HelloPlugin"
|
"IntroductionPlugin"
|
||||||
],
|
],
|
||||||
"helloPlugin": {
|
"introduction": {
|
||||||
"print": "Hello World"
|
"onStartCommandMessage": "Hello World"
|
||||||
},
|
},
|
||||||
"database": {
|
"database": {
|
||||||
"url": "jdbc:sqlite:file:local.db?mode=memory&cache=shared"
|
"url": "jdbc:sqlite:file:local.db?mode=memory&cache=shared"
|
||||||
|
@ -2,11 +2,14 @@
|
|||||||
|
|
||||||
kotlin = "1.6.21"
|
kotlin = "1.6.21"
|
||||||
plagubot = "1.1.1"
|
plagubot = "1.1.1"
|
||||||
|
kslog = "0.3.1"
|
||||||
|
|
||||||
[libraries]
|
[libraries]
|
||||||
|
|
||||||
kotlin = { module = "org.jetbrains.kotlin:kotlin-stdlib", version.ref = "kotlin" }
|
kotlin = { module = "org.jetbrains.kotlin:kotlin-stdlib", version.ref = "kotlin" }
|
||||||
plagubot = { module = "dev.inmo:plagubot.bot", version.ref = "plagubot" }
|
plagubot = { module = "dev.inmo:plagubot.bot", version.ref = "plagubot" }
|
||||||
|
plagubot-plugin = { module = "dev.inmo:plagubot.plugin", version.ref = "plagubot" }
|
||||||
|
kslog = { module = "dev.inmo:kslog", version.ref = "kslog" }
|
||||||
|
|
||||||
# Libs for classpath
|
# Libs for classpath
|
||||||
kotlin-gradle-plugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" }
|
kotlin-gradle-plugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" }
|
||||||
|
30
introduction/build.gradle
Normal file
30
introduction/build.gradle
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
buildscript {
|
||||||
|
repositories {
|
||||||
|
mavenCentral()
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
classpath libs.kotlin.gradle.plugin
|
||||||
|
classpath libs.kotlin.serialization.plugin
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
plugins {
|
||||||
|
alias libs.plugins.kotlin.jvm
|
||||||
|
alias libs.plugins.kotlin.serialization
|
||||||
|
}
|
||||||
|
|
||||||
|
repositories {
|
||||||
|
mavenCentral()
|
||||||
|
mavenLocal()
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
implementation libs.kotlin
|
||||||
|
api libs.plagubot.plugin
|
||||||
|
api libs.kslog
|
||||||
|
}
|
||||||
|
|
||||||
|
application {
|
||||||
|
mainClassName = 'dev.inmo.plagubot.AppKt'
|
||||||
|
}
|
44
introduction/src/main/kotlin/IntroductionPlugin.kt
Normal file
44
introduction/src/main/kotlin/IntroductionPlugin.kt
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
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
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
class IntroductionPlugin : Plugin {
|
||||||
|
private val log = logger
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
private class Config(
|
||||||
|
val onStartCommandMessage: String
|
||||||
|
)
|
||||||
|
|
||||||
|
override fun Module.setupDI(database: Database, params: JsonObject) {
|
||||||
|
single { get<Json>().decodeFromJsonElement(Config.serializer(), params["introduction"] ?: return@single null) }
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1 +1,3 @@
|
|||||||
rootProject.name = "bot"
|
rootProject.name = "bot"
|
||||||
|
|
||||||
|
include ":introduction"
|
||||||
|
Loading…
Reference in New Issue
Block a user