From ec70813e499a9081ffea1d00b6144f1409a8cb9b Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 30 Mar 2020 21:52:00 +0600 Subject: [PATCH] commands handling added --- CHANGELOG.md | 7 ++++++ .../TelegramBotAPI/extensions/api/GetMe.kt | 8 ++++-- .../extensions/api/bot/GetMe.kt | 6 +++++ .../extensions/api/bot/GetMyCommands.kt | 6 +++++ .../extensions/api/bot/SetMyCommands.kt | 10 ++++++++ .../TelegramBotAPI/requests/GetMe.kt | 19 ++++++-------- .../TelegramBotAPI/requests/bot/GetMe.kt | 14 +++++++++++ .../requests/bot/GetMyCommands.kt | 17 +++++++++++++ .../requests/bot/SetMyCommands.kt | 25 +++++++++++++++++++ .../TelegramBotAPI/types/Common.kt | 2 ++ 10 files changed, 101 insertions(+), 13 deletions(-) create mode 100644 TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/bot/GetMe.kt create mode 100644 TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/bot/GetMyCommands.kt create mode 100644 TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/bot/SetMyCommands.kt create mode 100644 TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/bot/GetMe.kt create mode 100644 TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/bot/GetMyCommands.kt create mode 100644 TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/bot/SetMyCommands.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 2b17fa808e..9bc7cee0f0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,8 +32,15 @@ * Class `Dice` was added (type [dice](https://core.telegram.org/bots/api#dice)) * Class `DiceContent` was added (for including it in [message](https://core.telegram.org/bots/api#message) object) * `BotCommand` was added + * `GetMyCommands` request was added + * `SetMyCommands` request was added + * `GetMe` now is object instead of class + * `GetMe` was replaced into package `com.github.insanusmokrassar.TelegramBotAPI.requests.bot.GetMe` * `TelegramBotAPI-extensions-api`: * Extensions `sendDice` was added + * Extension `getMyCommands` request was added + * Extension `setMyCommands` request was added + * Extension `getMe` was replaced into package `com.github.insanusmokrassar.TelegramBotAPI.extensions.api.bot.GetMeKt.getMe` ### 0.25.1 diff --git a/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/GetMe.kt b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/GetMe.kt index d0a48bdbf4..889d07a6b0 100644 --- a/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/GetMe.kt +++ b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/GetMe.kt @@ -1,6 +1,10 @@ package com.github.insanusmokrassar.TelegramBotAPI.extensions.api import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor -import com.github.insanusmokrassar.TelegramBotAPI.requests.GetMe +import com.github.insanusmokrassar.TelegramBotAPI.extensions.api.bot.getMe -suspend fun RequestsExecutor.getMe() = execute(GetMe()) \ No newline at end of file +@Deprecated( + "Replaced", + ReplaceWith("getMe", "com.github.insanusmokrassar.TelegramBotAPI.extensions.api.bot.GetMeKt.getMe") +) +suspend fun RequestsExecutor.getMe() = getMe() \ No newline at end of file diff --git a/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/bot/GetMe.kt b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/bot/GetMe.kt new file mode 100644 index 0000000000..66d9b4512d --- /dev/null +++ b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/bot/GetMe.kt @@ -0,0 +1,6 @@ +package com.github.insanusmokrassar.TelegramBotAPI.extensions.api.bot + +import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor +import com.github.insanusmokrassar.TelegramBotAPI.requests.bot.GetMe + +suspend fun RequestsExecutor.getMe() = execute(GetMe) diff --git a/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/bot/GetMyCommands.kt b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/bot/GetMyCommands.kt new file mode 100644 index 0000000000..41a9bef0f7 --- /dev/null +++ b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/bot/GetMyCommands.kt @@ -0,0 +1,6 @@ +package com.github.insanusmokrassar.TelegramBotAPI.extensions.api.bot + +import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor +import com.github.insanusmokrassar.TelegramBotAPI.requests.bot.GetMyCommands + +suspend fun RequestsExecutor.getMyCommands() = execute(GetMyCommands) diff --git a/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/bot/SetMyCommands.kt b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/bot/SetMyCommands.kt new file mode 100644 index 0000000000..d8da9a63bf --- /dev/null +++ b/TelegramBotAPI-extensions-api/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/api/bot/SetMyCommands.kt @@ -0,0 +1,10 @@ +package com.github.insanusmokrassar.TelegramBotAPI.extensions.api.bot + +import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor +import com.github.insanusmokrassar.TelegramBotAPI.requests.bot.GetMyCommands +import com.github.insanusmokrassar.TelegramBotAPI.requests.bot.SetMyCommands +import com.github.insanusmokrassar.TelegramBotAPI.types.BotCommand + +suspend fun RequestsExecutor.setMyCommands( + commands: List +) = execute(SetMyCommands(commands)) diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/GetMe.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/GetMe.kt index b0cd428c8c..4c4dab284b 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/GetMe.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/GetMe.kt @@ -1,14 +1,11 @@ package com.github.insanusmokrassar.TelegramBotAPI.requests -import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.SimpleRequest -import com.github.insanusmokrassar.TelegramBotAPI.types.ExtendedBot -import kotlinx.serialization.* +import com.github.insanusmokrassar.TelegramBotAPI.requests.bot.GetMe -@Serializable -class GetMe : SimpleRequest { - override fun method(): String = "getMe" - override val resultDeserializer: DeserializationStrategy - get() = ExtendedBot.serializer() - override val requestSerializer: SerializationStrategy<*> - get() = serializer() -} +@Deprecated( + "Replaced", + ReplaceWith( + "GetMe", "com.github.insanusmokrassar.TelegramBotAPI.requests.bot.GetMe" + ) +) +typealias GetMe = GetMe diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/bot/GetMe.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/bot/GetMe.kt new file mode 100644 index 0000000000..b200c34dbd --- /dev/null +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/bot/GetMe.kt @@ -0,0 +1,14 @@ +package com.github.insanusmokrassar.TelegramBotAPI.requests.bot + +import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.SimpleRequest +import com.github.insanusmokrassar.TelegramBotAPI.types.ExtendedBot +import kotlinx.serialization.* + +@Serializable +object GetMe : SimpleRequest { + override fun method(): String = "getMe" + override val resultDeserializer: DeserializationStrategy + get() = ExtendedBot.serializer() + override val requestSerializer: SerializationStrategy<*> + get() = serializer() +} diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/bot/GetMyCommands.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/bot/GetMyCommands.kt new file mode 100644 index 0000000000..a7a3c764bb --- /dev/null +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/bot/GetMyCommands.kt @@ -0,0 +1,17 @@ +package com.github.insanusmokrassar.TelegramBotAPI.requests.bot + +import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.SimpleRequest +import com.github.insanusmokrassar.TelegramBotAPI.types.BotCommand +import kotlinx.serialization.* +import kotlinx.serialization.builtins.ListSerializer + +private val getMyCommandsSerializer = ListSerializer(BotCommand.serializer()) + +@Serializable +object GetMyCommands : SimpleRequest> { + override fun method(): String = "getMyCommands" + override val resultDeserializer: DeserializationStrategy> + get() = getMyCommandsSerializer + override val requestSerializer: SerializationStrategy<*> + get() = serializer() +} diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/bot/SetMyCommands.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/bot/SetMyCommands.kt new file mode 100644 index 0000000000..a5dbfd2dc0 --- /dev/null +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/bot/SetMyCommands.kt @@ -0,0 +1,25 @@ +package com.github.insanusmokrassar.TelegramBotAPI.requests.bot + +import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.SimpleRequest +import com.github.insanusmokrassar.TelegramBotAPI.types.* +import kotlinx.serialization.* +import kotlinx.serialization.builtins.ListSerializer +import kotlinx.serialization.builtins.serializer + +@Serializable +class SetMyCommands( + @SerialName(botCommandsField) + val commands: List +) : SimpleRequest { + override fun method(): String = "getMyCommands" + override val resultDeserializer: DeserializationStrategy + get() = Boolean.serializer() + override val requestSerializer: SerializationStrategy<*> + get() = serializer() + + init { + if (commands.size !in botCommandsLimit) { + error("Bot commands list size able to be in range $botCommandsLimit, but incoming size is ${commands.size}") + } + } +} diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/Common.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/Common.kt index bfff406189..f5cfb8ae78 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/Common.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/Common.kt @@ -50,6 +50,7 @@ val diceResultLimit = 1 .. 6 val botCommandLimit = 1 .. 32 val botCommandDescriptionLimit = 3 .. 256 +val botCommandsLimit = 0 .. 100 const val chatIdField = "chat_id" const val messageIdField = "message_id" @@ -171,6 +172,7 @@ const val inputMessageContentField = "input_message_content" const val hideUrlField = "hide_url" const val botCommandField = "command" +const val botCommandsField = "commands" const val isMemberField = "is_member" const val canSendMessagesField = "can_send_messages"