Skip to content

Commit

Permalink
Drop a message this notice the dionea_bot #24
Browse files Browse the repository at this point in the history
  • Loading branch information
peterarsentev committed Jun 16, 2024
1 parent 0de884e commit d9662c0
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 19 deletions.
31 changes: 31 additions & 0 deletions src/main/kotlin/pro/dionea/service/actions/ActionConfig.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package pro.dionea.service.actions

import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.security.crypto.password.PasswordEncoder
import pro.dionea.service.*

@Configuration
class ActionConfig(
private val voteService: VoteService,
private val contactService: ContactService,
private val spamService: SpamService,
private val chatService: ChatService,
private val userService: UserService,
private val encoding: PasswordEncoder,
private val spamAnalysis: SpamAnalysis,
) {

@Bean
fun actions(remoteChat: RemoteChat, botUsername: String): List<UpdateAction> {
return listOf(
VoteCallBackAction(voteService, contactService, spamService, chatService, remoteChat),
EmptyMessageAction(),
JoinAction(contactService, remoteChat),
ImageAttachedAction(contactService, remoteChat),
PrivateChatAction(userService, encoding, remoteChat),
ReplyAction(remoteChat, botUsername),
TextMessageAction(contactService, spamService, spamAnalysis, chatService, remoteChat)
)
}
}
20 changes: 1 addition & 19 deletions src/main/kotlin/pro/dionea/service/actions/Receiver.kt
Original file line number Diff line number Diff line change
@@ -1,40 +1,22 @@
package pro.dionea.service.actions

import org.springframework.beans.factory.annotation.Value
import org.springframework.security.crypto.password.PasswordEncoder
import org.springframework.stereotype.Service
import org.telegram.telegrambots.bots.TelegramLongPollingBot
import org.telegram.telegrambots.meta.api.objects.Update
import pro.dionea.service.*

@Service
class Receiver(
@Value("\${tg.name}") val name: String,
@Value("\${tg.token}") val token: String,
spamAnalysis: SpamAnalysis,
spamService: SpamService,
voteService: VoteService,
userService: UserService,
encoding: PasswordEncoder,
contactService: ContactService,
chatService: ChatService,
private val actions: List<UpdateAction>
) : TelegramLongPollingBot(), RemoteChat {

companion object {
const val VOTE_SIZE_YES = 3
const val VOTE_SIZE_NO = 3
}

private val actions = listOf(
VoteCallBackAction(voteService, contactService, spamService, chatService, this),
EmptyMessageAction(),
JoinAction(contactService, this),
ImageAttachedAction(contactService, this),
PrivateChatAction(userService, encoding, this),
ReplyAction(this, botUsername),
TextMessageAction(contactService, spamService, spamAnalysis, chatService,this)
)

override fun onUpdateReceived(update: Update) {
for (action in actions) {
if (action.check(update)) {
Expand Down
18 changes: 18 additions & 0 deletions src/test/kotlin/pro/dionea/service/actions/RemoteChatFake.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package pro.dionea.service.actions

import org.telegram.telegrambots.meta.api.methods.BotApiMethod
import org.telegram.telegrambots.meta.api.objects.Message
import java.io.Serializable

class RemoteChatFake : RemoteChat {
private val messages = ArrayList<BotApiMethod<out Serializable>>()

override fun <T : Serializable, M : BotApiMethod<T>> execute(method: M): T {
messages.add(method)
return Message() as T
}

fun getMessages(): List<BotApiMethod<out Serializable>> {
return messages
}
}
75 changes: 75 additions & 0 deletions src/test/kotlin/pro/dionea/service/actions/ReplyActionTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package pro.dionea.service.actions

import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.telegram.telegrambots.meta.api.methods.send.SendMessage
import org.telegram.telegrambots.meta.api.objects.Chat
import org.telegram.telegrambots.meta.api.objects.Message
import org.telegram.telegrambots.meta.api.objects.Update
import org.telegram.telegrambots.meta.api.objects.replykeyboard.InlineKeyboardMarkup

class ReplyActionTest {

private lateinit var remoteChat: RemoteChatFake
private lateinit var replyAction: ReplyAction
private val botName = "testBot"

@BeforeEach
fun setUp() {
remoteChat = RemoteChatFake()
replyAction = ReplyAction(remoteChat, botName)
}

@Test
fun `when check with valid update then return true`() {
val update = createUpdate("testBot", true)
val result = replyAction.check(update)
assertThat(result).isTrue()
}

@Test
fun `when check with invalid update then return false`() {
val update = createUpdate("someOtherBot", true)
val result = replyAction.check(update)
assertThat(result).isFalse()
}

@Test
fun `when execute then send message with correct properties`() {
val update = createUpdate(botName, true)
replyAction.execute(update)
val messages = remoteChat.getMessages()
assertThat(messages).hasSize(1)
val sendMessage = messages[0] as SendMessage
with(sendMessage) {
assertThat(chatId).isEqualTo("12345")
assertThat(text).isEqualTo("Это спам? Проголосуйте за 3 раза.")
assertThat(replyToMessageId).isEqualTo(2)

val markup = replyMarkup as InlineKeyboardMarkup
val keyboard = markup.keyboard
assertThat(keyboard).hasSize(1)
assertThat(keyboard[0]).hasSize(2)
assertThat(keyboard[0][0].text).isEqualTo("Да: 0")
assertThat(keyboard[0][0].callbackData).isEqualTo("yes 1")
assertThat(keyboard[0][1].text).isEqualTo("Нет: 0")
assertThat(keyboard[0][1].callbackData).isEqualTo("no 1")
}
}

private fun createUpdate(botName: String, isReply: Boolean): Update {
val update = Update()
val message = Message()
message.chat = Chat(12345L, "private")
message.text = botName
if (isReply) {
val replyToMessage = Message()
replyToMessage.messageId = 2
message.replyToMessage = replyToMessage
}
message.messageId = 1
update.message = message
return update
}
}

0 comments on commit d9662c0

Please sign in to comment.