Skip to content

Commit c1f4280

Browse files
committed
/done command processor
1 parent 1b5a001 commit c1f4280

File tree

8 files changed

+380
-25
lines changed

8 files changed

+380
-25
lines changed

quizoji/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ dependencies {
66
api(project.projects.core)
77
api(libs.tgbotapi.core)
88
implementation(project.projects.dialogs)
9+
implementation(project.projects.votes)
10+
implementation(project.projects.votes.tgbotapiExtensions)
911
implementation(libs.log4j.api)
1012
implementation(libs.tgbotapi.extensions.api)
1113
implementation(libs.tgbotapi.extensions.utils)

quizoji/dynamodb/src/main/kotlin/by/jprof/telegram/bot/quizoji/dynamodb/dao/QuizojiDAO.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,9 @@ private val json = Json { serializersModule = TgBotAPI.module }
4242
fun Quizoji.toAttributes(): Map<String, AttributeValue> = mapOf(
4343
"id" to this.id.toAttributeValue(),
4444
"question" to json.encodeToString(this.question).toAttributeValue(),
45-
"options" to this.options.map { it.toAttributeValue() }.toAttributeValue(),
4645
)
4746

4847
fun Map<String, AttributeValue>.toQuizoji(): Quizoji = Quizoji(
4948
id = this["id"].toString("id"),
5049
question = json.decodeFromString(this["question"].toString("value")),
51-
options = this["options"]?.l()
52-
?.mapNotNull { it.s() }
53-
?: emptyList(),
5450
)

quizoji/dynamodb/src/test/kotlin/by/jprof/telegram/bot/quizoji/dynamodb/dao/QuizojiDAOTest.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,5 @@ internal class QuizojiDAOTest {
4646
text = "Choose the door",
4747
textSources = listOf(RegularTextSource("Choose the door")),
4848
),
49-
options = listOf("1", "2", "3"),
5049
)
5150
}

quizoji/dynamodb/src/test/kotlin/by/jprof/telegram/bot/quizoji/dynamodb/dao/QuizojiTest.kt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,12 @@ internal class QuizojiTest {
3131
text = "Choose the door",
3232
textSources = listOf(RegularTextSource("Choose the door")),
3333
),
34-
options = listOf("1", "2", "3"),
3534
)
3635
private val attributes
3736
get() = mapOf(
3837
"id" to AttributeValue.builder().s("test").build(),
3938
"question" to AttributeValue.builder()
4039
.s("{\"type\":\"TextContent\",\"text\":\"Choose the door\",\"textSources\":[{\"type\":\"regular\",\"value\":{\"source\":\"Choose the door\"}}]}")
4140
.build(),
42-
"options" to AttributeValue.builder().l(
43-
AttributeValue.builder().s("1").build(),
44-
AttributeValue.builder().s("2").build(),
45-
AttributeValue.builder().s("3").build(),
46-
).build(),
4741
)
4842
}

