Skip to content

Commit d01aee8

Browse files
authored
Merge pull request #29 from simple-robot/dev/support-internal-events
Support `TelegramBotRegisteredEvent` and `TelegramBotStartedEvent`
2 parents c642939 + 2fb5ab0 commit d01aee8

File tree

5 files changed

+130
-29
lines changed

5 files changed

+130
-29
lines changed

simbot-component-telegram-core/src/commonMain/kotlin/love/forte/simbot/component/telegram/core/bot/internal/TelegramBotImpl.kt

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ package love.forte.simbot.component.telegram.core.bot.internal
1919

2020
import kotlinx.coroutines.Job
2121
import kotlinx.coroutines.ensureActive
22+
import kotlinx.coroutines.flow.Flow
2223
import kotlinx.coroutines.flow.collect
24+
import kotlinx.coroutines.flow.launchIn
2325
import kotlinx.coroutines.flow.onCompletion
2426
import kotlinx.coroutines.sync.Mutex
2527
import kotlinx.coroutines.sync.withLock
@@ -37,12 +39,10 @@ import love.forte.simbot.component.telegram.core.bot.internal.command.TelegramBo
3739
import love.forte.simbot.component.telegram.core.bot.internal.command.toTGCommands
3840
import love.forte.simbot.component.telegram.core.component.TelegramComponent
3941
import love.forte.simbot.component.telegram.core.event.TelegramUnsupportedEvent
40-
import love.forte.simbot.component.telegram.core.event.internal.TelegramChannelMessageEventImpl
41-
import love.forte.simbot.component.telegram.core.event.internal.TelegramChatGroupMessageEventImpl
42-
import love.forte.simbot.component.telegram.core.event.internal.TelegramPrivateMessageEventImpl
43-
import love.forte.simbot.component.telegram.core.event.internal.TelegramSuperGroupMessageEventImpl
42+
import love.forte.simbot.component.telegram.core.event.internal.*
4443
import love.forte.simbot.event.Event
4544
import love.forte.simbot.event.EventDispatcher
45+
import love.forte.simbot.event.EventResult
4646
import love.forte.simbot.event.onEachError
4747
import love.forte.simbot.logger.LoggerFactory
4848
import love.forte.simbot.telegram.api.bot.GetMeApi
@@ -114,6 +114,9 @@ internal class TelegramBotImpl(
114114

115115
// mark started
116116
isStarted = true
117+
// push start event
118+
eventDispatcher.pushEventWithBot(TelegramBotStartedEventImpl(this), this)
119+
.launchIn(this)
117120
}
118121
}
119122

@@ -139,9 +142,6 @@ internal class TelegramBotImpl(
139142
}
140143

141144

