Skip to content

Commit

Permalink
Merge pull request #20 from simple-robot/dev/support-set-commands-API
Browse files Browse the repository at this point in the history
增加 `setMyCommands` 和 `deleteMyCommands` 的原始API实现
  • Loading branch information
ForteScarlet authored Jul 22, 2024
2 parents 31bad7b + c7790e1 commit 4abd5a8
Show file tree
Hide file tree
Showing 3 changed files with 207 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* 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.telegram.api.bot.command

import kotlinx.serialization.DeserializationStrategy
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.builtins.serializer
import love.forte.simbot.telegram.api.SimpleBodyTelegramApi
import love.forte.simbot.telegram.api.TelegramApiResult
import love.forte.simbot.telegram.type.BotCommandScope
import kotlin.jvm.JvmOverloads
import kotlin.jvm.JvmStatic


/**
* [deleteMyCommands](https://core.telegram.org/bots/api#deletemycommands)
*
* Use this method to delete the list of the bot's commands for the given scope and user language.
* After deletion, higher level commands will be shown to affected users. Returns True on success.
*
* @author ForteScarlet
*/
public class DeleteMyCommandsApi
private constructor(body: Body) : SimpleBodyTelegramApi<Boolean>() {
public companion object Factory {
private const val NAME = "deleteMyCommands"
private val EMPTY = DeleteMyCommandsApi(Body())

/**
* Create an instance of [DeleteMyCommandsApi].
*
* @param scope A JSON-serialized object, describing scope of users for which the commands are relevant.
* Defaults to BotCommandScopeDefault.
* @param languageCode A two-letter ISO 639-1 language code.
* If empty, commands will be applied to all users from the given scope,
* for whose language there are no dedicated commands
*/
@JvmStatic
@JvmOverloads
public fun create(
scope: BotCommandScope? = null,
languageCode: String? = null,
): DeleteMyCommandsApi {
return if (scope == null && languageCode == null) {
EMPTY
} else {
DeleteMyCommandsApi(
Body(
scope = scope,
languageCode = languageCode
)
)
}
}
}

/**
* Request body for [DeleteMyCommandsApi]
* @property scope A JSON-serialized object, describing scope of users for which the commands are relevant.
* Defaults to BotCommandScopeDefault.
* @property languageCode A two-letter ISO 639-1 language code.
* If empty, commands will be applied to all users from the given scope,
* for whose language there are no dedicated commands
*/
@Serializable
public data class Body(
val scope: BotCommandScope? = null,
@SerialName("language_code")
val languageCode: String? = null,
)

override val name: String
get() = NAME

override val body: Any = body

override val responseDeserializer: DeserializationStrategy<Boolean>
get() = Boolean.serializer()

override val resultDeserializer: DeserializationStrategy<TelegramApiResult<Boolean>>
get() = TelegramApiResult.BooleanSerializer
}

/*
Parameter Type Required Description
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* 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.telegram.api.bot.command

import kotlinx.serialization.DeserializationStrategy
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.builtins.serializer
import love.forte.simbot.telegram.api.SimpleBodyTelegramApi
import love.forte.simbot.telegram.api.TelegramApiResult
import love.forte.simbot.telegram.type.BotCommand
import love.forte.simbot.telegram.type.BotCommandScope
import kotlin.jvm.JvmOverloads
import kotlin.jvm.JvmStatic


/**
* [setMyCommands](https://core.telegram.org/bots/api#setmycommands)
* Use this method to change the list of the bot's commands.
* See this manual for more details about bot commands. Returns True on success.
*
* @author ForteScarlet
*/
public class SetMyCommandsApi
private constructor(body: Body) : SimpleBodyTelegramApi<Boolean>() {
public companion object Factory {
private const val NAME = "setMyCommands"

/**
* Create an instance of [SetMyCommandsApi].
*
* @param commands A JSON-serialized list of bot commands to be set as the list of the bot's commands.
* At most 100 commands can be specified.
* @param scope A JSON-serialized object, describing scope of users for which the commands are relevant.
* Defaults to BotCommandScopeDefault.
* @param languageCode A two-letter ISO 639-1 language code.
* If empty, commands will be applied to all users from the given scope,
* for whose language there are no dedicated commands
*/
@JvmStatic
@JvmOverloads
public fun create(
commands: List<BotCommand>,
scope: BotCommandScope? = null,
languageCode: String? = null,
): SetMyCommandsApi = SetMyCommandsApi(
Body(
commands = commands,
scope = scope,
languageCode = languageCode
)
)
}

/**
* Request body for [SetMyCommandsApi]
* @property commands A JSON-serialized list of bot commands to be set as the list of the bot's commands.
* At most 100 commands can be specified.
* @property scope A JSON-serialized object, describing scope of users for which the commands are relevant.
* Defaults to BotCommandScopeDefault.
* @property languageCode A two-letter ISO 639-1 language code.
* If empty, commands will be applied to all users from the given scope,
* for whose language there are no dedicated commands
*/
@Serializable
public data class Body(
val commands: List<BotCommand> = emptyList(),
val scope: BotCommandScope? = null,
@SerialName("language_code")
val languageCode: String? = null,
)

override val name: String
get() = NAME

override val body: Any = body

override val responseDeserializer: DeserializationStrategy<Boolean>
get() = Boolean.serializer()

override val resultDeserializer: DeserializationStrategy<TelegramApiResult<Boolean>>
get() = TelegramApiResult.BooleanSerializer
}

/*
Parameter Type Required Description
*/
4 changes: 3 additions & 1 deletion simbot-component-telegram-api/supports.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
- [bot](src/commonMain/kotlin/love/forte/simbot/telegram/api/bot)
- [x] [CloseApi](src/commonMain/kotlin/love/forte/simbot/telegram/api/bot/CloseApi.kt)
- [command](src/commonMain/kotlin/love/forte/simbot/telegram/api/bot/command)
- [x] [DeleteMyCommandsApi](src/commonMain/kotlin/love/forte/simbot/telegram/api/bot/command/DeleteMyCommandsApi.kt)
- [x] [GetMyCommandsApi](src/commonMain/kotlin/love/forte/simbot/telegram/api/bot/command/GetMyCommandsApi.kt)
- [x] [SetMyCommandsApi](src/commonMain/kotlin/love/forte/simbot/telegram/api/bot/command/SetMyCommandsApi.kt)
- [x] [GetChatMenuButtonApi](src/commonMain/kotlin/love/forte/simbot/telegram/api/bot/GetChatMenuButtonApi.kt)
- [x] [GetMeApi](src/commonMain/kotlin/love/forte/simbot/telegram/api/bot/GetMeApi.kt)
- [x] [GetMyDefaultAdministratorRightsApi](src/commonMain/kotlin/love/forte/simbot/telegram/api/bot/GetMyDefaultAdministratorRightsApi.kt)
Expand Down Expand Up @@ -38,4 +40,4 @@
- [x] [SetWebhookApi](src/commonMain/kotlin/love/forte/simbot/telegram/api/update/SetWebhookApi.kt)
- [user](src/commonMain/kotlin/love/forte/simbot/telegram/api/user)
- [x] [GetUserProfilePhotosApi](src/commonMain/kotlin/love/forte/simbot/telegram/api/user/GetUserProfilePhotosApi.kt)
- [ ] **Others not listed**
- [ ] **Others aren’t listed**

0 comments on commit 4abd5a8

Please sign in to comment.