Skip to content

Commit 73000b9

Browse files
authored
Merge pull request #18 from simple-robot/dev/fix-simbot-885
使TelegramBot.id 返回 ticket.token 而不是 user.id, 并增加新的属性 `userId`; 以及修复bot启动时未初始化用户信息的问题
2 parents 260675d + 12cf60b commit 73000b9

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,21 @@ public interface TelegramBot : Bot {
6464
get() = userInfo.username ?: userInfo.firstName
6565

6666
/**
67-
* The id of current bot.
67+
* The id of [userInfo].
6868
*
69-
* @throws IllegalStateException The bot has not been started, or has never been used [queryUserInfo].
70-
* @see userInfo
69+
* @throws IllegalStateException The bot has not be started,
70+
* or has never been used [queryUserInfo]
71+
*/
72+
public val userId: ID
73+
get() = userInfo.id.ID
74+
75+
/**
76+
* The bot token configured.
7177
*
78+
* If you want to get the `user.id` of this bot,
79+
* use [userId].
7280
*/
7381
override val id: ID
74-
get() = userInfo.id.ID
7582

7683
/**
7784
* Query user info of the current bot.

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ import kotlinx.coroutines.sync.withLock
2626
import love.forte.simbot.annotations.FragileSimbotAPI
2727
import love.forte.simbot.bot.JobBasedBot
2828
import love.forte.simbot.common.id.ID
29-
import love.forte.simbot.common.id.literal
29+
import love.forte.simbot.common.id.StringID.Companion.ID
30+
import love.forte.simbot.common.id.toLongOrNull
3031
import love.forte.simbot.component.telegram.core.bot.StdlibBot
3132
import love.forte.simbot.component.telegram.core.bot.TelegramBot
3233
import love.forte.simbot.component.telegram.core.bot.TelegramBotConfiguration
@@ -71,6 +72,8 @@ internal class TelegramBotImpl(
7172
internal val logger = LoggerFactory.getLogger("love.forte.simbot.component.telegram.core.bot")
7273
internal val eventLogger = LoggerFactory.getLogger("love.forte.simbot.component.telegram.stdlib.bot.event")
7374

75+
override val id: ID = source.ticket.token.ID
76+
7477
private var _userInfo: User? = null
7578

7679
override val userInfo: User
@@ -83,10 +86,10 @@ internal class TelegramBotImpl(
8386
}
8487

8588
override fun isMe(id: ID): Boolean {
86-
// TODO
87-
return source.ticket.token == id.literal
88-
}
89+
val ui = _userInfo ?: return this.id == id
8990

91+
return this.id == id || ui.id == id.toLongOrNull()
92+
}
9093

9194
private val startLock = Mutex()
9295

@@ -99,6 +102,9 @@ internal class TelegramBotImpl(
99102
}
100103

101104
source.start()
105+
// init bot user info
106+
queryUserInfo()
107+
102108
subscribeInternalProcessor(this, source, eventDispatcher)
103109

104110
// mark started

0 commit comments

Comments
 (0)