142-
143-
144-
145145
@OptIn(FragileSimbotAPI::class)
146146
internal fun subscribeInternalProcessor(
147147
bot: TelegramBotImpl,
@@ -151,14 +151,11 @@ internal fun subscribeInternalProcessor(
151151
val divider = object : SuspendableUpdateDivider<StdlibEvent>() {
152152
private suspend fun pushEvent(event: Event) {
153153
bot.logger.debug("Bot {} on event: {}", bot, event)
154-
eventDispatcher.push(event)
155-
.onEachError { result ->
156-
bot.eventLogger.error("Bot {} on event dispatch error result: {}", bot, result, result.content)
157-
}
158-
.onCompletion {
154+
eventDispatcher.pushEventWithBot(event, bot) { flow ->
155+
flow.onCompletion {
159156
bot.eventLogger.trace("Bot {} event publish completed", bot)
160157
}
161-
.collect()
158+
}.collect()
162159
}
163160

164161
override suspend fun onMismatchUpdateEvent(name: String, value: Any, update: Update?, context: StdlibEvent) {
@@ -379,3 +376,15 @@ internal fun subscribeInternalProcessor(
379376
}
380377
}
381378

379+
380+
internal inline fun EventDispatcher.pushEventWithBot(
381+
event: Event,
382+
bot: TelegramBotImpl,
383+
block: (Flow<EventResult>) -> Flow<EventResult> = { it }
384+
): Flow<EventResult> {
385+
return push(event)
386+
.onEachError { result ->
387+
bot.eventLogger.error("Bot {} on event dispatch error result: {}", bot, result, result.content)
388+
}
389+
.let(block)
390+
}

simbot-component-telegram-core/src/commonMain/kotlin/love/forte/simbot/component/telegram/core/bot/internal/TelegramBotManagerImpl.kt

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,21 @@
1818
package love.forte.simbot.component.telegram.core.bot.internal
1919

2020
import kotlinx.coroutines.Job
21+
import kotlinx.coroutines.flow.launchIn
2122
import love.forte.simbot.bot.ConflictBotException
2223
import love.forte.simbot.bot.JobBasedBotManager
2324
import love.forte.simbot.common.collection.computeValue
2425
import love.forte.simbot.common.collection.concurrentMutableMap
2526
import love.forte.simbot.common.collection.removeValue
2627
import love.forte.simbot.common.coroutines.mergeWith
2728
import love.forte.simbot.common.id.ID
28-
import love.forte.simbot.common.id.NumericalID
2929
import love.forte.simbot.common.id.literal
3030
import love.forte.simbot.component.telegram.core.bot.TelegramBot
3131
import love.forte.simbot.component.telegram.core.bot.TelegramBotConfiguration
3232
import love.forte.simbot.component.telegram.core.bot.TelegramBotManager
3333
import love.forte.simbot.component.telegram.core.bot.TelegramBotManagerConfiguration
3434
import love.forte.simbot.component.telegram.core.component.TelegramComponent
35+
import love.forte.simbot.component.telegram.core.event.internal.TelegramBotRegisteredEventImpl
3536
import love.forte.simbot.event.EventDispatcher
3637
import love.forte.simbot.telegram.stdlib.bot.Bot
3738
import love.forte.simbot.telegram.stdlib.bot.BotFactory
@@ -53,7 +54,7 @@ internal class TelegramBotManagerImpl(
5354
private val botsWithTokenKey = concurrentMutableMap<String, TelegramBotImpl>()
5455

5556
// id token cache
56-
private val idTokenMap = concurrentMutableMap<Long, String>()
57+
// private val idTokenMap = concurrentMutableMap<Long, String>()
5758

5859
override fun register(ticket: Bot.Ticket, configuration: TelegramBotConfiguration): TelegramBot {
5960
val token = ticket.token
@@ -78,30 +79,27 @@ internal class TelegramBotManagerImpl(
7879
createBot()
7980
}!!
8081

81-
// TODO register update idTokenMap
82-
8382
newBot.onCompletion {
8483
botsWithTokenKey.removeValue(token) { newBot }
8584
}
8685

86+
eventDispatcher.pushRegisteredEvent(newBot)
87+
8788
return newBot
8889
}
8990

9091
override fun all(): Sequence<TelegramBot> =
9192
botsWithTokenKey.values.asSequence()
9293

9394
override fun find(id: ID): TelegramBot? {
94-
fun findByLongId(lid: Long): TelegramBot? =
95-
idTokenMap[lid]?.let { token -> botsWithTokenKey[token] }?.takeIf { it.isActive }
96-
97-
if (id is NumericalID) {
98-
return findByLongId(id.toLong())
99-
}
100-
10195
val token = id.literal
102-
val foundByToken = botsWithTokenKey[token]
103-
if (foundByToken != null) return foundByToken
104-
105-
return token.toLongOrNull()?.let { longId -> findByLongId(longId) }
96+
return botsWithTokenKey[token]
10697
}
10798
}
99+
100+
internal fun EventDispatcher.pushRegisteredEvent(
101+
bot: TelegramBotImpl
102+
): Job {
103+
val e = TelegramBotRegisteredEventImpl(bot)
104+
return pushEventWithBot(e, bot).launchIn(bot)
105+
}

simbot-component-telegram-core/src/commonMain/kotlin/love/forte/simbot/component/telegram/core/event/TelegramEvent.kt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,25 @@ import love.forte.simbot.telegram.type.Message
3030
*/
3131
public typealias StdlibEvent = love.forte.simbot.telegram.stdlib.event.Event
3232

33+
/**
34+
* An telegram bot event.
35+
*
36+
* @see TelegramEvent
37+
*/
38+
public interface TelegramBotEvent : BotEvent {
39+
/**
40+
* The [TelegramBot].
41+
*/
42+
override val bot: TelegramBot
43+
}
44+
3345
/**
3446
* Is a Telegram simbot component event definition type.
3547
* Is an implementation of the event type in the simbot API.
3648
*
3749
* @author ForteScarlet
3850
*/
39-
public interface TelegramEvent : BotEvent {
51+
public interface TelegramEvent : TelegramBotEvent {
4052
// 所有的 Telegram Event 都是建立在 Bot 之上的,
4153
// 因此所有 TelegramEvent 都实现 BotEvent
4254

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright (c) 2024. ForteScarlet.
3+
*
4+
* This file is part of simbot-component-telegram.
5+
*
6+
* simbot-component-telegram is free software: you can redistribute it and/or modify it under the terms
7+
* of the GNU Lesser General Public License as published by the Free Software Foundation,
8+
* either version 3 of the License, or (at your option) any later version.
9+
*
10+
* simbot-component-telegram is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
11+
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12+
* See the GNU Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public License along with simbot-component-telegram.
15+
* If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
package love.forte.simbot.component.telegram.core.event
19+
20+
import love.forte.simbot.component.telegram.core.bot.TelegramBot
21+
import love.forte.simbot.event.BotRegisteredEvent
22+
23+
24+
/**
25+
* An event that indicating a [TelegramBot] has been registered.
26+
*
27+
* This is an event used as a notification
28+
* that the bot may not have been started yet.
29+
*/
30+
public interface TelegramBotRegisteredEvent : BotRegisteredEvent, TelegramBotEvent {
31+
override val bot: TelegramBot
32+
}
33+
34+
/**
35+
* An event that indicating a [TelegramBot] has been started at the first time.
36+
*
37+
* This is an event used as a notification.
38+
* At this point, [bot.isStarted][TelegramBot.isStarted] is already `true`.
39+
*/
40+
public interface TelegramBotStartedEvent : BotRegisteredEvent, TelegramBotEvent {
41+
override val bot: TelegramBot
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (c) 2024. ForteScarlet.
3+
*
4+
* This file is part of simbot-component-telegram.
5+
*
6+
* simbot-component-telegram is free software: you can redistribute it and/or modify it under the terms
7+
* of the GNU Lesser General Public License as published by the Free Software Foundation,
8+
* either version 3 of the License, or (at your option) any later version.
9+
*
10+
* simbot-component-telegram is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
11+
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12+
* See the GNU Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public License along with simbot-component-telegram.
15+
* If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
package love.forte.simbot.component.telegram.core.event.internal
19+
20+
import love.forte.simbot.annotations.ExperimentalSimbotAPI
21+
import love.forte.simbot.common.id.ID
22+
import love.forte.simbot.common.id.UUID
23+
import love.forte.simbot.common.time.Timestamp
24+
import love.forte.simbot.component.telegram.core.bot.TelegramBot
25+
import love.forte.simbot.component.telegram.core.event.TelegramBotRegisteredEvent
26+
import love.forte.simbot.component.telegram.core.event.TelegramBotStartedEvent
27+
28+
internal class TelegramBotRegisteredEventImpl(override val bot: TelegramBot) : TelegramBotRegisteredEvent {
29+
override val id: ID = UUID.random()
30+
31+
@OptIn(ExperimentalSimbotAPI::class)
32+
override val time: Timestamp = Timestamp.now()
33+
}
34+
35+
internal class TelegramBotStartedEventImpl(override val bot: TelegramBot) : TelegramBotStartedEvent {
36+
override val id: ID = UUID.random()
37+
38+
@OptIn(ExperimentalSimbotAPI::class)
39+
override val time: Timestamp = Timestamp.now()
40+
}

0 commit comments

Comments
 (0)