diff --git a/CHANGELOG.md b/CHANGELOG.md index 18bc057097..bf11583178 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ * `SendQuizPoll` was created and represent `sendPoll` method with type `quiz` * `language` field in PreTextSource now correctly passed from telegram MessageEntities * `PollAnswer` type was added +* `Poll#createRequest` extension was added ## 0.22.0 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 index cdebf73d11..b4b390f6e2 100644 --- 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 @@ -7,6 +7,7 @@ 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 com.github.insanusmokrassar.TelegramBotAPI.types.polls.* import kotlinx.serialization.* private val commonResultDeserializer: DeserializationStrategy> = TelegramBotAPIMessageDeserializationStrategyClass() @@ -48,6 +49,47 @@ fun SendPoll( replyMarkup = replyMarkup ) +fun Poll.createRequest( + chatId: ChatIdentifier, + disableNotification: Boolean = false, + replyToMessageId: MessageIdentifier? = null, + replyMarkup: KeyboardMarkup? = null +) = when (this) { + is RegularPoll -> SendRegularPoll( + chatId, + question, + options.map { it.text }, + isAnonymous, + isClosed, + allowMultipleAnswers, + disableNotification, + replyToMessageId, + replyMarkup + ) + is QuizPoll -> SendQuizPoll( + chatId, + question, + options.map { it.text }, + correctOptionId, + isAnonymous, + isClosed, + disableNotification, + replyToMessageId, + replyMarkup + ) + is UnknownPollType -> SendRegularPoll( + chatId, + question, + options.map { it.text }, + isAnonymous, + isClosed, + false, + disableNotification, + replyToMessageId, + replyMarkup + ) +} + sealed class SendPoll : SendMessageRequest>, ReplyingMarkupSendMessageRequest> { abstract val question: String 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 bc53c44fc2..c60177186d 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 @@ -2,12 +2,14 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.message.content import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.Request import com.github.insanusmokrassar.TelegramBotAPI.requests.send.polls.SendPoll +import com.github.insanusmokrassar.TelegramBotAPI.requests.send.polls.createRequest import com.github.insanusmokrassar.TelegramBotAPI.types.ChatIdentifier import com.github.insanusmokrassar.TelegramBotAPI.types.MessageIdentifier import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.KeyboardMarkup import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.ContentMessage import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.abstracts.MessageContent import com.github.insanusmokrassar.TelegramBotAPI.types.polls.Poll +import com.github.insanusmokrassar.TelegramBotAPI.types.polls.RegularPoll data class PollContent( val poll: Poll @@ -17,13 +19,10 @@ data class PollContent( disableNotification: Boolean, replyToMessageId: MessageIdentifier?, replyMarkup: KeyboardMarkup? - ): Request> = - SendPoll( - chatId, - poll.question, - poll.options.map { it.text }, - disableNotification, - replyToMessageId, - replyMarkup - ) + ): Request> = poll.createRequest( + chatId, + disableNotification, + replyToMessageId, + replyMarkup + ) } diff --git a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/polls/Poll.kt b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/polls/Poll.kt index cf142a16a9..c0e198528e 100644 --- a/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/polls/Poll.kt +++ b/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/polls/Poll.kt @@ -11,7 +11,7 @@ sealed class Poll { abstract val question: String abstract val options: List abstract val votesCount: Int - abstract val closed: Boolean + abstract val isClosed: Boolean abstract val isAnonymous: Boolean } @@ -26,7 +26,7 @@ data class UnknownPollType( @SerialName(totalVoterCountField) override val votesCount: Int, @SerialName(isClosedField) - override val closed: Boolean = false, + override val isClosed: Boolean = false, @SerialName(isAnonymousField) override val isAnonymous: Boolean = false, val raw: String @@ -43,7 +43,7 @@ data class RegularPoll( @SerialName(totalVoterCountField) override val votesCount: Int, @SerialName(isClosedField) - override val closed: Boolean = false, + override val isClosed: Boolean = false, @SerialName(isAnonymousField) override val isAnonymous: Boolean = false, @SerialName(allowsMultipleAnswersField) @@ -60,12 +60,12 @@ data class QuizPoll( override val options: List, @SerialName(totalVoterCountField) override val votesCount: Int, - @SerialName(isClosedField) - override val closed: Boolean = false, - @SerialName(isAnonymousField) - override val isAnonymous: Boolean = false, @SerialName(correctOptionIdField) - val correctOptionId: Boolean = false + val correctOptionId: Int, + @SerialName(isClosedField) + override val isClosed: Boolean = false, + @SerialName(isAnonymousField) + override val isAnonymous: Boolean = false ) : Poll() @Serializer(Poll::class)