Skip to content

Commit bd71e64

Browse files
upfill polls bot
1 parent 514d9d6 commit bd71e64

1 file changed

Lines changed: 53 additions & 4 deletions

File tree

PollsBot/src/main/kotlin/PollsBot.kt

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,16 @@ import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
66
import dev.inmo.tgbotapi.extensions.api.bot.setMyCommands
77
import dev.inmo.tgbotapi.extensions.api.send.polls.sendQuizPoll
88
import dev.inmo.tgbotapi.extensions.api.send.polls.sendRegularPoll
9+
import dev.inmo.tgbotapi.extensions.api.send.reply
910
import dev.inmo.tgbotapi.extensions.api.send.send
1011
import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviourAndLongPolling
1112
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand
13+
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onContentMessage
1214
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onPollAnswer
15+
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onPollOptionAdded
16+
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onPollOptionDeleted
1317
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onPollUpdates
18+
import dev.inmo.tgbotapi.extensions.utils.accessibleMessageOrNull
1419
import dev.inmo.tgbotapi.extensions.utils.customEmojiTextSourceOrNull
1520
import dev.inmo.tgbotapi.extensions.utils.extensions.parseCommandsWithArgsSources
1621
import dev.inmo.tgbotapi.types.BotCommand
@@ -105,7 +110,9 @@ suspend fun main(vararg args: String) {
105110
}
106111
},
107112
isAnonymous = false,
108-
replyParameters = ReplyParameters(it)
113+
replyParameters = ReplyParameters(it),
114+
allowAddingOptions = true,
115+
hideResultsUntilCloses = true,
109116
)
110117
pollToChatMutex.withLock {
111118
pollToChat[sentPoll.content.poll.id] = sentPoll.chat.id
@@ -118,7 +125,12 @@ suspend fun main(vararg args: String) {
118125
.firstOrNull { it.first.command == "quiz" }
119126
?.second
120127
?.firstNotNullOfOrNull { it.customEmojiTextSourceOrNull() }
121-
val correctAnswer = Random.nextInt(10)
128+
val correctAnswer = mutableListOf<Int>()
129+
(1 until Random.nextInt(9)).forEach {
130+
val option = Random.nextInt(10)
131+
if (correctAnswer.contains(option)) return@forEach
132+
correctAnswer.add(option)
133+
}
122134
val sentPoll = sendQuizPoll(
123135
it.chat.id,
124136
questionEntities = buildEntities {
@@ -127,7 +139,13 @@ suspend fun main(vararg args: String) {
127139
customEmoji(customEmoji.customEmojiId, customEmoji.subsources)
128140
}
129141
},
130-
(1 .. 10).map {
142+
descriptionTextSources = buildEntities {
143+
regular("Test quiz poll description:")
144+
if (customEmoji != null) {
145+
customEmoji(customEmoji.customEmojiId, customEmoji.subsources)
146+
}
147+
},
148+
options = (1 .. 10).map {
131149
InputPollOption {
132150
regular(it.toString()) + " "
133151
if (customEmoji != null) {
@@ -137,14 +155,19 @@ suspend fun main(vararg args: String) {
137155
},
138156
isAnonymous = false,
139157
replyParameters = ReplyParameters(it),
140-
correctOptionId = correctAnswer,
158+
correctOptionIds = correctAnswer.sorted(),
159+
allowMultipleAnswers = correctAnswer.size > 1,
160+
allowsRevoting = true,
161+
shuffleOptions = true,
162+
hideResultsUntilCloses = true,
141163
explanationTextSources = buildEntities {
142164
regular("Random solved it to be ") + underline((correctAnswer + 1).toString()) + " "
143165
if (customEmoji != null) {
144166
customEmoji(customEmoji.customEmojiId, customEmoji.subsources)
145167
}
146168
}
147169
)
170+
println("Sent poll data: $sentPoll")
148171
pollToChatMutex.withLock {
149172
pollToChat[sentPoll.content.poll.id] = sentPoll.chat.id
150173
}
@@ -168,6 +191,32 @@ suspend fun main(vararg args: String) {
168191
}
169192
}
170193

194+
onPollOptionAdded {
195+
it.chatEvent.pollMessage ?.accessibleMessageOrNull() ?.let { pollMessage ->
196+
reply(pollMessage) {
197+
+"Poll option added: \n"
198+
+it.chatEvent.optionTextSources
199+
}
200+
}
201+
}
202+
onPollOptionDeleted {
203+
it.chatEvent.pollMessage ?.accessibleMessageOrNull() ?.let { pollMessage ->
204+
reply(pollMessage) {
205+
+"Poll option deleted: \n"
206+
+it.chatEvent.optionTextSources
207+
}
208+
}
209+
}
210+
211+
onContentMessage {
212+
val replyPollOptionId = it.replyInfo ?.pollOptionId ?: return@onContentMessage
213+
it.replyTo ?.accessibleMessageOrNull() ?.let { replied ->
214+
reply(replied, pollOptionId = replyPollOptionId) {
215+
+"Reply to poll option"
216+
}
217+
}
218+
}
219+
171220
setMyCommands(
172221
BotCommand("anonymous", "Create anonymous regular poll"),
173222
BotCommand("public", "Create non anonymous regular poll"),

0 commit comments

Comments
 (0)