From 4e39f77b5397b84ffc2d18894618608b6c86cf11 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 23 Jan 2020 17:01:13 +0600 Subject: [PATCH] SendRegularPoll, SendQuizPoll --- CHANGELOG.md | 3 + .../TelegramBotAPI/requests/send/SendPoll.kt | 51 ------- .../requests/send/polls/SendPoll.kt | 127 ++++++++++++++++++ .../types/message/content/PollContent.kt | 19 +-- 4 files changed, 140 insertions(+), 60 deletions(-) delete mode 100644 src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/SendPoll.kt create mode 100644 src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/polls/SendPoll.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 15d384b7b4..44772d7c52 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ * `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 +* `SendPoll` was rewritten as sealed class + * `SendRegularPoll` was created and represent `sendPoll` method with type `regular` + * `SendQuizPoll` was created and represent `sendPoll` method with type `quiz` ## 0.22.0 diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/SendPoll.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/SendPoll.kt deleted file mode 100644 index d640043256..0000000000 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/SendPoll.kt +++ /dev/null @@ -1,51 +0,0 @@ -package com.github.insanusmokrassar.TelegramBotAPI.requests.send - -import com.github.insanusmokrassar.TelegramBotAPI.requests.send.abstracts.ReplyingMarkupSendMessageRequest -import com.github.insanusmokrassar.TelegramBotAPI.requests.send.abstracts.SendMessageRequest -import com.github.insanusmokrassar.TelegramBotAPI.types.* -import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.KeyboardMarkup -import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.ContentMessage -import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.TelegramBotAPIMessageDeserializationStrategyClass -import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.PollContent -import kotlinx.serialization.* - -private val commonResultDeserializer: DeserializationStrategy> - = TelegramBotAPIMessageDeserializationStrategyClass() - -@Serializable -data class SendPoll( - @SerialName(chatIdField) - override val chatId: ChatIdentifier, - @SerialName(questionField) - val question: String, - @SerialName(optionsField) - val options: List, - @SerialName(disableNotificationField) - override val disableNotification: Boolean = false, - @SerialName(replyToMessageIdField) - override val replyToMessageId: MessageIdentifier? = null, - @SerialName(replyMarkupField) - override val replyMarkup: KeyboardMarkup? = null -) : SendMessageRequest>, - ReplyingMarkupSendMessageRequest> { - - init { - if (question.length !in pollQuestionTextLength) { - throw IllegalArgumentException("The length of questions for polls must be in $pollQuestionTextLength range, but was ${question.length}") - } - options.forEach { - if (it.length !in pollOptionTextLength) { - throw IllegalArgumentException("The length of question option text for polls must be in $pollOptionTextLength range, but was ${it.length}") - } - } - if (options.size !in pollOptionsLimit) { - throw IllegalArgumentException("The amount of question options for polls must be in $pollOptionsLimit range, but was ${options.size}") - } - } - - override fun method(): String = "sendPoll" - override val resultDeserializer: DeserializationStrategy> - get() = commonResultDeserializer - override val requestSerializer: SerializationStrategy<*> - get() = serializer() -} \ No newline at end of file diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/polls/SendPoll.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/polls/SendPoll.kt new file mode 100644 index 0000000000..cdebf73d11 --- /dev/null +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/polls/SendPoll.kt @@ -0,0 +1,127 @@ +package com.github.insanusmokrassar.TelegramBotAPI.requests.send.polls + +import com.github.insanusmokrassar.TelegramBotAPI.requests.send.abstracts.ReplyingMarkupSendMessageRequest +import com.github.insanusmokrassar.TelegramBotAPI.requests.send.abstracts.SendMessageRequest +import com.github.insanusmokrassar.TelegramBotAPI.types.* +import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.KeyboardMarkup +import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.ContentMessage +import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.TelegramBotAPIMessageDeserializationStrategyClass +import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.PollContent +import kotlinx.serialization.* + +private val commonResultDeserializer: DeserializationStrategy> = TelegramBotAPIMessageDeserializationStrategyClass() + +private fun checkPollInfo( + question: String, + options: List +) { + if (question.length !in pollQuestionTextLength) { + throw IllegalArgumentException("The length of questions for polls must be in $pollQuestionTextLength range, but was ${question.length}") + } + options.forEach { + if (it.length !in pollOptionTextLength) { + throw IllegalArgumentException("The length of question option text for polls must be in $pollOptionTextLength range, but was ${it.length}") + } + } + if (options.size !in pollOptionsLimit) { + throw IllegalArgumentException("The amount of question options for polls must be in $pollOptionsLimit range, but was ${options.size}") + } +} + +fun SendPoll( + chatId: ChatIdentifier, + question: String, + options: List, + isAnonymous: Boolean = true, + isClosed: Boolean = false, + disableNotification: Boolean = false, + replyToMessageId: MessageIdentifier? = null, + replyMarkup: KeyboardMarkup? = null +) = SendRegularPoll( + chatId, + question, + options, + isAnonymous, + isClosed, + disableNotification = disableNotification, + replyToMessageId = replyToMessageId, + replyMarkup = replyMarkup +) + +sealed class SendPoll : SendMessageRequest>, + ReplyingMarkupSendMessageRequest> { + abstract val question: String + abstract val options: List + abstract val isAnonymous: Boolean + abstract val isClosed: Boolean + abstract val type: String + + override fun method(): String = "sendPoll" + override val resultDeserializer: DeserializationStrategy> + get() = commonResultDeserializer +} + +@Serializable +data class SendRegularPoll( + @SerialName(chatIdField) + override val chatId: ChatIdentifier, + @SerialName(questionField) + override val question: String, + @SerialName(optionsField) + override val options: List, + @SerialName(isAnonymousField) + override val isAnonymous: Boolean = true, + @SerialName(isClosedField) + override val isClosed: Boolean = false, + @SerialName(allowsMultipleAnswersField) + val allowMultipleAnswers: Boolean = false, + @SerialName(disableNotificationField) + override val disableNotification: Boolean = false, + @SerialName(replyToMessageIdField) + override val replyToMessageId: MessageIdentifier? = null, + @SerialName(replyMarkupField) + override val replyMarkup: KeyboardMarkup? = null +) : SendPoll() { + override val type: String = regularPollType + override val requestSerializer: SerializationStrategy<*> + get() = serializer() + + init { + checkPollInfo(question, options) + } +} + +@Serializable +data class SendQuizPoll( + @SerialName(chatIdField) + override val chatId: ChatIdentifier, + @SerialName(questionField) + override val question: String, + @SerialName(optionsField) + override val options: List, + @SerialName(correctOptionIdField) + val correctOptionId: Int, + @SerialName(isAnonymousField) + override val isAnonymous: Boolean = true, + @SerialName(isClosedField) + override val isClosed: Boolean = false, + @SerialName(disableNotificationField) + override val disableNotification: Boolean = false, + @SerialName(replyToMessageIdField) + override val replyToMessageId: MessageIdentifier? = null, + @SerialName(replyMarkupField) + override val replyMarkup: KeyboardMarkup? = null +) : SendPoll() { + override val type: String = quizPollType + override val requestSerializer: SerializationStrategy<*> + get() = serializer() + + init { + checkPollInfo(question, options) + val correctOptionIdRange = 0 .. options.size + if (correctOptionId !in correctOptionIdRange) { + throw IllegalArgumentException("Correct option id must be in range of $correctOptionIdRange, but actual " + + "value is $correctOptionId") + } + } +} \ No newline at end of file diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/content/PollContent.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/content/PollContent.kt index 8f89750809..bc53c44fc2 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/content/PollContent.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/content/PollContent.kt @@ -1,7 +1,7 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.message.content import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.Request -import com.github.insanusmokrassar.TelegramBotAPI.requests.send.SendPoll +import com.github.insanusmokrassar.TelegramBotAPI.requests.send.polls.SendPoll import com.github.insanusmokrassar.TelegramBotAPI.types.ChatIdentifier import com.github.insanusmokrassar.TelegramBotAPI.types.MessageIdentifier import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.KeyboardMarkup @@ -17,12 +17,13 @@ data class PollContent( disableNotification: Boolean, replyToMessageId: MessageIdentifier?, replyMarkup: KeyboardMarkup? - ): Request> = SendPoll( - chatId, - poll.question, - poll.options.map { it.text }, - disableNotification, - replyToMessageId, - replyMarkup - ) + ): Request> = + SendPoll( + chatId, + poll.question, + poll.options.map { it.text }, + disableNotification, + replyToMessageId, + replyMarkup + ) }