From 7f69052deab47c6d0617918496ac435c82be107a Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 26 Jun 2021 01:42:35 +0600 Subject: [PATCH] *MyCommands actualization --- CHANGELOG.md | 1 + .../inmo/tgbotapi/requests/bot/BotCommand.kt | 9 ++++++++ .../tgbotapi/requests/bot/DeleteMyCommands.kt | 22 +++++++++++++++++++ .../tgbotapi/requests/bot/GetMyCommands.kt | 13 +++++++++-- .../tgbotapi/requests/bot/SetMyCommands.kt | 10 +++++++-- .../kotlin/dev/inmo/tgbotapi/types/Common.kt | 1 + .../extensions/api/bot/DeleteMyCommands.kt | 11 ++++++++++ .../extensions/api/bot/GetMyCommands.kt | 7 +++++- .../extensions/api/bot/SetMyCommands.kt | 14 ++++++++---- 9 files changed, 79 insertions(+), 9 deletions(-) create mode 100644 tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/bot/BotCommand.kt create mode 100644 tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/bot/DeleteMyCommands.kt create mode 100644 tgbotapi.extensions.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/bot/DeleteMyCommands.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 67f14cd528..0ae7235f5f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ * `Bot API 5.3`: * Add type `BotCommandScope`, its serializer `BotCommandScopeSerializer` and all its children + * New request `DeleteMyCommands` and updates in `GetMyCommands` and `SetMyCommands` ## 0.35.0 diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/bot/BotCommand.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/bot/BotCommand.kt new file mode 100644 index 0000000000..bc6b7cac0b --- /dev/null +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/bot/BotCommand.kt @@ -0,0 +1,9 @@ +package dev.inmo.tgbotapi.requests.bot + +import dev.inmo.tgbotapi.requests.abstracts.SimpleRequest +import dev.inmo.tgbotapi.types.commands.BotCommandScope + +sealed interface MyCommandsRequest : SimpleRequest { + val scope: BotCommandScope + val languageCode: String? +} diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/bot/DeleteMyCommands.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/bot/DeleteMyCommands.kt new file mode 100644 index 0000000000..867b5fc978 --- /dev/null +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/bot/DeleteMyCommands.kt @@ -0,0 +1,22 @@ +package dev.inmo.tgbotapi.requests.bot + +import dev.inmo.tgbotapi.types.commands.* +import dev.inmo.tgbotapi.types.languageCodeField +import dev.inmo.tgbotapi.types.scopeField +import kotlinx.serialization.* +import kotlinx.serialization.builtins.serializer + +@Serializable +data class DeleteMyCommands( + @SerialName(scopeField) + @Serializable(BotCommandScopeSerializer::class) + override val scope: BotCommandScope = BotCommandScopeDefault, + @SerialName(languageCodeField) + override val languageCode: String? = null +) : MyCommandsRequest { + override fun method(): String = "deleteMyCommands" + override val requestSerializer: SerializationStrategy = serializer() + override val resultDeserializer: DeserializationStrategy = Boolean.serializer() + + companion object : MyCommandsRequest by DeleteMyCommands() +} diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/bot/GetMyCommands.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/bot/GetMyCommands.kt index ebb3ee54d2..10ade23af5 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/bot/GetMyCommands.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/bot/GetMyCommands.kt @@ -1,17 +1,26 @@ package dev.inmo.tgbotapi.requests.bot import dev.inmo.tgbotapi.requests.abstracts.SimpleRequest -import dev.inmo.tgbotapi.types.BotCommand +import dev.inmo.tgbotapi.types.* +import dev.inmo.tgbotapi.types.commands.* import kotlinx.serialization.* import kotlinx.serialization.builtins.ListSerializer private val getMyCommandsSerializer = ListSerializer(BotCommand.serializer()) @Serializable -object GetMyCommands : SimpleRequest> { +data class GetMyCommands( + @SerialName(scopeField) + @Serializable(BotCommandScopeSerializer::class) + override val scope: BotCommandScope = BotCommandScopeDefault, + @SerialName(languageCodeField) + override val languageCode: String? = null +) : MyCommandsRequest> { override fun method(): String = "getMyCommands" override val resultDeserializer: DeserializationStrategy> get() = getMyCommandsSerializer override val requestSerializer: SerializationStrategy<*> get() = serializer() + + companion object : MyCommandsRequest> by GetMyCommands() } diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/bot/SetMyCommands.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/bot/SetMyCommands.kt index 1e8e068328..efb4d62657 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/bot/SetMyCommands.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/bot/SetMyCommands.kt @@ -2,14 +2,20 @@ package dev.inmo.tgbotapi.requests.bot import dev.inmo.tgbotapi.requests.abstracts.SimpleRequest import dev.inmo.tgbotapi.types.* +import dev.inmo.tgbotapi.types.commands.* import kotlinx.serialization.* import kotlinx.serialization.builtins.serializer @Serializable class SetMyCommands( @SerialName(botCommandsField) - val commands: List -) : SimpleRequest { + val commands: List, + @SerialName(scopeField) + @Serializable(BotCommandScopeSerializer::class) + override val scope: BotCommandScope = BotCommandScopeDefault, + @SerialName(languageCodeField) + override val languageCode: String? = null +) : MyCommandsRequest { override fun method(): String = "setMyCommands" override val resultDeserializer: DeserializationStrategy get() = Boolean.serializer() diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/Common.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/Common.kt index 8c89fdf0d5..8d9f796715 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/Common.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/Common.kt @@ -234,6 +234,7 @@ const val hideUrlField = "hide_url" const val botCommandField = "command" const val botCommandsField = "commands" +const val scopeField = "scope" const val isMemberField = "is_member" const val canSendMessagesField = "can_send_messages" diff --git a/tgbotapi.extensions.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/bot/DeleteMyCommands.kt b/tgbotapi.extensions.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/bot/DeleteMyCommands.kt new file mode 100644 index 0000000000..31abad88f5 --- /dev/null +++ b/tgbotapi.extensions.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/bot/DeleteMyCommands.kt @@ -0,0 +1,11 @@ +package dev.inmo.tgbotapi.extensions.api.bot + +import dev.inmo.tgbotapi.bot.TelegramBot +import dev.inmo.tgbotapi.requests.bot.DeleteMyCommands +import dev.inmo.tgbotapi.types.commands.BotCommandScope +import dev.inmo.tgbotapi.types.commands.BotCommandScopeDefault + +suspend fun TelegramBot.deleteMyCommands( + scope: BotCommandScope = BotCommandScopeDefault, + languageCode: String? = null +) = execute(DeleteMyCommands(scope, languageCode)) diff --git a/tgbotapi.extensions.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/bot/GetMyCommands.kt b/tgbotapi.extensions.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/bot/GetMyCommands.kt index 22995619e7..07a0c6a3ac 100644 --- a/tgbotapi.extensions.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/bot/GetMyCommands.kt +++ b/tgbotapi.extensions.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/bot/GetMyCommands.kt @@ -2,5 +2,10 @@ package dev.inmo.tgbotapi.extensions.api.bot import dev.inmo.tgbotapi.bot.TelegramBot import dev.inmo.tgbotapi.requests.bot.GetMyCommands +import dev.inmo.tgbotapi.types.commands.BotCommandScope +import dev.inmo.tgbotapi.types.commands.BotCommandScopeDefault -suspend fun TelegramBot.getMyCommands() = execute(GetMyCommands) +suspend fun TelegramBot.getMyCommands( + scope: BotCommandScope = BotCommandScopeDefault, + languageCode: String? = null +) = execute(GetMyCommands(scope, languageCode)) diff --git a/tgbotapi.extensions.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/bot/SetMyCommands.kt b/tgbotapi.extensions.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/bot/SetMyCommands.kt index df5444d62c..d0d8b0a8cc 100644 --- a/tgbotapi.extensions.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/bot/SetMyCommands.kt +++ b/tgbotapi.extensions.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/bot/SetMyCommands.kt @@ -3,11 +3,17 @@ package dev.inmo.tgbotapi.extensions.api.bot import dev.inmo.tgbotapi.bot.TelegramBot import dev.inmo.tgbotapi.requests.bot.SetMyCommands import dev.inmo.tgbotapi.types.BotCommand +import dev.inmo.tgbotapi.types.commands.BotCommandScope +import dev.inmo.tgbotapi.types.commands.BotCommandScopeDefault suspend fun TelegramBot.setMyCommands( - commands: List -) = execute(SetMyCommands(commands)) + commands: List, + scope: BotCommandScope = BotCommandScopeDefault, + languageCode: String? = null +) = execute(SetMyCommands(commands, scope, languageCode)) suspend fun TelegramBot.setMyCommands( - vararg commands: BotCommand -) = setMyCommands(commands.toList()) + vararg commands: BotCommand, + scope: BotCommandScope = BotCommandScopeDefault, + languageCode: String? = null +) = setMyCommands(commands.toList(), scope, languageCode)