Skip to content

Commit e4b2f94

Browse files
committed
@feat: basic processing of messages sent personally to comrade Beria
1 parent 6f78070 commit e4b2f94

File tree

4 files changed

+30
-7
lines changed

4 files changed

+30
-7
lines changed

src/main/kotlin/su/redbyte/androidkrdbot/cli/ChatCommissar.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,12 @@ fun main() {
5050
LootInfoCmd(searchArticles)
5151
)
5252

53-
val listeners = listOf(
53+
val messageListeners = listOf(
5454
CacheMessageListener(),
5555
CacheComradeListener(appScope, fetchComrades),
5656
NewMembersListener(getRandomQuestion, scheduleVerification),
57-
AnswerListener(checkAnswer)
57+
AnswerListener(checkAnswer),
58+
ReplyToMessageListener()
5859
)
5960

6061
val adminOnly = AdminOnly(checkAdminRights)
@@ -67,7 +68,7 @@ fun main() {
6768
commands = commands,
6869
globalMiddlewares = globalMW,
6970
adminOnly = adminOnly,
70-
messageListeners = listeners,
71+
messageListeners = messageListeners,
7172
)
7273

7374
Runtime.getRuntime().addShutdownHook(Thread {

src/main/kotlin/su/redbyte/androidkrdbot/cli/engine/BotEngine.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package su.redbyte.androidkrdbot.cli.engine
22

33
import com.github.kotlintelegrambot.bot
44
import com.github.kotlintelegrambot.dispatch
5-
import com.github.kotlintelegrambot.dispatcher.chatMember
65
import com.github.kotlintelegrambot.dispatcher.command
76
import com.github.kotlintelegrambot.dispatcher.message
87
import kotlinx.coroutines.CoroutineScope
@@ -11,7 +10,6 @@ import su.redbyte.androidkrdbot.cli.command.BotCommand
1110
import su.redbyte.androidkrdbot.cli.command.CommandContext
1211
import su.redbyte.androidkrdbot.cli.command.RequireAdmin
1312
import su.redbyte.androidkrdbot.cli.command.buildContext
14-
import su.redbyte.androidkrdbot.cli.message.ChatMemberListener
1513
import su.redbyte.androidkrdbot.cli.message.MessageContext
1614
import su.redbyte.androidkrdbot.cli.message.MessageListener
1715
import su.redbyte.androidkrdbot.cli.middleware.Middleware
@@ -52,6 +50,7 @@ class BotEngine(
5250
messageListeners.forEach { it.handle(mctx) }
5351
}
5452
}
53+
5554
}
5655
}
5756

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package su.redbyte.androidkrdbot.cli.message
2+
3+
import su.redbyte.androidkrdbot.utils.containsBotMention
4+
5+
class ReplyToMessageListener : MessageListener {
6+
override suspend fun handle(ctx: MessageContext) {
7+
val botUserName = ctx.bot.getMe().get().username ?: ""
8+
if (!ctx.message.containsBotMention(botUserName)) return
9+
val cleanText = ctx.message.text
10+
?.replace("@$botUserName", "", ignoreCase = true)
11+
?.trim()
12+
?: ""
13+
//todo: STUB
14+
println(cleanText)
15+
// ctx.reply(cleanText)
16+
}
17+
}

src/main/kotlin/su/redbyte/androidkrdbot/utils/utils.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package su.redbyte.androidkrdbot.utils
33
import com.github.kotlintelegrambot.Bot
44
import com.github.kotlintelegrambot.entities.ChatId
55
import com.github.kotlintelegrambot.entities.Message
6+
import com.github.kotlintelegrambot.entities.MessageEntity
67
import com.github.kotlintelegrambot.entities.User
78
import com.github.kotlintelegrambot.types.TelegramBotResult
89
import su.redbyte.androidkrdbot.data.repository.MessageCache
@@ -55,7 +56,12 @@ fun Bot.sendAndCacheMessage(
5556
response.getOrNull()?.let {
5657
MessageCache.add(chatId.rawChatId(), botId, it.messageId)
5758
}
58-
5959
return response
60+
}
61+
62+
fun Message.containsBotMention(botUserName: String): Boolean =
63+
entities
64+
?.filter { it.type == MessageEntity.Type.MENTION }
65+
?.any { text?.substring(it.offset, it.offset + it.length) == "@$botUserName" }
66+
?: false
6067

61-
}

0 commit comments

Comments
 (0)