diff --git a/.templates/tutorial_module/ModuleGenerator.json b/.templates/tutorial_module/ModuleGenerator.json new file mode 100644 index 0000000..8a715f2 --- /dev/null +++ b/.templates/tutorial_module/ModuleGenerator.json @@ -0,0 +1,5 @@ +{ + "module_name": "Sample", + "module_package": "{{.module_name.lowercase()}}", + "module_path": "{{.module_package.replace('.', '/')}}" +} diff --git a/.templates/tutorial_module/{{.module_name.lowercase()}}/build.gradle b/.templates/tutorial_module/{{.module_name.lowercase()}}/build.gradle new file mode 100644 index 0000000..df9d28f --- /dev/null +++ b/.templates/tutorial_module/{{.module_name.lowercase()}}/build.gradle @@ -0,0 +1,27 @@ +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() + maven { url "https://git.inmo.dev/api/packages/InsanusMokrassar/maven" } +} + +dependencies { + implementation libs.kotlin + api libs.plagubot.plugin + api libs.kslog +} diff --git a/.templates/tutorial_module/{{.module_name.lowercase()}}/src/main/kotlin/{{.module_name}}Plugin.kt b/.templates/tutorial_module/{{.module_name.lowercase()}}/src/main/kotlin/{{.module_name}}Plugin.kt new file mode 100644 index 0000000..df41d82 --- /dev/null +++ b/.templates/tutorial_module/{{.module_name.lowercase()}}/src/main/kotlin/{{.module_name}}Plugin.kt @@ -0,0 +1,60 @@ +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 + +/** + * This is template of plugin with preset [log]ger, [Config] and template configurations of [setupDI] and [setupBotPlugin]. + * Replace [pluginConfigSectionName] value with your one to customize configuration section name + */ +@Serializable +class {{.module_name}}Plugin : Plugin { + /** + * Default logger of [WelcomePlugin] got with [logger] + */ + private val log = logger + + /** + * 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 + */ + @Serializable + private class Config( + ) + + /** + * DI configuration of current plugin. Here we are decoding [Config] and put it into [Module] receiver + */ + override fun Module.setupDI(database: Database, params: JsonObject) { + single { get().decodeFromJsonElement(Config.serializer(), params[pluginConfigSectionName] ?: return@single null) } + } + + /** + * Final configuration of bot. Here we are getting [Config] from [koin] + */ + override suspend fun BehaviourContext.setupBotPlugin(koin: Koin) { + val config = koin.getOrNull() + + if (config == null) { + log.w("Plugin has been disabled due to absence of \"$pluginConfigSectionName\" field in config or some error during configuration loading") + return + } + } + + companion object { + private const val pluginConfigSectionName = "new" + } +} diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 5f6294d..d0ef027 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,10 +1,10 @@ [versions] -kotlin = "1.8.20" -plagubot = "5.1.0" +kotlin = "1.8.21" +plagubot = "5.1.2" kslog = "1.1.1" -plagubot-commands = "0.11.0" -tgbotapi-libraries = "0.11.0" +plagubot-commands = "0.11.2" +tgbotapi-libraries = "0.11.2" [libraries] diff --git a/runner/Dockerfile b/runner/Dockerfile new file mode 100644 index 0000000..1b23778 --- /dev/null +++ b/runner/Dockerfile @@ -0,0 +1,8 @@ +FROM adoptopenjdk/openjdk11 + +USER 1000 + +ENTRYPOINT ["/runner/bin/runner", "/runner/local.config.json"] + +ADD ./build/distributions/runner.tar / +ADD ./local.config.json /runner/ diff --git a/runner/build.gradle b/runner/build.gradle new file mode 100644 index 0000000..96a50a8 --- /dev/null +++ b/runner/build.gradle @@ -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 + id 'application' +} + +application.mainClassName = 'dev.inmo.plagubot.AppKt' + +repositories { + mavenCentral() + mavenLocal() + maven { url "https://git.inmo.dev/api/packages/InsanusMokrassar/maven" } +} + +dependencies { + implementation libs.kotlin + api libs.plagubot.plugin + api libs.kslog +} diff --git a/runner/config.json b/runner/config.json new file mode 100644 index 0000000..399428f --- /dev/null +++ b/runner/config.json @@ -0,0 +1,15 @@ +{ + "botToken": "1234567890:ABCDEFGHIJKLMNOP_qrstuvwxyz12345678", + "plugins": [ + "dev.inmo.plagubot.plugins.commands.CommandsPlugin", + + "IntroductionPlugin", + "WelcomePlugin" + ], + "introduction": { + "onStartCommandMessage": "Hello World" + }, + "database": { + "url": "jdbc:sqlite:file:/data/local.db" + } +} diff --git a/runner/deploy b/runner/deploy new file mode 100644 index 0000000..e904d82 --- /dev/null +++ b/runner/deploy @@ -0,0 +1,25 @@ +#!/bin/bash + +function send_notification() { + echo "$1" +} + +function assert_success() { + "${@}" + local status=${?} + if [ ${status} -ne 0 ]; then + send_notification "### Error ${status} at: ${BASH_LINENO[*]} ###" + exit ${status} + fi +} + +app=runner +version=0.0.0 +server="$USER" + +assert_success ../gradlew build +assert_success sudo docker build -t $app:"$version" . +assert_success sudo docker tag $app:"$version" $server/$app:$version +assert_success sudo docker tag $app:"$version" $server/$app:latest +assert_success sudo docker push $server/$app:$version +assert_success sudo docker push $server/$app:latest diff --git a/runner/docker-compose.yml b/runner/docker-compose.yml new file mode 100644 index 0000000..ca3eedb --- /dev/null +++ b/runner/docker-compose.yml @@ -0,0 +1,10 @@ +version: "3.4" + +services: + adminbot: + image: user/runner + container_name: runner + restart: unless-stopped + volumes: + - "./data/:/data/" + - "./config.json:/config.json:ro" diff --git a/runner/nonsudo_deploy b/runner/nonsudo_deploy new file mode 100644 index 0000000..7e7c51c --- /dev/null +++ b/runner/nonsudo_deploy @@ -0,0 +1,25 @@ +#!/bin/bash + +function send_notification() { + echo "$1" +} + +function assert_success() { + "${@}" + local status=${?} + if [ ${status} -ne 0 ]; then + send_notification "### Error ${status} at: ${BASH_LINENO[*]} ###" + exit ${status} + fi +} + +app=runner +version=0.0.0 +server="$USER" + +assert_success ../gradlew build +assert_success docker build -t $app:"$version" . +assert_success docker tag $app:"$version" $server/$app:$version +assert_success docker tag $app:"$version" $server/$app:latest +assert_success docker push $server/$app:$version +assert_success docker push $server/$app:latest diff --git a/settings.gradle b/settings.gradle index ecd58c9..a345734 100644 --- a/settings.gradle +++ b/settings.gradle @@ -2,3 +2,4 @@ rootProject.name = "tutorial" include ":introduction" include ":welcome" +include ":runner"