From 0c11be7fe4e97b2bab4a4891bd4027cd23a6b1fa Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 23 Jan 2020 22:32:45 +0600 Subject: [PATCH] rewrite work with Bot class --- CHANGELOG.md | 6 ++- README.md | 9 ++-- .../TelegramBotAPI/requests/GetMe.kt | 9 ++-- .../TelegramBotAPI/types/User.kt | 45 ++++++++++++++++--- 4 files changed, 50 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ba98e80970..0266904fce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,8 +29,10 @@ * `QuizKeyboardButtonPollType` * `User` now is sealed class * `CommonUser` was added as representation of default `User` - * `Bot` was added as representation of bot user - * `GetMe` now return `Bot` object + * `Bot` was added as representation of bot user (it is sealed class) + * `ExtendedBot` with additional info + * `CommonBot` with simple info + * `GetMe` now return `ExtendedBot` object * Now extension `javaLocale` is extension for `CommonUser` ## 0.22.0 diff --git a/README.md b/README.md index 23033972eb..5adb92ff4e 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ moments are describing by official [Telegram Bot API](https://core.telegram.org/ ## Compatibility -This version compatible with [31th of December 2019 update of TelegramBotAPI (version 4.5)](https://core.telegram.org/bots/api#december-31-2019). +This version compatible with [23th of January 2020 update of TelegramBotAPI (version 4.6)](https://core.telegram.org/bots/api#january-23-2020). There is Telegram Passport API exception of implemented functionality, which was presented in [August 2018 update of TelegramBotAPI](https://core.telegram.org/bots/api#august-27-2018) update. It will be implemented as soon as possible. All APIs that are not included are presented @@ -96,10 +96,9 @@ val requestsExecutor: RequestsExecutor = ... requestsExecutor.execute(GetMe()) ``` -The result type of [GetMe](https://github.com/InsanusMokrassar/TelegramBotAPI/blob/master/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/GetMe.kt) request is -[User](https://github.com/InsanusMokrassar/TelegramBotAPI/blob/master/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/User.kt). In fact, in this result must contain -`isBot` equal to `true` always. - +The result type of [GetMe](https://github.com/InsanusMokrassar/TelegramBotAPI/blob/master/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/GetMe.kt) +request is +[ExtendedBot](https://github.com/InsanusMokrassar/TelegramBotAPI/blob/master/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/User.kt). ### RequestsExecutor diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/GetMe.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/GetMe.kt index cdd3848747..2f19df4909 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/GetMe.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/GetMe.kt @@ -1,15 +1,14 @@ package com.github.insanusmokrassar.TelegramBotAPI.requests import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.SimpleRequest -import com.github.insanusmokrassar.TelegramBotAPI.types.Bot -import com.github.insanusmokrassar.TelegramBotAPI.types.User +import com.github.insanusmokrassar.TelegramBotAPI.types.* import kotlinx.serialization.* @Serializable -class GetMe : SimpleRequest { +class GetMe : SimpleRequest { override fun method(): String = "getMe" - override val resultDeserializer: DeserializationStrategy - get() = Bot.serializer() + override val resultDeserializer: DeserializationStrategy + get() = ExtendedBot.serializer() override val requestSerializer: SerializationStrategy<*> get() = serializer() } \ No newline at end of file diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/User.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/User.kt index e19ca3aaca..d23a1ce1d0 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/User.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/User.kt @@ -21,8 +21,25 @@ data class CommonUser( val languageCode: String? = null ) : User() +@Serializable(UserSerializer::class) +sealed class Bot : User() + @Serializable -data class Bot( +data class CommonBot( + override val id: ChatId, + @SerialName(firstNameField) + override val firstName: String, + @SerialName(lastNameField) + override val lastName: String = "", + @SerialName(usernameField) + override val username: Username? = null +) : Bot() { + @SerialName(isBotField) + private val isBot = true +} + +@Serializable +data class ExtendedBot( override val id: ChatId, @SerialName(firstNameField) override val firstName: String, @@ -36,11 +53,12 @@ data class Bot( val canReadAllGroupMessages: Boolean = false, @SerialName(supportInlineQueriesField) val supportsInlineQueries: Boolean = false -) : User() { +) : Bot() { @SerialName(isBotField) private val isBot = true } + @Serializer(User::class) internal object UserSerializer : KSerializer { override fun deserialize(decoder: Decoder): User { @@ -51,17 +69,30 @@ internal object UserSerializer : KSerializer { CommonUser.serializer(), asJson ) - else -> Json.nonstrict.fromJson( - Bot.serializer(), - asJson - ) + else -> { + if ((asJson.get(canJoinGroupsField) + ?: asJson.get(canReadAllGroupMessagesField) + ?: asJson.get(supportInlineQueriesField)) != null + ) { + Json.nonstrict.fromJson( + ExtendedBot.serializer(), + asJson + ) + } else { + Json.nonstrict.fromJson( + CommonBot.serializer(), + asJson + ) + } + } } } override fun serialize(encoder: Encoder, obj: User) { when (obj) { is CommonUser -> CommonUser.serializer().serialize(encoder, obj) - is Bot -> Bot.serializer().serialize(encoder, obj) + is CommonBot -> CommonBot.serializer().serialize(encoder, obj) + is ExtendedBot -> ExtendedBot.serializer().serialize(encoder, obj) } } }