Skip to content

Commit

Permalink
improvement: возможность запуска приложения без интеграции со сторонн…
Browse files Browse the repository at this point in the history
…ими API
  • Loading branch information
Djaler committed Oct 9, 2022
1 parent 44e1be9 commit 41a7b80
Show file tree
Hide file tree
Showing 15 changed files with 73 additions and 23 deletions.
7 changes: 6 additions & 1 deletion src/main/kotlin/com/github/djaler/evilbot/Application.kt
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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()
}
}
Original file line number Diff line number Diff line change
@@ -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 = ""
)
Original file line number Diff line number Diff line change
@@ -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()
}
}
Original file line number Diff line number Diff line change
@@ -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 = ""
)
Original file line number Diff line number Diff line change
@@ -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()
}
}
Original file line number Diff line number Diff line change
@@ -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
)
val key: String = ""
)
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
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
import java.time.LocalDate
import java.time.ZoneOffset

@Service
@Conditional(FixerApiCondition::class)
class CurrencyService(
private val fixerClient: FixerClient
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
) {
Expand Down

0 comments on commit 41a7b80

Please sign in to comment.