1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-22 08:13:47 +00:00

rewrite work with Bot class

This commit is contained in:
InsanusMokrassar 2020-01-23 22:32:45 +06:00
parent 7a880ba2bd
commit 0c11be7fe4
4 changed files with 50 additions and 19 deletions

View File

@ -29,8 +29,10 @@
* `QuizKeyboardButtonPollType` * `QuizKeyboardButtonPollType`
* `User` now is sealed class * `User` now is sealed class
* `CommonUser` was added as representation of default `User` * `CommonUser` was added as representation of default `User`
* `Bot` was added as representation of bot user * `Bot` was added as representation of bot user (it is sealed class)
* `GetMe` now return `Bot` object * `ExtendedBot` with additional info
* `CommonBot` with simple info
* `GetMe` now return `ExtendedBot` object
* Now extension `javaLocale` is extension for `CommonUser` * Now extension `javaLocale` is extension for `CommonUser`
## 0.22.0 ## 0.22.0

View File

@ -10,7 +10,7 @@ moments are describing by official [Telegram Bot API](https://core.telegram.org/
## Compatibility ## 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 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 [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 as soon as possible. All APIs that are not included are presented
@ -96,10 +96,9 @@ val requestsExecutor: RequestsExecutor = ...
requestsExecutor.execute(GetMe()) 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 The result type of [GetMe](https://github.com/InsanusMokrassar/TelegramBotAPI/blob/master/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/GetMe.kt)
[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 request is
`isBot` equal to `true` always. [ExtendedBot](https://github.com/InsanusMokrassar/TelegramBotAPI/blob/master/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/User.kt).
### RequestsExecutor ### RequestsExecutor

View File

@ -1,15 +1,14 @@
package com.github.insanusmokrassar.TelegramBotAPI.requests package com.github.insanusmokrassar.TelegramBotAPI.requests
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.SimpleRequest import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.SimpleRequest
import com.github.insanusmokrassar.TelegramBotAPI.types.Bot import com.github.insanusmokrassar.TelegramBotAPI.types.*
import com.github.insanusmokrassar.TelegramBotAPI.types.User
import kotlinx.serialization.* import kotlinx.serialization.*
@Serializable @Serializable
class GetMe : SimpleRequest<Bot> { class GetMe : SimpleRequest<ExtendedBot> {
override fun method(): String = "getMe" override fun method(): String = "getMe"
override val resultDeserializer: DeserializationStrategy<Bot> override val resultDeserializer: DeserializationStrategy<ExtendedBot>
get() = Bot.serializer() get() = ExtendedBot.serializer()
override val requestSerializer: SerializationStrategy<*> override val requestSerializer: SerializationStrategy<*>
get() = serializer() get() = serializer()
} }

View File

@ -21,8 +21,25 @@ data class CommonUser(
val languageCode: String? = null val languageCode: String? = null
) : User() ) : User()
@Serializable(UserSerializer::class)
sealed class Bot : User()
@Serializable @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, override val id: ChatId,
@SerialName(firstNameField) @SerialName(firstNameField)
override val firstName: String, override val firstName: String,
@ -36,11 +53,12 @@ data class Bot(
val canReadAllGroupMessages: Boolean = false, val canReadAllGroupMessages: Boolean = false,
@SerialName(supportInlineQueriesField) @SerialName(supportInlineQueriesField)
val supportsInlineQueries: Boolean = false val supportsInlineQueries: Boolean = false
) : User() { ) : Bot() {
@SerialName(isBotField) @SerialName(isBotField)
private val isBot = true private val isBot = true
} }
@Serializer(User::class) @Serializer(User::class)
internal object UserSerializer : KSerializer<User> { internal object UserSerializer : KSerializer<User> {
override fun deserialize(decoder: Decoder): User { override fun deserialize(decoder: Decoder): User {
@ -51,17 +69,30 @@ internal object UserSerializer : KSerializer<User> {
CommonUser.serializer(), CommonUser.serializer(),
asJson asJson
) )
else -> Json.nonstrict.fromJson( else -> {
Bot.serializer(), if ((asJson.get(canJoinGroupsField)
asJson ?: 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) { override fun serialize(encoder: Encoder, obj: User) {
when (obj) { when (obj) {
is CommonUser -> CommonUser.serializer().serialize(encoder, 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)
} }
} }
} }