File tree Expand file tree Collapse file tree 4 files changed +30
-7
lines changed
src/main/kotlin/su/redbyte/androidkrdbot Expand file tree Collapse file tree 4 files changed +30
-7
lines changed Original file line number Diff line number Diff line change @@ -50,11 +50,12 @@ fun main() {
50
50
LootInfoCmd (searchArticles)
51
51
)
52
52
53
- val listeners = listOf (
53
+ val messageListeners = listOf (
54
54
CacheMessageListener (),
55
55
CacheComradeListener (appScope, fetchComrades),
56
56
NewMembersListener (getRandomQuestion, scheduleVerification),
57
- AnswerListener (checkAnswer)
57
+ AnswerListener (checkAnswer),
58
+ ReplyToMessageListener ()
58
59
)
59
60
60
61
val adminOnly = AdminOnly (checkAdminRights)
@@ -67,7 +68,7 @@ fun main() {
67
68
commands = commands,
68
69
globalMiddlewares = globalMW,
69
70
adminOnly = adminOnly,
70
- messageListeners = listeners ,
71
+ messageListeners = messageListeners ,
71
72
)
72
73
73
74
Runtime .getRuntime().addShutdownHook(Thread {
Original file line number Diff line number Diff line change @@ -2,7 +2,6 @@ package su.redbyte.androidkrdbot.cli.engine
2
2
3
3
import com.github.kotlintelegrambot.bot
4
4
import com.github.kotlintelegrambot.dispatch
5
- import com.github.kotlintelegrambot.dispatcher.chatMember
6
5
import com.github.kotlintelegrambot.dispatcher.command
7
6
import com.github.kotlintelegrambot.dispatcher.message
8
7
import kotlinx.coroutines.CoroutineScope
@@ -11,7 +10,6 @@ import su.redbyte.androidkrdbot.cli.command.BotCommand
11
10
import su.redbyte.androidkrdbot.cli.command.CommandContext
12
11
import su.redbyte.androidkrdbot.cli.command.RequireAdmin
13
12
import su.redbyte.androidkrdbot.cli.command.buildContext
14
- import su.redbyte.androidkrdbot.cli.message.ChatMemberListener
15
13
import su.redbyte.androidkrdbot.cli.message.MessageContext
16
14
import su.redbyte.androidkrdbot.cli.message.MessageListener
17
15
import su.redbyte.androidkrdbot.cli.middleware.Middleware
@@ -52,6 +50,7 @@ class BotEngine(
52
50
messageListeners.forEach { it.handle(mctx) }
53
51
}
54
52
}
53
+
55
54
}
56
55
}
57
56
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ package su.redbyte.androidkrdbot.utils
3
3
import com.github.kotlintelegrambot.Bot
4
4
import com.github.kotlintelegrambot.entities.ChatId
5
5
import com.github.kotlintelegrambot.entities.Message
6
+ import com.github.kotlintelegrambot.entities.MessageEntity
6
7
import com.github.kotlintelegrambot.entities.User
7
8
import com.github.kotlintelegrambot.types.TelegramBotResult
8
9
import su.redbyte.androidkrdbot.data.repository.MessageCache
@@ -55,7 +56,12 @@ fun Bot.sendAndCacheMessage(
55
56
response.getOrNull()?.let {
56
57
MessageCache .add(chatId.rawChatId(), botId, it.messageId)
57
58
}
58
-
59
59
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
60
67
61
- }
You can’t perform that action at this time.
0 commit comments