mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2024-11-22 16:23:48 +00:00
extend polls
This commit is contained in:
parent
229334e781
commit
423efafa04
@ -2,6 +2,11 @@
|
|||||||
|
|
||||||
## 0.23.0 TelegramBotAPI 4.6
|
## 0.23.0 TelegramBotAPI 4.6
|
||||||
|
|
||||||
|
* `Poll` now is sealed class
|
||||||
|
* `RegularPoll` type was added to represent polls with type `regular`
|
||||||
|
* `QuizPoll` type was added to represent polls with type `quiz`
|
||||||
|
* `UnknownPollType` type was added to represent polls which are unknown in current version
|
||||||
|
|
||||||
## 0.22.0
|
## 0.22.0
|
||||||
|
|
||||||
* **`KtorCallFactory` must return `HttpStatement` instead of `HttpClientCall`**
|
* **`KtorCallFactory` must return `HttpStatement` instead of `HttpClientCall`**
|
||||||
|
@ -90,6 +90,9 @@ const val lastErrorDateField = "last_error_date"
|
|||||||
const val lastErrorMessageField = "last_error_message"
|
const val lastErrorMessageField = "last_error_message"
|
||||||
const val votesCountField = "voter_count"
|
const val votesCountField = "voter_count"
|
||||||
const val isClosedField = "is_closed"
|
const val isClosedField = "is_closed"
|
||||||
|
const val correctOptionIdField = "correct_option_id"
|
||||||
|
const val allowsMultipleAnswersField = "allows_multiple_answers"
|
||||||
|
const val isAnonymousField = "is_anonymous"
|
||||||
const val loginUrlField = "login_url"
|
const val loginUrlField = "login_url"
|
||||||
const val forwardTextField = "forward_text"
|
const val forwardTextField = "forward_text"
|
||||||
const val botUsernameField = "bot_username"
|
const val botUsernameField = "bot_username"
|
||||||
@ -273,3 +276,6 @@ const val mediaField = "media"
|
|||||||
const val disableEditMessageField = "disable_edit_message"
|
const val disableEditMessageField = "disable_edit_message"
|
||||||
const val scoreField = "score"
|
const val scoreField = "score"
|
||||||
const val forceField = "force"
|
const val forceField = "force"
|
||||||
|
|
||||||
|
const val regularPollType = "regular"
|
||||||
|
const val quizPollType = "quiz"
|
||||||
|
@ -1,17 +1,108 @@
|
|||||||
package com.github.insanusmokrassar.TelegramBotAPI.types.polls
|
package com.github.insanusmokrassar.TelegramBotAPI.types.polls
|
||||||
|
|
||||||
import com.github.insanusmokrassar.TelegramBotAPI.types.*
|
import com.github.insanusmokrassar.TelegramBotAPI.types.*
|
||||||
import kotlinx.serialization.SerialName
|
import kotlinx.serialization.*
|
||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.internal.ArrayListSerializer
|
||||||
|
import kotlinx.serialization.json.*
|
||||||
|
|
||||||
|
@Serializable(PollSerializer::class)
|
||||||
|
sealed class Poll {
|
||||||
|
abstract val id: PollIdentifier
|
||||||
|
abstract val question: String
|
||||||
|
abstract val options: List<PollOption>
|
||||||
|
abstract val closed: Boolean
|
||||||
|
abstract val isAnonymous: Boolean
|
||||||
|
}
|
||||||
|
|
||||||
@Serializable
|
@Serializable
|
||||||
data class Poll(
|
data class UnknownPollType(
|
||||||
@SerialName(idField)
|
@SerialName(idField)
|
||||||
val id: PollIdentifier,
|
override val id: PollIdentifier,
|
||||||
@SerialName(questionField)
|
@SerialName(questionField)
|
||||||
val question: String,
|
override val question: String,
|
||||||
@SerialName(optionsField)
|
@SerialName(optionsField)
|
||||||
val options: List<PollOption>,
|
override val options: List<PollOption>,
|
||||||
@SerialName(isClosedField)
|
@SerialName(isClosedField)
|
||||||
val closed: Boolean = false
|
override val closed: Boolean = false,
|
||||||
)
|
@SerialName(isAnonymousField)
|
||||||
|
override val isAnonymous: Boolean = false,
|
||||||
|
val raw: String
|
||||||
|
) : Poll()
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
data class RegularPoll(
|
||||||
|
@SerialName(idField)
|
||||||
|
override val id: PollIdentifier,
|
||||||
|
@SerialName(questionField)
|
||||||
|
override val question: String,
|
||||||
|
@SerialName(optionsField)
|
||||||
|
override val options: List<PollOption>,
|
||||||
|
@SerialName(isClosedField)
|
||||||
|
override val closed: Boolean = false,
|
||||||
|
@SerialName(isAnonymousField)
|
||||||
|
override val isAnonymous: Boolean = false,
|
||||||
|
@SerialName(allowsMultipleAnswersField)
|
||||||
|
val allowMultipleAnswers: Boolean = false
|
||||||
|
) : Poll()
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
data class QuizPoll(
|
||||||
|
@SerialName(idField)
|
||||||
|
override val id: PollIdentifier,
|
||||||
|
@SerialName(questionField)
|
||||||
|
override val question: String,
|
||||||
|
@SerialName(optionsField)
|
||||||
|
override val options: List<PollOption>,
|
||||||
|
@SerialName(isClosedField)
|
||||||
|
override val closed: Boolean = false,
|
||||||
|
@SerialName(isAnonymousField)
|
||||||
|
override val isAnonymous: Boolean = false,
|
||||||
|
@SerialName(correctOptionIdField)
|
||||||
|
val correctOptionId: Boolean = false
|
||||||
|
) : Poll()
|
||||||
|
|
||||||
|
@Serializer(Poll::class)
|
||||||
|
internal object PollSerializer : KSerializer<Poll> {
|
||||||
|
private val pollOptionsSerializer = ArrayListSerializer(PollOption.serializer())
|
||||||
|
override fun deserialize(decoder: Decoder): Poll {
|
||||||
|
val asJson = JsonObjectSerializer.deserialize(decoder)
|
||||||
|
|
||||||
|
return when (asJson.getPrimitive(typeField).content) {
|
||||||
|
regularPollType -> Json.nonstrict.fromJson(
|
||||||
|
RegularPoll.serializer(),
|
||||||
|
asJson
|
||||||
|
)
|
||||||
|
quizPollType -> Json.nonstrict.fromJson(
|
||||||
|
QuizPoll.serializer(),
|
||||||
|
asJson
|
||||||
|
)
|
||||||
|
else -> UnknownPollType(
|
||||||
|
asJson.getPrimitive(idField).content,
|
||||||
|
asJson.getPrimitive(questionField).content,
|
||||||
|
Json.nonstrict.fromJson(
|
||||||
|
pollOptionsSerializer,
|
||||||
|
asJson.getArray(optionsField)
|
||||||
|
),
|
||||||
|
asJson.getPrimitiveOrNull(isClosedField) ?.booleanOrNull ?: false,
|
||||||
|
asJson.getPrimitiveOrNull(isAnonymousField) ?.booleanOrNull ?: true,
|
||||||
|
asJson.toString()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun serialize(encoder: Encoder, obj: Poll) {
|
||||||
|
val asJson = when (obj) {
|
||||||
|
is RegularPoll -> Json.nonstrict.toJson(RegularPoll.serializer(), obj)
|
||||||
|
is QuizPoll -> Json.nonstrict.toJson(QuizPoll.serializer(), obj)
|
||||||
|
is UnknownPollType -> throw IllegalArgumentException("Currently unable to correctly serialize object of poll $obj")
|
||||||
|
}
|
||||||
|
val resultJson = JsonObject(
|
||||||
|
asJson.jsonObject + (typeField to when (obj) {
|
||||||
|
is RegularPoll -> JsonPrimitive(regularPollType)
|
||||||
|
is QuizPoll -> JsonPrimitive(quizPollType)
|
||||||
|
is UnknownPollType -> throw IllegalArgumentException("Currently unable to correctly serialize object of poll $obj")
|
||||||
|
})
|
||||||
|
)
|
||||||
|
JsonObjectSerializer.serialize(encoder, resultJson)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user