Skip to content

Commit 0550fdf

Browse files
committed
Rewrite SendPoll to Kotlin
1 parent 53e4ea3 commit 0550fdf

File tree

3 files changed

+163
-71
lines changed

3 files changed

+163
-71
lines changed

library/src/main/java/com/pengrad/telegrambot/request/SendPoll.java

Lines changed: 0 additions & 71 deletions
This file was deleted.
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package com.pengrad.telegrambot.request
2+
3+
import com.pengrad.telegrambot.model.MessageEntity
4+
import com.pengrad.telegrambot.model.Poll
5+
import com.pengrad.telegrambot.model.request.InputPollOption
6+
import com.pengrad.telegrambot.model.request.ParseMode
7+
import com.pengrad.telegrambot.utility.kotlin.checkDeprecatedConstructorParameters
8+
import com.pengrad.telegrambot.utility.kotlin.optionalRequestParameter
9+
import com.pengrad.telegrambot.utility.kotlin.requestParameter
10+
11+
class SendPoll private constructor(
12+
chatId: Long? = null,
13+
channelUsername: String? = null,
14+
15+
question: String,
16+
options: List<InputPollOption>
17+
) : KAbstractSendRequest<SendPoll>(
18+
chatId = chatId,
19+
channelUsername = channelUsername,
20+
) {
21+
22+
constructor(
23+
chatId: Long,
24+
question: String,
25+
options: List<InputPollOption>
26+
) : this(
27+
chatId = chatId,
28+
channelUsername = null,
29+
question = question,
30+
options = options
31+
)
32+
33+
constructor(
34+
channelUsername: String,
35+
question: String,
36+
options: List<InputPollOption>
37+
) : this(
38+
chatId = null,
39+
channelUsername = channelUsername,
40+
question = question,
41+
options = options
42+
)
43+
44+
@Deprecated("Use constructor with chatId or channelUsername instead", ReplaceWith("SendPoll(chatId, question, options)"))
45+
constructor(
46+
chatId: Any,
47+
question: String,
48+
vararg options: InputPollOption
49+
) : this(
50+
chatId = (chatId as? Number)?.toLong(),
51+
channelUsername = chatId as? String,
52+
question = question,
53+
options = options.toList()
54+
) {
55+
checkDeprecatedConstructorParameters()
56+
}
57+
58+
val question: String by requestParameter(question)
59+
val options: List<InputPollOption> by requestParameter(options)
60+
61+
var type: Poll.Type? by optionalRequestParameter()
62+
var typeRaw: String? by optionalRequestParameter(customParameterName = "type")
63+
64+
var isAnonymous: Boolean? by optionalRequestParameter()
65+
var allowsMultipleAnswers: Boolean? by optionalRequestParameter()
66+
var correctOptionId: Int? by optionalRequestParameter()
67+
68+
var questionParseMode: ParseMode? by optionalRequestParameter()
69+
var questionEntities: List<MessageEntity>? by optionalRequestParameter()
70+
71+
var explanation: String? by optionalRequestParameter()
72+
var explanationParseMode: ParseMode? by optionalRequestParameter()
73+
var explanationEntities: List<MessageEntity>? by optionalRequestParameter()
74+
75+
var openPeriod: Int? by optionalRequestParameter()
76+
var closeDate: Long? by optionalRequestParameter()
77+
var isClosed: Boolean? by optionalRequestParameter()
78+
79+
fun type(type: Poll.Type) = applySelf { this.type = type }
80+
81+
fun type(typeRaw: String) = applySelf { this.typeRaw = typeRaw }
82+
83+
fun isAnonymous(isAnonymous: Boolean) = applySelf { this.isAnonymous = isAnonymous }
84+
85+
fun allowsMultipleAnswers(allowMultipleAnswers: Boolean) = applySelf { this.allowsMultipleAnswers = allowMultipleAnswers }
86+
87+
fun correctOptionId(correctOptionId: Int) = applySelf { this.correctOptionId = correctOptionId }
88+
89+
fun questionParseMode(questionParseMode: ParseMode) = applySelf { this.questionParseMode = questionParseMode }
90+
91+
fun questionEntities(questionEntities: List<MessageEntity>) = applySelf { this.questionEntities = questionEntities }
92+
93+
fun questionEntities(vararg questionEntities: MessageEntity) = questionEntities(questionEntities.toList())
94+
95+
fun explanation(explanation: String) = applySelf { this.explanation = explanation }
96+
97+
fun explanationParseMode(explanationParseMode: ParseMode) = applySelf { this.explanationParseMode = explanationParseMode }
98+
99+
fun explanationEntities(explanationEntities: List<MessageEntity>) = applySelf { this.explanationEntities = explanationEntities }
100+
101+
fun explanationEntities(vararg explanationEntities: MessageEntity) = explanationEntities(explanationEntities.toList())
102+
103+
fun openPeriod(openPeriod: Int) = applySelf { this.openPeriod = openPeriod }
104+
105+
fun closeDate(closeDate: Long) = applySelf { this.closeDate = closeDate }
106+
107+
fun isClosed(isClosed: Boolean) = applySelf { this.isClosed = isClosed }
108+
109+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.pengrad.telegrambot.utility.kotlin.extension.request
2+
3+
import com.pengrad.telegrambot.TelegramAware
4+
import com.pengrad.telegrambot.model.request.InputPollOption
5+
import com.pengrad.telegrambot.request.SendPoll
6+
import com.pengrad.telegrambot.utility.kotlin.extension.execute
7+
8+
inline fun TelegramAware.sendPoll(
9+
chatId: Long,
10+
question: String,
11+
options: List<InputPollOption>,
12+
modifier: SendPoll.() -> Unit = {}
13+
) = this.execute(SendPoll(
14+
chatId = chatId,
15+
question = question,
16+
options = options
17+
), modifier)
18+
19+
inline fun TelegramAware.sendPoll(
20+
chatId: Long,
21+
question: String,
22+
vararg options: InputPollOption,
23+
modifier: SendPoll.() -> Unit = {}
24+
) = this.sendPoll(
25+
chatId = chatId,
26+
question = question,
27+
options = options.toList(),
28+
modifier = modifier
29+
)
30+
31+
32+
inline fun TelegramAware.sendPoll(
33+
channelUsername: String,
34+
question: String,
35+
options: List<InputPollOption>,
36+
modifier: SendPoll.() -> Unit = {}
37+
) = this.execute(SendPoll(
38+
channelUsername = channelUsername,
39+
question = question,
40+
options = options
41+
), modifier)
42+
43+
inline fun TelegramAware.sendPoll(
44+
channelUsername: String,
45+
question: String,
46+
vararg options: InputPollOption,
47+
modifier: SendPoll.() -> Unit = {}
48+
) = this.sendPoll(
49+
channelUsername = channelUsername,
50+
question = question,
51+
options = options.toList(),
52+
modifier = modifier
53+
)
54+

0 commit comments

Comments
 (0)