1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-12-22 16:47:13 +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`
* `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

View File

@ -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

View File

@ -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<Bot> {
class GetMe : SimpleRequest<ExtendedBot> {
override fun method(): String = "getMe"
override val resultDeserializer: DeserializationStrategy<Bot>
get() = Bot.serializer()
override val resultDeserializer: DeserializationStrategy<ExtendedBot>
get() = ExtendedBot.serializer()
override val requestSerializer: SerializationStrategy<*>
get() = serializer()
}

View File

@ -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<User> {
override fun deserialize(decoder: Decoder): User {
@ -51,17 +69,30 @@ internal object UserSerializer : KSerializer<User> {
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)
}
}
}