diff --git a/CHANGELOG.md b/CHANGELOG.md index d062162adb..b0a50cc6d2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ * `openPeriodField` * `closeDateField` * Field `SendPoll#closeInfo` was added + * Range `openPeriodPollSecondsLimit` was added and used in all `SendPoll` requests for checking income data * `SendQuizPoll` now able to use fields `caption` and `parseMode` for `explanation` functionality * `quizPollExplanationLimit` was added for checking `QuizPoll` explanation size * Field `TextLinkTextSource#url` was added diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/polls/SendPoll.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/polls/SendPoll.kt index 1c09393282..3252747706 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/polls/SendPoll.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/polls/SendPoll.kt @@ -14,6 +14,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.PollCont import com.github.insanusmokrassar.TelegramBotAPI.types.polls.* import com.github.insanusmokrassar.TelegramBotAPI.utils.fullListOfSubSource import com.github.insanusmokrassar.TelegramBotAPI.utils.toMarkdownV2Captions +import com.soywiz.klock.DateTime import kotlinx.serialization.* private val commonResultDeserializer: DeserializationStrategy> = TelegramBotAPIMessageDeserializationStrategyClass() @@ -118,6 +119,16 @@ fun Poll.createRequest( ) } +private fun ScheduledCloseInfo.checkSendData() { + val span = when (this) { + is ExactScheduledCloseInfo -> (closeDateTime - DateTime.now()).seconds + is ApproximateScheduledCloseInfo -> openDuration.seconds + }.toInt() + if (span !in openPeriodPollSecondsLimit) { + error("Duration of autoclose for polls must be in range $openPeriodPollSecondsLimit, but was $span") + } +} + sealed class SendPoll : SendMessageRequest>, ReplyingMarkupSendMessageRequest> { abstract val question: String @@ -172,6 +183,7 @@ data class SendRegularPoll( init { checkPollInfo(question, options) + closeInfo ?.checkSendData() } } @@ -216,6 +228,7 @@ data class SendQuizPoll( init { checkPollInfo(question, options) + closeInfo ?.checkSendData() val correctOptionIdRange = 0 .. options.size if (correctOptionId !in correctOptionIdRange) { throw IllegalArgumentException("Correct option id must be in range of $correctOptionIdRange, but actual " + diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/Common.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/Common.kt index 69229daa23..68bd6b2fe2 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/Common.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/Common.kt @@ -57,6 +57,8 @@ val botCommandsLimit = 0 .. 100 val quizPollExplanationLimit = 0 .. 200 +val openPeriodPollSecondsLimit = 5 .. 600 + const val chatIdField = "chat_id" const val messageIdField = "message_id" const val updateIdField = "update_id"