Commit 08380a29 authored by nk2's avatar nk2 🙃

5-alpha

filtering bots
botid
subscriber is now tied to bot
parent 49bac18c
This diff is collapsed.
......@@ -43,7 +43,7 @@ class BotCommunicatorImpl: BotCommunicator {
}
}
override fun sendMessage(subscriber: Subscriber, message: Message) {
botStarter.botImplMap.forEach {
botStarter.botImplMap.filter { subscriber.botId == it.value.botId }.forEach {
it.value.sendMessage(subscriber, message)
}
}
......
......@@ -14,9 +14,6 @@ import java.util.HashMap
import kotlin.reflect.full.findAnnotation
import kotlin.reflect.full.superclasses
//TODO no command handler
//TODO filter for bot type in subscriber
@Component
class BotStarterImpl: BotStarter {
val botImplMap = HashMap<String, Bot>()
......
......@@ -3,8 +3,8 @@ package ga.nk2ishere.dev.bot.data
import ga.nk2ishere.dev.bot.interfaces.BotSubscriberRepository
class FakeBotSubscriberRepository: BotSubscriberRepository {
override fun get(chatId: Long): Subscriber = Subscriber(chatId, "")
override fun get(userName: String): Subscriber = Subscriber(0, userName)
override fun get(botId: String, chatId: Long): Subscriber = Subscriber(botId, chatId, "")
override fun get(botId: String, userName: String): Subscriber = Subscriber(botId,0, userName)
override fun contains(subscriber: Subscriber): Boolean = false
override fun put(subscriber: Subscriber) {}
override fun edit(subscriber: Subscriber) {}
......
......@@ -7,7 +7,7 @@ sealed class MessageItem
data class MessageText(val text: String) : MessageItem()
data class MessageImage(val image: File) : MessageItem()
class Message {
class Message() {
private val items = LinkedList<MessageItem>()
fun addText(text: String) = items.add(MessageText(text))
......
package ga.nk2ishere.dev.bot.data
data class Subscriber(
val botId: String,
val chatId: Long,
val userName: String,
var state: SubscriberState? = null
......
......@@ -5,6 +5,7 @@ import ga.nk2ishere.dev.bot.data.Subscriber
import java.io.File
interface Bot {
val botId: String
fun onStart()
fun sendText(subscriber: Subscriber, text: String)
fun sendImage(subscriber: Subscriber, image: File)
......
......@@ -3,8 +3,8 @@ package ga.nk2ishere.dev.bot.interfaces
import ga.nk2ishere.dev.bot.data.Subscriber
interface BotSubscriberRepository {
fun get(chatId: Long): Subscriber?
fun get(userName: String): Subscriber?
fun get(botId: String, chatId: Long): Subscriber?
fun get(botId: String, userName: String): Subscriber?
fun contains(subscriber: Subscriber): Boolean
fun put(subscriber: Subscriber)
fun edit(subscriber: Subscriber)
......
......@@ -10,8 +10,8 @@ import org.springframework.stereotype.Component
class BotRepositoryImpl: BotSubscriberRepository {
val subscriberList = hashSetOf<Subscriber>()
override fun get(chatId: Long): Subscriber? = subscriberList.firstOrNull { it.chatId == chatId }
override fun get(userName: String): Subscriber? = subscriberList.firstOrNull { it.userName == userName }
override fun get(botId: String, chatId: Long): Subscriber? = subscriberList.firstOrNull { it.chatId == chatId && it.botId == botId }
override fun get(botId: String, userName: String): Subscriber? = subscriberList.firstOrNull { it.userName == userName && it.botId == botId }
override fun contains(subscriber: Subscriber): Boolean = subscriberList.firstOrNull { it.chatId == subscriber.chatId } != null
......
......@@ -33,6 +33,9 @@ class TelegramBotImpl: TelegramLongPollingBot(), Bot {
@Value("#{environment['telegram.token']}") lateinit var token: String
@Value("#{environment['telegram.username']}") lateinit var userName: String
override val botId: String
get() = javaClass.simpleName
override fun getBotToken(): String = token
override fun getBotUsername(): String = userName
......@@ -62,8 +65,8 @@ class TelegramBotImpl: TelegramLongPollingBot(), Bot {
override fun onUpdateReceived(update: Update?) {
update?.let {
botCommunicator.onMessageReceived(
botSubscriberRepository.get(update.message.chatId)
?: Subscriber(update.message.chatId, update.message.from.userName),
botSubscriberRepository.get(botId, update.message.chatId)
?: Subscriber(botId, 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