diff --git a/src/main/kotlin/com/github/djaler/evilbot/Application.kt b/src/main/kotlin/com/github/djaler/evilbot/Application.kt index d662c726..78f7bd42 100644 --- a/src/main/kotlin/com/github/djaler/evilbot/Application.kt +++ b/src/main/kotlin/com/github/djaler/evilbot/Application.kt @@ -1,6 +1,11 @@ package com.github.djaler.evilbot -import com.github.djaler.evilbot.config.* +import com.github.djaler.evilbot.config.BotProperties +import com.github.djaler.evilbot.config.CacheProperties +import com.github.djaler.evilbot.config.TelegramProperties +import com.github.djaler.evilbot.config.fixer.FixerApiProperties +import com.github.djaler.evilbot.config.locationiq.LocationiqApiProperties +import com.github.djaler.evilbot.config.vkcloud.VKCloudApiProperties import org.springframework.boot.autoconfigure.SpringBootApplication import org.springframework.boot.context.properties.EnableConfigurationProperties import org.springframework.boot.runApplication diff --git a/src/main/kotlin/com/github/djaler/evilbot/clients/FixerClient.kt b/src/main/kotlin/com/github/djaler/evilbot/clients/FixerClient.kt index 533d3ad4..7bb257c7 100644 --- a/src/main/kotlin/com/github/djaler/evilbot/clients/FixerClient.kt +++ b/src/main/kotlin/com/github/djaler/evilbot/clients/FixerClient.kt @@ -1,18 +1,21 @@ package com.github.djaler.evilbot.clients import com.github.djaler.evilbot.components.RecordBreadcrumb -import com.github.djaler.evilbot.config.FixerApiProperties +import com.github.djaler.evilbot.config.fixer.FixerApiCondition +import com.github.djaler.evilbot.config.fixer.FixerApiProperties import com.github.djaler.evilbot.utils.cached import com.github.djaler.evilbot.utils.getCacheOrThrow import io.ktor.client.* import io.ktor.client.call.* import io.ktor.client.request.* import org.springframework.cache.CacheManager +import org.springframework.context.annotation.Conditional import org.springframework.stereotype.Component import java.math.BigDecimal import java.time.LocalDate @Component +@Conditional(FixerApiCondition::class) @RecordBreadcrumb class FixerClient( private val httpClient: HttpClient, diff --git a/src/main/kotlin/com/github/djaler/evilbot/clients/LocationiqClient.kt b/src/main/kotlin/com/github/djaler/evilbot/clients/LocationiqClient.kt index 1ddad997..8f7610c4 100644 --- a/src/main/kotlin/com/github/djaler/evilbot/clients/LocationiqClient.kt +++ b/src/main/kotlin/com/github/djaler/evilbot/clients/LocationiqClient.kt @@ -3,7 +3,8 @@ package com.github.djaler.evilbot.clients import com.fasterxml.jackson.databind.PropertyNamingStrategy import com.fasterxml.jackson.databind.annotation.JsonNaming import com.github.djaler.evilbot.components.RecordBreadcrumb -import com.github.djaler.evilbot.config.LocationiqApiProperties +import com.github.djaler.evilbot.config.locationiq.LocationiqApiCondition +import com.github.djaler.evilbot.config.locationiq.LocationiqApiProperties import com.github.djaler.evilbot.utils.cached import com.github.djaler.evilbot.utils.getCacheOrThrow import io.github.resilience4j.kotlin.ratelimiter.RateLimiterConfig @@ -13,11 +14,13 @@ import io.ktor.client.* import io.ktor.client.call.* import io.ktor.client.request.* import org.springframework.cache.CacheManager +import org.springframework.context.annotation.Conditional import org.springframework.stereotype.Component import java.io.Serializable import java.time.Duration @Component +@Conditional(LocationiqApiCondition::class) @RecordBreadcrumb class LocationiqClient( private val httpClient: HttpClient, diff --git a/src/main/kotlin/com/github/djaler/evilbot/clients/VoiceClient.kt b/src/main/kotlin/com/github/djaler/evilbot/clients/VoiceClient.kt index 4aaae161..2359477f 100644 --- a/src/main/kotlin/com/github/djaler/evilbot/clients/VoiceClient.kt +++ b/src/main/kotlin/com/github/djaler/evilbot/clients/VoiceClient.kt @@ -2,16 +2,19 @@ package com.github.djaler.evilbot.clients import com.fasterxml.jackson.annotation.JsonAlias import com.github.djaler.evilbot.components.RecordBreadcrumb -import com.github.djaler.evilbot.config.VKCloudApiProperties +import com.github.djaler.evilbot.config.vkcloud.VKCloudApiCondition +import com.github.djaler.evilbot.config.vkcloud.VKCloudApiProperties import io.ktor.client.* import io.ktor.client.call.* import io.ktor.client.plugins.* import io.ktor.client.request.* import io.ktor.http.* import io.ktor.http.content.* +import org.springframework.context.annotation.Conditional import org.springframework.stereotype.Component @Component +@Conditional(VKCloudApiCondition::class) @RecordBreadcrumb class VoiceClient( private val httpClient: HttpClient, diff --git a/src/main/kotlin/com/github/djaler/evilbot/config/fixer/FixerApiCondition.kt b/src/main/kotlin/com/github/djaler/evilbot/config/fixer/FixerApiCondition.kt new file mode 100644 index 00000000..74e65efc --- /dev/null +++ b/src/main/kotlin/com/github/djaler/evilbot/config/fixer/FixerApiCondition.kt @@ -0,0 +1,11 @@ +package com.github.djaler.evilbot.config.fixer + +import org.springframework.context.annotation.Condition +import org.springframework.context.annotation.ConditionContext +import org.springframework.core.type.AnnotatedTypeMetadata + +class FixerApiCondition : Condition { + override fun matches(context: ConditionContext, metadata: AnnotatedTypeMetadata): Boolean { + return context.environment.getProperty("fixer.api.key", "").isNotBlank() + } +} diff --git a/src/main/kotlin/com/github/djaler/evilbot/config/FixerApiProperties.kt b/src/main/kotlin/com/github/djaler/evilbot/config/fixer/FixerApiProperties.kt similarity index 61% rename from src/main/kotlin/com/github/djaler/evilbot/config/FixerApiProperties.kt rename to src/main/kotlin/com/github/djaler/evilbot/config/fixer/FixerApiProperties.kt index 83c1931f..8cef9375 100644 --- a/src/main/kotlin/com/github/djaler/evilbot/config/FixerApiProperties.kt +++ b/src/main/kotlin/com/github/djaler/evilbot/config/fixer/FixerApiProperties.kt @@ -1,15 +1,11 @@ -package com.github.djaler.evilbot.config +package com.github.djaler.evilbot.config.fixer import org.springframework.boot.context.properties.ConfigurationProperties import org.springframework.boot.context.properties.ConstructorBinding -import org.springframework.validation.annotation.Validated -import javax.validation.constraints.NotBlank @ConfigurationProperties(prefix = "fixer.api") @ConstructorBinding -@Validated data class FixerApiProperties( - @field:NotBlank - val key: String + val key: String = "" ) diff --git a/src/main/kotlin/com/github/djaler/evilbot/config/locationiq/LocationiqApiCondition.kt b/src/main/kotlin/com/github/djaler/evilbot/config/locationiq/LocationiqApiCondition.kt new file mode 100644 index 00000000..cf93d620 --- /dev/null +++ b/src/main/kotlin/com/github/djaler/evilbot/config/locationiq/LocationiqApiCondition.kt @@ -0,0 +1,11 @@ +package com.github.djaler.evilbot.config.locationiq + +import org.springframework.context.annotation.Condition +import org.springframework.context.annotation.ConditionContext +import org.springframework.core.type.AnnotatedTypeMetadata + +class LocationiqApiCondition : Condition { + override fun matches(context: ConditionContext, metadata: AnnotatedTypeMetadata): Boolean { + return context.environment.getProperty("locationiq.api.key", "").isNotBlank() + } +} diff --git a/src/main/kotlin/com/github/djaler/evilbot/config/LocationiqApiProperties.kt b/src/main/kotlin/com/github/djaler/evilbot/config/locationiq/LocationiqApiProperties.kt similarity index 62% rename from src/main/kotlin/com/github/djaler/evilbot/config/LocationiqApiProperties.kt rename to src/main/kotlin/com/github/djaler/evilbot/config/locationiq/LocationiqApiProperties.kt index dfbc4695..01b32aab 100644 --- a/src/main/kotlin/com/github/djaler/evilbot/config/LocationiqApiProperties.kt +++ b/src/main/kotlin/com/github/djaler/evilbot/config/locationiq/LocationiqApiProperties.kt @@ -1,15 +1,11 @@ -package com.github.djaler.evilbot.config +package com.github.djaler.evilbot.config.locationiq import org.springframework.boot.context.properties.ConfigurationProperties import org.springframework.boot.context.properties.ConstructorBinding -import org.springframework.validation.annotation.Validated -import javax.validation.constraints.NotBlank @ConfigurationProperties(prefix = "locationiq.api") @ConstructorBinding -@Validated data class LocationiqApiProperties( - @field:NotBlank - val key: String + val key: String = "" ) diff --git a/src/main/kotlin/com/github/djaler/evilbot/config/vkcloud/VKCloudApiCondition.kt b/src/main/kotlin/com/github/djaler/evilbot/config/vkcloud/VKCloudApiCondition.kt new file mode 100644 index 00000000..a723ba23 --- /dev/null +++ b/src/main/kotlin/com/github/djaler/evilbot/config/vkcloud/VKCloudApiCondition.kt @@ -0,0 +1,11 @@ +package com.github.djaler.evilbot.config.vkcloud + +import org.springframework.context.annotation.Condition +import org.springframework.context.annotation.ConditionContext +import org.springframework.core.type.AnnotatedTypeMetadata + +class VKCloudApiCondition : Condition { + override fun matches(context: ConditionContext, metadata: AnnotatedTypeMetadata): Boolean { + return context.environment.getProperty("vk.api.key", "").isNotBlank() + } +} diff --git a/src/main/kotlin/com/github/djaler/evilbot/config/VKCloudApiProperties.kt b/src/main/kotlin/com/github/djaler/evilbot/config/vkcloud/VKCloudApiProperties.kt similarity index 60% rename from src/main/kotlin/com/github/djaler/evilbot/config/VKCloudApiProperties.kt rename to src/main/kotlin/com/github/djaler/evilbot/config/vkcloud/VKCloudApiProperties.kt index 47bf2494..afd5e958 100644 --- a/src/main/kotlin/com/github/djaler/evilbot/config/VKCloudApiProperties.kt +++ b/src/main/kotlin/com/github/djaler/evilbot/config/vkcloud/VKCloudApiProperties.kt @@ -1,14 +1,10 @@ -package com.github.djaler.evilbot.config +package com.github.djaler.evilbot.config.vkcloud import org.springframework.boot.context.properties.ConfigurationProperties import org.springframework.boot.context.properties.ConstructorBinding -import org.springframework.validation.annotation.Validated -import javax.validation.constraints.NotBlank @ConfigurationProperties(prefix = "vk.api") @ConstructorBinding -@Validated data class VKCloudApiProperties( - @field:NotBlank - val key: String -) \ No newline at end of file + val key: String = "" +) diff --git a/src/main/kotlin/com/github/djaler/evilbot/handlers/VoiceMessageHandler.kt b/src/main/kotlin/com/github/djaler/evilbot/handlers/VoiceMessageHandler.kt index ea13f018..29820f41 100644 --- a/src/main/kotlin/com/github/djaler/evilbot/handlers/VoiceMessageHandler.kt +++ b/src/main/kotlin/com/github/djaler/evilbot/handlers/VoiceMessageHandler.kt @@ -1,6 +1,7 @@ package com.github.djaler.evilbot.handlers import com.github.djaler.evilbot.clients.VoiceClient +import com.github.djaler.evilbot.config.vkcloud.VKCloudApiCondition import com.github.djaler.evilbot.handlers.base.MessageHandler import dev.inmo.tgbotapi.bot.RequestsExecutor import dev.inmo.tgbotapi.extensions.api.files.downloadFile @@ -9,10 +10,12 @@ import dev.inmo.tgbotapi.extensions.api.send.withTypingAction import dev.inmo.tgbotapi.extensions.utils.asContentMessage import dev.inmo.tgbotapi.extensions.utils.asVoiceContent import dev.inmo.tgbotapi.types.message.abstracts.Message +import org.springframework.context.annotation.Conditional import org.springframework.stereotype.Component @Component +@Conditional(VKCloudApiCondition::class) class VoiceMessageHandler( private val requestExecutor: RequestsExecutor, private val voiceClient: VoiceClient diff --git a/src/main/kotlin/com/github/djaler/evilbot/handlers/commands/CurrencyConverterHandler.kt b/src/main/kotlin/com/github/djaler/evilbot/handlers/commands/CurrencyConverterHandler.kt index bcf3c4ab..6c13b0e0 100644 --- a/src/main/kotlin/com/github/djaler/evilbot/handlers/commands/CurrencyConverterHandler.kt +++ b/src/main/kotlin/com/github/djaler/evilbot/handlers/commands/CurrencyConverterHandler.kt @@ -1,5 +1,6 @@ package com.github.djaler.evilbot.handlers.commands +import com.github.djaler.evilbot.config.fixer.FixerApiCondition import com.github.djaler.evilbot.handlers.base.CommandHandler import com.github.djaler.evilbot.handlers.base.spacesRegex import com.github.djaler.evilbot.service.CurrencyService @@ -11,6 +12,7 @@ import dev.inmo.tgbotapi.extensions.api.send.reply import dev.inmo.tgbotapi.types.chat.ExtendedBot import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage import dev.inmo.tgbotapi.types.message.content.TextContent +import org.springframework.context.annotation.Conditional import org.springframework.stereotype.Component import java.math.BigDecimal import java.math.MathContext @@ -19,6 +21,7 @@ import java.text.DecimalFormat import java.util.* @Component +@Conditional(FixerApiCondition::class) class CurrencyConverterHandler( private val currencyService: CurrencyService, private val requestsExecutor: RequestsExecutor, diff --git a/src/main/kotlin/com/github/djaler/evilbot/handlers/commands/CurrentTimeHandler.kt b/src/main/kotlin/com/github/djaler/evilbot/handlers/commands/CurrentTimeHandler.kt index 2f3fc653..c2dde966 100644 --- a/src/main/kotlin/com/github/djaler/evilbot/handlers/commands/CurrentTimeHandler.kt +++ b/src/main/kotlin/com/github/djaler/evilbot/handlers/commands/CurrentTimeHandler.kt @@ -1,6 +1,7 @@ package com.github.djaler.evilbot.handlers.commands import com.github.djaler.evilbot.components.TelegramMediaSender +import com.github.djaler.evilbot.config.locationiq.LocationiqApiCondition import com.github.djaler.evilbot.handlers.base.CommandHandler import com.github.djaler.evilbot.service.TimeService import dev.inmo.tgbotapi.bot.RequestsExecutor @@ -10,12 +11,14 @@ import dev.inmo.tgbotapi.extensions.utils.asLocationContent import dev.inmo.tgbotapi.types.chat.ExtendedBot import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage import dev.inmo.tgbotapi.types.message.content.TextContent +import org.springframework.context.annotation.Conditional import org.springframework.core.io.ClassPathResource import org.springframework.stereotype.Component import java.time.LocalDateTime import java.time.format.DateTimeFormatter @Component +@Conditional(LocationiqApiCondition::class) class CurrentTimeHandler( private val timeService: TimeService, private val requestsExecutor: RequestsExecutor, diff --git a/src/main/kotlin/com/github/djaler/evilbot/service/CurrencyService.kt b/src/main/kotlin/com/github/djaler/evilbot/service/CurrencyService.kt index eaed092d..6468f3c4 100644 --- a/src/main/kotlin/com/github/djaler/evilbot/service/CurrencyService.kt +++ b/src/main/kotlin/com/github/djaler/evilbot/service/CurrencyService.kt @@ -1,6 +1,8 @@ package com.github.djaler.evilbot.service import com.github.djaler.evilbot.clients.FixerClient +import com.github.djaler.evilbot.config.fixer.FixerApiCondition +import org.springframework.context.annotation.Conditional import org.springframework.stereotype.Service import java.math.BigDecimal import java.math.RoundingMode @@ -8,6 +10,7 @@ import java.time.LocalDate import java.time.ZoneOffset @Service +@Conditional(FixerApiCondition::class) class CurrencyService( private val fixerClient: FixerClient ) { diff --git a/src/main/kotlin/com/github/djaler/evilbot/service/TimeService.kt b/src/main/kotlin/com/github/djaler/evilbot/service/TimeService.kt index a7c1144a..b1aa45a1 100644 --- a/src/main/kotlin/com/github/djaler/evilbot/service/TimeService.kt +++ b/src/main/kotlin/com/github/djaler/evilbot/service/TimeService.kt @@ -1,12 +1,15 @@ package com.github.djaler.evilbot.service import com.github.djaler.evilbot.clients.LocationiqClient +import com.github.djaler.evilbot.config.locationiq.LocationiqApiCondition import io.ktor.client.plugins.* +import org.springframework.context.annotation.Conditional import org.springframework.stereotype.Service import java.time.LocalDateTime import java.time.ZoneOffset @Service +@Conditional(LocationiqApiCondition::class) class TimeService( private val locationiqClient: LocationiqClient ) {