quizoji/dynamodb/src/test/resources/quizoji.items.json

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,6 @@
88
},
99
"question": {
1010
"S": "{\"type\":\"TextContent\",\"text\":\"Choose the door\",\"textSources\":[{\"type\":\"regular\",\"value\":{\"source\":\"Choose the door\"}}]}"
11-
},
12-
"options": {
13-
"L": [
14-
{
15-
"S": "1"
16-
},
17-
{
18-
"S": "2"
19-
},
20-
{
21-
"S": "3"
22-
}
23-
]
2411
}
2512
}
2613
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package by.jprof.telegram.bot.quizoji
2+
3+
import by.jprof.telegram.bot.core.UpdateProcessor
4+
import by.jprof.telegram.bot.dialogs.dao.DialogStateDAO
5+
import by.jprof.telegram.bot.dialogs.model.quizoji.WaitingForOptions
6+
import by.jprof.telegram.bot.quizoji.dao.QuizojiDAO
7+
import by.jprof.telegram.bot.quizoji.model.Quizoji
8+
import by.jprof.telegram.bot.votes.dao.VotesDAO
9+
import by.jprof.telegram.bot.votes.model.Votes
10+
import by.jprof.telegram.bot.votes.tgbotapi_extensions.toInlineKeyboardMarkup
11+
import dev.inmo.tgbotapi.bot.RequestsExecutor
12+
import dev.inmo.tgbotapi.extensions.api.send.sendMessage
13+
import dev.inmo.tgbotapi.extensions.utils.asMessageUpdate
14+
import dev.inmo.tgbotapi.extensions.utils.asPrivateChat
15+
import dev.inmo.tgbotapi.extensions.utils.asPrivateContentMessage
16+
import dev.inmo.tgbotapi.extensions.utils.asTextContent
17+
import dev.inmo.tgbotapi.types.buttons.InlineKeyboardButtons.SwitchInlineQueryInlineKeyboardButton
18+
import dev.inmo.tgbotapi.types.buttons.InlineKeyboardMarkup
19+
import dev.inmo.tgbotapi.types.message.content.TextContent
20+
import dev.inmo.tgbotapi.types.update.abstracts.Update
21+
import org.apache.logging.log4j.LogManager
22+
23+
class QuizojiDoneCommandUpdateProcessor(
24+
private val dialogStateDAO: DialogStateDAO,
25+
private val quizojiDAO: QuizojiDAO,
26+
private val votesDAO: VotesDAO,
27+
private val bot: RequestsExecutor,
28+
) : UpdateProcessor {
29+
companion object {
30+
private val logger = LogManager.getLogger(QuizojiDoneCommandUpdateProcessor::class.java)!!
31+
private val idChars = ('0'..'9') + ('a'..'z') + ('A'..'Z')
32+
private const val idLength = 20
33+
}
34+
35+
override suspend fun process(update: Update) {
36+
val message = update.asMessageUpdate()?.data?.asPrivateContentMessage() ?: return
37+
val chat = message.chat.asPrivateChat() ?: return
38+
val state = dialogStateDAO.get(chat.id.chatId, message.user.id.chatId)
39+
40+
if (state !is WaitingForOptions) {
41+
return
42+
}
43+
44+
val content = message.content.asTextContent() ?: return
45+
46+
if (content.text != "/done") {
47+
return
48+
}
49+
50+
logger.debug("{} finished his Quizoji", chat.id.chatId)
51+
52+
val quizoji = Quizoji(
53+
id = generateQuizojiID(),
54+
question = state.question,
55+
)
56+
val votes = Votes(
57+
id = "QUIZOJI-${quizoji.id}",
58+
options = state.options
59+
)
60+
61+
votesDAO.save(votes)
62+
quizojiDAO.save(quizoji)
63+
dialogStateDAO.delete(chatId = chat.id.chatId, userId = message.user.id.chatId)
64+
65+
bot.sendMessage(
66+
chat = chat,
67+
text = "Quizoji created! Use the 'Publish' button to send it."
68+
)
69+
70+
when (val question = state.question) {
71+
is TextContent -> {
72+
bot.sendMessage(
73+
chat = chat,
74+
entities = question.textSources,
75+
replyMarkup = InlineKeyboardMarkup(
76+
votes
77+
.toInlineKeyboardMarkup(8)
78+
.keyboard
79+
.plus(
80+
listOf(
81+
listOf(
82+
SwitchInlineQueryInlineKeyboardButton(
83+
text = "Publish",
84+
switchInlineQuery = "quizoji ${quizoji.id}",
85+
)
86+
)
87+
)
88+
)
89+
)
90+
)
91+
}
92+
}
93+
}
94+
95+
private fun generateQuizojiID(): String {
96+
return (0 until idLength).map { idChars.random() }.joinToString(separator = "")
97+
}
98+
}

quizoji/src/main/kotlin/by/jprof/telegram/bot/quizoji/model/Quizoji.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,4 @@ import dev.inmo.tgbotapi.types.message.content.abstracts.MessageContent
55
data class Quizoji(
66
val id: String,
77
val question: MessageContent,
8-
val options: List<String>,
98
)

0 commit comments

Comments
 (0)