-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): 组件模块下支持
TelegramBotCommands
、TelegramBotCommand
、`Teleg…
…ramBotCommandsUpdater` 等与 bot commands 相关的API
- Loading branch information
1 parent
c7790e1
commit e7fe507
Showing
7 changed files
with
403 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
...nMain/kotlin/love/forte/simbot/component/telegram/core/bot/command/TelegramBotCommands.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
* Copyright (c) 2024. ForteScarlet. | ||
* | ||
* This file is part of simbot-component-telegram. | ||
* | ||
* simbot-component-telegram is free software: you can redistribute it and/or modify it under the terms | ||
* of the GNU Lesser General Public License as published by the Free Software Foundation, | ||
* either version 3 of the License, or (at your option) any later version. | ||
* | ||
* simbot-component-telegram is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; | ||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
* See the GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License along with simbot-component-telegram. | ||
* If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package love.forte.simbot.component.telegram.core.bot.command | ||
|
||
import love.forte.simbot.ability.DeleteOption | ||
import love.forte.simbot.ability.DeleteSupport | ||
import love.forte.simbot.component.telegram.core.bot.TelegramBot | ||
import love.forte.simbot.telegram.type.BotCommand | ||
import love.forte.simbot.telegram.type.BotCommandScope | ||
import kotlin.jvm.JvmSynthetic | ||
|
||
|
||
/** | ||
* A set of Telegram's [BotCommand]s | ||
* | ||
* @author ForteScarlet | ||
*/ | ||
public interface TelegramBotCommands : DeleteSupport, Iterable<TelegramBotCommand> { | ||
/** | ||
* The list of [TelegramBotCommand] | ||
*/ | ||
public val values: List<TelegramBotCommand> | ||
|
||
/** | ||
* Iterator of [values]. | ||
*/ | ||
override fun iterator(): Iterator<TelegramBotCommand> = | ||
values.iterator() | ||
|
||
/** | ||
* A command scope used in the query. | ||
*/ | ||
public val scope: BotCommandScope? | ||
|
||
/** | ||
* A two-letter ISO 639-1 language code used in the query. | ||
*/ | ||
public val languageCode: String? | ||
|
||
/** | ||
* Delete this set of commands with [scope] and [languageCode]. | ||
*/ | ||
@JvmSynthetic | ||
override suspend fun delete(vararg options: DeleteOption) | ||
|
||
/** | ||
* Get a [TelegramBotCommandsUpdater] with [scope] and [languageCode] | ||
* for update commands. | ||
* | ||
* @see TelegramBotCommandsUpdater | ||
*/ | ||
public val updater: TelegramBotCommandsUpdater | ||
} | ||
|
||
/** | ||
* Update bot commands by [TelegramBotCommands.updater] | ||
* | ||
* @see TelegramBotCommands.updater | ||
*/ | ||
public suspend inline fun TelegramBotCommands.update( | ||
block: TelegramBotCommandsUpdater.() -> Unit | ||
): TelegramBotCommands = updater.also(block).update() | ||
|
||
/** | ||
* A [BotCommand] of [TelegramBotCommands] from [TelegramBot.commands]. | ||
* | ||
* @see TelegramBotCommands | ||
* @see BotCommand | ||
*/ | ||
public interface TelegramBotCommand { | ||
/** | ||
* The source type [BotCommand] | ||
*/ | ||
public val source: BotCommand | ||
|
||
/** | ||
* Text of the command. | ||
* | ||
* @see BotCommand.command | ||
*/ | ||
public val command: String | ||
get() = source.command | ||
|
||
/** | ||
* Description of the command. | ||
* | ||
* @see BotCommand.description | ||
*/ | ||
public val description: String | ||
get() = source.description | ||
} | ||
|
||
|
82 changes: 82 additions & 0 deletions
82
...otlin/love/forte/simbot/component/telegram/core/bot/command/TelegramBotCommandsUpdater.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Copyright (c) 2024. ForteScarlet. | ||
* | ||
* This file is part of simbot-component-telegram. | ||
* | ||
* simbot-component-telegram is free software: you can redistribute it and/or modify it under the terms | ||
* of the GNU Lesser General Public License as published by the Free Software Foundation, | ||
* either version 3 of the License, or (at your option) any later version. | ||
* | ||
* simbot-component-telegram is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; | ||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
* See the GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License along with simbot-component-telegram. | ||
* If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package love.forte.simbot.component.telegram.core.bot.command | ||
|
||
import love.forte.simbot.suspendrunner.ST | ||
import love.forte.simbot.telegram.type.BotCommand | ||
import love.forte.simbot.telegram.type.BotCommandScope | ||
|
||
|
||
/** | ||
* An updater for set bot commands. | ||
* | ||
* @author ForteScarlet | ||
*/ | ||
public interface TelegramBotCommandsUpdater { | ||
/** | ||
* the [BotCommandScope] | ||
*/ | ||
public var scope: BotCommandScope? | ||
|
||
/** | ||
* the [languageCode] | ||
*/ | ||
public var languageCode: String? | ||
|
||
/** | ||
* Commands for update. | ||
*/ | ||
public var commands: MutableList<BotCommand> | ||
|
||
/** | ||
* @see scope | ||
*/ | ||
public fun scope(scope: BotCommandScope?): TelegramBotCommandsUpdater = apply { | ||
this.scope = scope | ||
} | ||
|
||
/** | ||
* @see languageCode | ||
*/ | ||
public fun languageCode(languageCode: String?): TelegramBotCommandsUpdater = apply { | ||
this.languageCode = languageCode | ||
} | ||
|
||
/** | ||
* Add a command into [commands] | ||
* @see commands | ||
*/ | ||
public fun addCommand(command: BotCommand): TelegramBotCommandsUpdater = apply { | ||
commands.add(command) | ||
} | ||
|
||
/** | ||
* Add a command into [commands] | ||
* @see commands | ||
*/ | ||
public fun addCommand(command: String, description: String): TelegramBotCommandsUpdater = | ||
addCommand(BotCommand(command, description)) | ||
|
||
/** | ||
* Execute setting and a new [TelegramBotCommands] that | ||
* values **directly** based on new commands is returned. | ||
* | ||
*/ | ||
@ST | ||
public suspend fun update(): TelegramBotCommands | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
...love/forte/simbot/component/telegram/core/bot/internal/command/TelegramBotCommandsImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Copyright (c) 2024. ForteScarlet. | ||
* | ||
* This file is part of simbot-component-telegram. | ||
* | ||
* simbot-component-telegram is free software: you can redistribute it and/or modify it under the terms | ||
* of the GNU Lesser General Public License as published by the Free Software Foundation, | ||
* either version 3 of the License, or (at your option) any later version. | ||
* | ||
* simbot-component-telegram is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; | ||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
* See the GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License along with simbot-component-telegram. | ||
* If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package love.forte.simbot.component.telegram.core.bot.internal.command | ||
|
||
import love.forte.simbot.ability.DeleteFailureException | ||
import love.forte.simbot.ability.DeleteOption | ||
import love.forte.simbot.ability.StandardDeleteOption | ||
import love.forte.simbot.component.telegram.core.bot.TelegramBot | ||
import love.forte.simbot.component.telegram.core.bot.command.TelegramBotCommand | ||
import love.forte.simbot.component.telegram.core.bot.command.TelegramBotCommands | ||
import love.forte.simbot.component.telegram.core.bot.command.TelegramBotCommandsUpdater | ||
import love.forte.simbot.telegram.api.bot.command.DeleteMyCommandsApi | ||
import love.forte.simbot.telegram.stdlib.bot.requestDataBy | ||
import love.forte.simbot.telegram.type.BotCommand | ||
import love.forte.simbot.telegram.type.BotCommandScope | ||
|
||
internal class TelegramBotCommandsImpl( | ||
private val bot: TelegramBot, | ||
override val values: List<TelegramBotCommand>, | ||
override val scope: BotCommandScope?, | ||
override val languageCode: String?, | ||
) : TelegramBotCommands { | ||
override suspend fun delete(vararg options: DeleteOption) { | ||
runCatching { | ||
DeleteMyCommandsApi.create( | ||
scope = scope, | ||
languageCode = languageCode | ||
).requestDataBy(bot.source) | ||
}.onFailure { err -> | ||
if (StandardDeleteOption.IGNORE_ON_FAILURE !in options) { | ||
throw DeleteFailureException(err.message, err) | ||
} | ||
} | ||
} | ||
|
||
override val updater: TelegramBotCommandsUpdater | ||
get() = TelegramBotCommandsUpdaterImpl( | ||
bot = bot, | ||
scope = scope, | ||
languageCode = languageCode | ||
) | ||
|
||
override fun toString(): String = | ||
"TelegramBotCommands(scope=$scope, languageCode=$languageCode, values=$values)" | ||
} | ||
|
||
internal fun Iterable<BotCommand>.toTGCommands( | ||
bot: TelegramBot, | ||
scope: BotCommandScope?, | ||
languageCode: String?, | ||
): TelegramBotCommandsImpl = | ||
TelegramBotCommandsImpl( | ||
bot, | ||
this.map { it.toTGCommand() }, | ||
scope, | ||
languageCode | ||
) | ||
|
||
/** | ||
* | ||
* @author ForteScarlet | ||
*/ | ||
internal class TelegramBotCommandImpl( | ||
override val source: BotCommand | ||
) : TelegramBotCommand { | ||
override fun toString(): String { | ||
return "TelegramBotCommand(source=$source)" | ||
} | ||
} | ||
|
||
internal fun BotCommand.toTGCommand(): TelegramBotCommandImpl = | ||
TelegramBotCommandImpl(this) |
Oops, something went wrong.