Commit 49bac18c authored by nk2's avatar nk2 🙃

4-alpha

new abstraction layer
parent b2af394e
......@@ -6,6 +6,7 @@
<fileset id="fileset" name="TelegramBotImpl" removed="false">
<file>file://$MODULE_DIR$/../../src/main/kotlin/ga/nk2ishere/dev/runtime/config/TelegramBotImpl.kt</file>
<file>file://$MODULE_DIR$/../../src/main/kotlin/ga/nk2ishere/dev/bot/BotCommunicatorImpl.kt</file>
<file>file://$MODULE_DIR$/../../src/main/kotlin/ga/nk2ishere/dev/runtime/Bootstrap.kt</file>
</fileset>
</configuration>
</facet>
......
This diff is collapsed.
package ga.nk2ishere.dev.bot
import ga.nk2ishere.dev.bot.data.FakeBotSubscriberRepository
import ga.nk2ishere.dev.bot.data.Message
import ga.nk2ishere.dev.bot.data.Subscriber
import ga.nk2ishere.dev.bot.interfaces.BotCommunicator
......@@ -11,15 +10,15 @@ import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
@Configuration
@ConditionalOnBean(BotSubscriberRepositoryStarter::class, BotControllerStarter::class)
@ConditionalOnBean(BotSubscriberRepositoryStarterImpl::class, BotControllerStarterImpl::class)
class BotCommunicatorImpl: BotCommunicator {
private val log = LoggerFactory.getLogger(this.javaClass)
@Bean fun botCommunicator(): BotCommunicator = BotCommunicatorImpl()
@Autowired private lateinit var botStarter: BotStarter
@Autowired private lateinit var botSubscriberRepositoryStarter: BotSubscriberRepositoryStarter
@Autowired private lateinit var botControllerStarter: BotControllerStarter
@Autowired private lateinit var botExceptionControllerStarter: BotExceptionControllerStarter
@Autowired private lateinit var botNoCommandCheckerStarter: BotNoCommandCheckerStarter
private val log = LoggerFactory.getLogger(this.javaClass)
@Autowired private lateinit var botStarter: BotStarterImpl
@Autowired private lateinit var botControllerStarter: BotControllerStarterImpl
@Autowired private lateinit var botExceptionControllerStarter: BotExceptionControllerStarterImpl
@Autowired private lateinit var botNoCommandCheckerStarter: BotNoCommandCheckerStarterImpl
private fun handleException(commandName: String, exception: Exception, subscriber: Subscriber, message: Message) {
log.error("Error ${exception.cause} while trying to invoke $commandName")
......@@ -33,7 +32,7 @@ class BotCommunicatorImpl: BotCommunicator {
log.info("Got message from: $subscriber $message")
botControllerStarter.botControllerMap.filter { it.value.third(message) }.also {
if(it.isEmpty())
botNoCommandCheckerStarter.handleNoCommandMessage(subscriber, message)
sendMessage(subscriber, botNoCommandCheckerStarter.handleNoCommandMessage(subscriber, message))
}.forEach {
log.info("Passed message to ${it.key}")
try {
......@@ -48,5 +47,4 @@ class BotCommunicatorImpl: BotCommunicator {
it.value.sendMessage(subscriber, message)
}
}
override fun getSubcriberRepository() = botSubscriberRepositoryStarter.botSubscriberRepository ?: FakeBotSubscriberRepository()
}
\ No newline at end of file
......@@ -5,6 +5,7 @@ import ga.nk2ishere.dev.bot.annotations.BotMethod
import ga.nk2ishere.dev.bot.annotations.BotMethodChecker
import ga.nk2ishere.dev.bot.data.Message
import ga.nk2ishere.dev.bot.data.Subscriber
import ga.nk2ishere.dev.bot.interfaces.BotStarter
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.config.BeanPostProcessor
......@@ -14,7 +15,7 @@ import java.util.HashMap
@Component
class BotControllerStarter: BeanPostProcessor {
class BotControllerStarterImpl: BotStarter {
private val checkerBaseClass = { msg: Message -> true }.javaClass
val botControllerMap = HashMap<String, Triple<Any, Method, (Message) -> Boolean>>()
override fun postProcessBeforeInitialization(bean: Any, name: String): Any? {
......
......@@ -3,6 +3,7 @@ package ga.nk2ishere.dev.bot
import ga.nk2ishere.dev.bot.annotations.*
import ga.nk2ishere.dev.bot.data.Message
import ga.nk2ishere.dev.bot.data.Subscriber
import ga.nk2ishere.dev.bot.interfaces.BotStarter
import org.springframework.beans.factory.config.BeanPostProcessor
import org.springframework.stereotype.Component
import java.lang.reflect.Method
......@@ -10,7 +11,7 @@ import java.util.HashMap
import kotlin.reflect.KClass
@Component
class BotExceptionControllerStarter: BeanPostProcessor {
class BotExceptionControllerStarterImpl: BotStarter {
val botControllerMap = HashMap<KClass<*>, Pair<Any, Method>>()
override fun postProcessBeforeInitialization(bean: Any, name: String): Any? {
if(bean.javaClass.isAnnotationPresent(BotExceptionController::class.java)) {
......
......@@ -6,12 +6,13 @@ import ga.nk2ishere.dev.bot.data.FakeBotNoCommandChecker
import ga.nk2ishere.dev.bot.data.Message
import ga.nk2ishere.dev.bot.data.Subscriber
import ga.nk2ishere.dev.bot.interfaces.BotNoCommandChecker
import ga.nk2ishere.dev.bot.interfaces.BotStarter
import ga.nk2ishere.dev.bot.interfaces.BotSubscriberRepository
import org.springframework.beans.factory.config.BeanPostProcessor
import org.springframework.stereotype.Component
@Component
class BotNoCommandCheckerStarter: BeanPostProcessor {
class BotNoCommandCheckerStarterImpl: BotStarter {
private val defaultBotNoCommandChecker = FakeBotNoCommandChecker()
private var botNoCommandChecker: BotNoCommandChecker? = null
fun handleNoCommandMessage(subscriber: Subscriber, message: Message) = botNoCommandChecker?.onNoCommand(subscriber, message)
......
......@@ -5,6 +5,7 @@ import ga.nk2ishere.dev.bot.data.FakeBotSubscriberRepository
import ga.nk2ishere.dev.bot.data.Message
import ga.nk2ishere.dev.bot.data.Subscriber
import ga.nk2ishere.dev.bot.interfaces.Bot
import ga.nk2ishere.dev.bot.interfaces.BotStarter
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.config.BeanPostProcessor
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean
......@@ -17,7 +18,7 @@ import kotlin.reflect.full.superclasses
//TODO filter for bot type in subscriber
@Component
class BotStarter: BeanPostProcessor {
class BotStarterImpl: BotStarter {
val botImplMap = HashMap<String, Bot>()
override fun postProcessBeforeInitialization(bean: Any, name: String): Any? {
if(bean.javaClass.superclass.isAnnotationPresent(BotImpl::class.java))
......
package ga.nk2ishere.dev.bot
import ga.nk2ishere.dev.bot.annotations.BotSubscriberRepositoryImpl
import ga.nk2ishere.dev.bot.interfaces.BotStarter
import ga.nk2ishere.dev.bot.interfaces.BotSubscriberRepository
import org.springframework.beans.factory.config.BeanPostProcessor
import org.slf4j.LoggerFactory
import org.springframework.context.annotation.Bean
import org.springframework.stereotype.Component
@Component
class BotSubscriberRepositoryStarter: BeanPostProcessor {
var botSubscriberRepository: BotSubscriberRepository? = null
class BotSubscriberRepositoryStarterImpl: BotStarter {
private val log = LoggerFactory.getLogger(javaClass)
private var botSubscriberRepository: BotSubscriberRepository? = null
@Bean() fun botSubscriberRepository() = botSubscriberRepository
override fun postProcessBeforeInitialization(bean: Any, beanName: String): Any? {
if(bean.javaClass.isAnnotationPresent(BotSubscriberRepositoryImpl::class.java))
if(botSubscriberRepository == null) {
botSubscriberRepository = if(botSubscriberRepository == null) {
when(bean) {
is BotSubscriberRepository -> botSubscriberRepository = bean
is BotSubscriberRepository -> bean
else -> throw BotException("Annotated BotSubscriberRepositoryImpl class ${bean.javaClass.simpleName} must implement BotSubscriberRepository")
}
} else throw BotException("Only one class can be BotSubscriberRepositoryImpl, current: ${bean.javaClass.simpleName}, used: ${botSubscriberRepository?.javaClass?.simpleName}")
} else {
log.info("Only one class can be BotSubscriberRepositoryImpl, current: ${bean.javaClass.simpleName}, used: ${botSubscriberRepository?.javaClass?.simpleName}")
botSubscriberRepository
}
return bean
}
......
......@@ -6,5 +6,4 @@ import ga.nk2ishere.dev.bot.data.Subscriber
interface BotCommunicator {
fun onMessageReceived(subscriber: Subscriber, message: Message)
fun sendMessage(subscriber: Subscriber, message: Message)
fun getSubcriberRepository(): BotSubscriberRepository
}
\ No newline at end of file
package ga.nk2ishere.dev.bot.interfaces
import org.springframework.beans.factory.config.BeanPostProcessor
interface BotStarter: BeanPostProcessor
\ No newline at end of file
......@@ -8,7 +8,7 @@ import org.springframework.stereotype.Component
@Component
@BotExceptionController
class ExceptionUseCase {
class ExceptionController {
@BotExceptionMethod(NullPointerException::class)
fun handleNullPointerException(subscriber: Subscriber, message: Message) =
Message().apply { addText("Uh oh! Something bad happened") }
......
......@@ -4,9 +4,11 @@ import ga.nk2ishere.dev.bot.annotations.BotNoCommandCheckerImpl
import ga.nk2ishere.dev.bot.data.Message
import ga.nk2ishere.dev.bot.data.Subscriber
import ga.nk2ishere.dev.bot.interfaces.BotNoCommandChecker
import org.springframework.stereotype.Component
@Component
@BotNoCommandCheckerImpl
class NoCommandUseCase: BotNoCommandChecker {
class NoCommandController: BotNoCommandChecker {
override fun onNoCommand(subscriber: Subscriber, message: Message): Message =
Message().apply { addText("Oh! There's no command found like that") }
}
\ No newline at end of file
......@@ -8,7 +8,7 @@ import org.springframework.stereotype.Component
@Component
@BotController
class StartUseCase {
class StartController {
@BotMethodChecker("/hi") @JvmField final val checkHiCommand = { msg: Message -> msg.hasTextFull("/hi") }
@BotMethod("/hi") fun hiCommand(subscriber: Subscriber, message: Message): Message {
return Message().apply { this.addText("Welcome!") }
......
package ga.nk2ishere.dev.runtime.config
import ga.nk2ishere.dev.bot.*
import ga.nk2ishere.dev.bot.annotations.BotImpl
import ga.nk2ishere.dev.bot.data.Message
import ga.nk2ishere.dev.bot.data.MessageImage
......@@ -8,12 +7,12 @@ import ga.nk2ishere.dev.bot.data.MessageText
import ga.nk2ishere.dev.bot.data.Subscriber
import ga.nk2ishere.dev.bot.interfaces.Bot
import ga.nk2ishere.dev.bot.interfaces.BotCommunicator
import ga.nk2ishere.dev.bot.interfaces.BotSubscriberRepository
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Value
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.PropertySource
import org.telegram.telegrambots.ApiContextInitializer
import org.telegram.telegrambots.api.methods.send.SendMessage
import org.telegram.telegrambots.api.methods.send.SendPhoto
import org.telegram.telegrambots.api.objects.Update
......@@ -29,6 +28,7 @@ import org.telegram.telegrambots.TelegramBotsApi
class TelegramBotImpl: TelegramLongPollingBot(), Bot {
@Autowired private lateinit var botCommunicator: BotCommunicator
@Autowired private lateinit var telegramBotImpl: TelegramBotImpl
@Autowired private lateinit var botSubscriberRepository: BotSubscriberRepository
val log = LoggerFactory.getLogger(this.javaClass)
@Value("#{environment['telegram.token']}") lateinit var token: String
......@@ -62,7 +62,7 @@ class TelegramBotImpl: TelegramLongPollingBot(), Bot {
override fun onUpdateReceived(update: Update?) {
update?.let {
botCommunicator.onMessageReceived(
botCommunicator.getSubcriberRepository().get(update.message.chatId)
botSubscriberRepository.get(update.message.chatId)
?: Subscriber(update.message.chatId, update.message.from.userName),
Message().apply { this.addText(it.message.text) }
)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment