1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-25 11:38:45 +00:00

add SendPoll

This commit is contained in:
InsanusMokrassar 2019-04-16 16:16:13 +08:00
parent e2267c1cb4
commit d7bbb6dd85
4 changed files with 55 additions and 2 deletions

View File

@ -5,7 +5,8 @@
* Type `PollOption` and `AnonymousPollOption` added * Type `PollOption` and `AnonymousPollOption` added
* Type `Poll` added * Type `Poll` added
* Type `PollUpdate` added and implemented in `RawUpdate`. Now `PollUpdate` can be retrieved from `RawUpdate` * Type `PollUpdate` added and implemented in `RawUpdate`. Now `PollUpdate` can be retrieved from `RawUpdate`
* type `PollContent` added - now it can be a value of `ContentMessage#content` * Type `PollContent` added - now it can be a value of `ContentMessage#content`
* Request `SendPoll` added and `PollContent#createResend` now use it
## 0.12.0 Webhooks ## 0.12.0 Webhooks

View File

@ -0,0 +1,43 @@
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.RawMessage
import kotlinx.serialization.KSerializer
import kotlinx.serialization.SerialName
data class SendPoll(
@SerialName(chatIdField)
override val chatId: ChatIdentifier,
@SerialName(questionField)
val question: String,
@SerialName(optionsField)
val options: List<String>,
@SerialName(disableNotificationField)
override val disableNotification: Boolean = false,
@SerialName(replyToMessageIdField)
override val replyToMessageId: MessageIdentifier? = null,
@SerialName(replyMarkupField)
override val replyMarkup: KeyboardMarkup? = null
) : SendMessageRequest<RawMessage>,
ReplyingMarkupSendMessageRequest<RawMessage> {
init {
if (question.length !in pollQuectionTextLength) {
throw IllegalArgumentException("The length of questions for polls must be in $pollQuectionTextLength 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 fun resultSerializer(): KSerializer<RawMessage> = RawMessage.serializer()
}

View File

@ -32,6 +32,7 @@ val invoicePayloadBytesLimit = 1 until 128
val pollOptionTextLength = 1 .. 100 val pollOptionTextLength = 1 .. 100
val pollQuectionTextLength = 1 until 256 val pollQuectionTextLength = 1 until 256
val pollOptionsLimit = 2 .. 10
val livePeriodLimit = 60 .. 86400 val livePeriodLimit = 60 .. 86400

View File

@ -2,6 +2,7 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.message.content
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.Request import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.Request
import com.github.insanusmokrassar.TelegramBotAPI.requests.send.SendMessage import com.github.insanusmokrassar.TelegramBotAPI.requests.send.SendMessage
import com.github.insanusmokrassar.TelegramBotAPI.requests.send.SendPoll
import com.github.insanusmokrassar.TelegramBotAPI.types.ChatIdentifier import com.github.insanusmokrassar.TelegramBotAPI.types.ChatIdentifier
import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.MessageEntity import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.MessageEntity
import com.github.insanusmokrassar.TelegramBotAPI.types.MessageIdentifier import com.github.insanusmokrassar.TelegramBotAPI.types.MessageIdentifier
@ -21,5 +22,12 @@ data class PollContent(
disableNotification: Boolean, disableNotification: Boolean,
replyToMessageId: MessageIdentifier?, replyToMessageId: MessageIdentifier?,
replyMarkup: KeyboardMarkup? replyMarkup: KeyboardMarkup?
): Request<RawMessage> = TODO() ): Request<RawMessage> = SendPoll(
chatId,
poll.question,
poll.options.map { it.text },
disableNotification,
replyToMessageId,
replyMarkup
)
} }