mirror of
https://github.com/LamGC/ScalaBot.git
synced 2025-12-16 07:00:43 +00:00
feat: 将 TelegramBots 升级至 8.0.0, 并适配 TelegramBots 的新改动.
将 TelegramBots 升级至新版本, 以支持新的 API.
由于 TelegramBots 发生无法兼容旧版本的重大变更, 因此 ScalaBot 将随着此次更新一同进行重大更改.
BREAKING CHANGE: ScalaBot 所依赖的 TelegramBots 发生重大更改, 所有扩展都需要进行适配.
有关 TelegramBots 的重大变更说明请参考官方文档.
ScalaBot 的最低 Java 版本已全部升级至 Java 17 (这是 TelegramBots 的最低兼容性要求), 所有扩展都应该至少迁移至 Java 17 版本.
ScalaBot 的重大更改:
- scalabot-extension
- `net.lamgc.scalabot.extension.util.AbilityBots.getBotAccountId(BaseAbilityBot): long` 已被移除, 由于 BaseAbilityBot 不再允许获取 botToken, 因此该方法被移除. 作为代替, 请通过 `net.lamgc.scalabot.extension.BotExtensionFactory.createExtensionInstance` 所得到的 `BotExtensionCreateOptions` 中获取 botAccountId.
另外, scalabot-extension 中的 `org.jetbrains.kotlinx.binary-compatibility-validator` 似乎不再对 Java 代码起作用, 因此移除该插件, 并在后续寻找替代品.
TelegramBots 文档: https://rubenlagus.github.io/TelegramBotsDocumentation/how-to-update-7.html
This commit is contained in:
@ -6,43 +6,42 @@ import io.prometheus.client.Summary
|
||||
import mu.KotlinLogging
|
||||
import net.lamgc.scalabot.config.BotConfig
|
||||
import org.eclipse.aether.artifact.Artifact
|
||||
import org.telegram.abilitybots.api.bot.AbilityBot
|
||||
import org.telegram.abilitybots.api.db.DBContext
|
||||
import org.telegram.abilitybots.api.objects.Ability
|
||||
import org.telegram.abilitybots.api.toggle.BareboneToggle
|
||||
import org.telegram.abilitybots.api.toggle.DefaultToggle
|
||||
import org.telegram.telegrambots.bots.DefaultBotOptions
|
||||
import org.telegram.telegrambots.abilitybots.api.bot.AbilityBot
|
||||
import org.telegram.telegrambots.abilitybots.api.db.DBContext
|
||||
import org.telegram.telegrambots.abilitybots.api.objects.Ability
|
||||
import org.telegram.telegrambots.abilitybots.api.toggle.BareboneToggle
|
||||
import org.telegram.telegrambots.abilitybots.api.toggle.DefaultToggle
|
||||
import org.telegram.telegrambots.meta.api.methods.commands.DeleteMyCommands
|
||||
import org.telegram.telegrambots.meta.api.methods.commands.SetMyCommands
|
||||
import org.telegram.telegrambots.meta.api.objects.Update
|
||||
import org.telegram.telegrambots.meta.api.objects.commands.BotCommand
|
||||
import org.telegram.telegrambots.meta.generics.TelegramClient
|
||||
|
||||
/**
|
||||
* 可扩展 Bot.
|
||||
* @property creatorId 机器人所有人的 Telegram 用户 Id. 可通过联系部分机器人来获取该信息.
|
||||
* @property creatorId 机器人所有人的 Telegram 用户 ID. 可通过联系部分机器人来获取该信息.
|
||||
* (e.g. [@userinfobot](http://t.me/userinfobot))
|
||||
* @param db 机器人数据库对象. 用于状态机等用途.
|
||||
* @param options AbilityBot 设置对象.
|
||||
* @property extensions 扩展坐标集合.
|
||||
*/
|
||||
@Suppress("CanBeParameter", "MemberVisibilityCanBePrivate")
|
||||
internal class ScalaBot(
|
||||
db: DBContext,
|
||||
options: DefaultBotOptions,
|
||||
client: TelegramClient,
|
||||
extensionFinders: Set<ExtensionPackageFinder>,
|
||||
val botConfig: BotConfig,
|
||||
private val creatorId: Long = botConfig.account.creatorId,
|
||||
val accountId: Long = botConfig.account.id,
|
||||
private val creatorId: Long = botConfig.account.creatorId,
|
||||
val extensions: Set<Artifact> = botConfig.extensions
|
||||
) :
|
||||
AbilityBot(
|
||||
botConfig.account.token,
|
||||
client,
|
||||
botConfig.account.name,
|
||||
db,
|
||||
if (botConfig.disableBuiltInAbility)
|
||||
BareboneToggle()
|
||||
else
|
||||
DefaultToggle(),
|
||||
options
|
||||
DefaultToggle()
|
||||
) {
|
||||
|
||||
private val extensionLoader = ExtensionLoader(
|
||||
@ -67,13 +66,13 @@ internal class ScalaBot(
|
||||
|
||||
override fun creatorId(): Long = creatorId
|
||||
|
||||
override fun onUpdateReceived(update: Update?) {
|
||||
override fun consume(update: Update?) {
|
||||
botUpdateCounter.labels(botUsername, accountIdString).inc()
|
||||
botUpdateGauge.labels(botUsername, accountIdString).inc()
|
||||
|
||||
val timer = updateProcessTime.labels(botUsername, accountIdString).startTimer()
|
||||
try {
|
||||
super.onUpdateReceived(update)
|
||||
super.consume(update)
|
||||
} catch (e: Exception) {
|
||||
exceptionHandlingCounter.labels(botUsername, accountIdString).inc()
|
||||
throw e
|
||||
@ -92,11 +91,11 @@ internal class ScalaBot(
|
||||
* @return 更新成功返回 `true`.
|
||||
*/
|
||||
fun updateCommandList(): Boolean {
|
||||
if (abilities() == null) {
|
||||
if (abilities == null) {
|
||||
throw IllegalStateException("Abilities has not been initialized.")
|
||||
}
|
||||
|
||||
val botCommands = abilities().values.map {
|
||||
val botCommands = abilities.values.map {
|
||||
val abilityInfo = if (it.info() == null || it.info().trim().isEmpty()) {
|
||||
log.warn { "[Bot $botUsername] Ability `${it.name()}` 没有说明信息." }
|
||||
"(The command has no description)"
|
||||
@ -112,9 +111,10 @@ internal class ScalaBot(
|
||||
return true
|
||||
}
|
||||
|
||||
val setMyCommands = SetMyCommands()
|
||||
setMyCommands.commands = botCommands
|
||||
return execute(DeleteMyCommands()) && execute(setMyCommands)
|
||||
val setMyCommands = SetMyCommands.builder()
|
||||
.commands(botCommands)
|
||||
.build()
|
||||
return telegramClient.execute(DeleteMyCommands()) && telegramClient.execute(setMyCommands)
|
||||
}
|
||||
|
||||
override fun onRegister() {
|
||||
@ -122,10 +122,6 @@ internal class ScalaBot(
|
||||
onlineBotGauge.inc()
|
||||
}
|
||||
|
||||
override fun onClosing() {
|
||||
onlineBotGauge.dec()
|
||||
}
|
||||
|
||||
companion object {
|
||||
@JvmStatic
|
||||
private val log = KotlinLogging.logger { }
|
||||
|
||||
Reference in New Issue
Block a user