Skip to content

Commit b83d817

Browse files
committed
Quizoji votes processor
1 parent c1f4280 commit b83d817

File tree

8 files changed

+256
-8
lines changed

8 files changed

+256
-8
lines changed

quizoji/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ dependencies {
88
implementation(project.projects.dialogs)
99
implementation(project.projects.votes)
1010
implementation(project.projects.votes.tgbotapiExtensions)
11+
implementation(project.projects.votes.votingProcessor)
1112
implementation(libs.log4j.api)
1213
implementation(libs.tgbotapi.extensions.api)
1314
implementation(libs.tgbotapi.extensions.utils)

quizoji/src/main/kotlin/by/jprof/telegram/bot/quizoji/QuizojiInlineQueryUpdateProcessor.kt

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
package by.jprof.telegram.bot.quizoji
22

33
import by.jprof.telegram.bot.core.UpdateProcessor
4+
import by.jprof.telegram.bot.quizoji.dao.QuizojiDAO
5+
import by.jprof.telegram.bot.votes.dao.VotesDAO
6+
import by.jprof.telegram.bot.votes.tgbotapi_extensions.toInlineKeyboardMarkup
47
import dev.inmo.tgbotapi.bot.RequestsExecutor
58
import dev.inmo.tgbotapi.extensions.api.answers.answerInlineQuery
69
import dev.inmo.tgbotapi.extensions.utils.asInlineQueryUpdate
10+
import dev.inmo.tgbotapi.types.InlineQueries.InlineQueryResult.InlineQueryResultArticle
11+
import dev.inmo.tgbotapi.types.InlineQueries.InputMessageContent.InputTextMessageContent
12+
import dev.inmo.tgbotapi.types.message.content.TextContent
713
import dev.inmo.tgbotapi.types.update.abstracts.Update
814
import org.apache.logging.log4j.LogManager
915

1016
class QuizojiInlineQueryUpdateProcessor(
17+
private val quizojiDAO: QuizojiDAO,
18+
private val votesDAO: VotesDAO,
1119
private val bot: RequestsExecutor,
1220
) : UpdateProcessor {
1321
companion object {
@@ -23,6 +31,44 @@ class QuizojiInlineQueryUpdateProcessor(
2331
switchPmText = "Create new quizoji",
2432
switchPmParameter = "quizoji",
2533
)
34+
} else if (inlineQuery.query.startsWith("quizoji")) {
35+
val id = inlineQuery.query.substring(7).trim()
36+
37+
logger.debug("Quizoji #{} requested", id)
38+
39+
val quizoji = quizojiDAO.get(id)
40+
41+
if (null == quizoji) {
42+
logger.warn("Quizoji #{} not found!", id)
43+
44+
return
45+
}
46+
47+
val votes = votesDAO.get("QUIZOJI-$id")
48+
49+
if (null == votes) {
50+
logger.warn("Votes for quizoji #{} not found!", id)
51+
52+
return
53+
}
54+
55+
when (val question = quizoji.question) {
56+
is TextContent -> {
57+
bot.answerInlineQuery(
58+
inlineQuery = inlineQuery,
59+
results = listOf(
60+
InlineQueryResultArticle(
61+
id = quizoji.id,
62+
title = "Quizoji with ${votes.options.joinToString()} options",
63+
inputMessageContent = InputTextMessageContent(
64+
entities = question.textSources
65+
),
66+
replyMarkup = votes.toInlineKeyboardMarkup(8)
67+
)
68+
)
69+
)
70+
}
71+
}
2672
}
2773
}
2874
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package by.jprof.telegram.bot.quizoji
2+
3+
import by.jprof.telegram.bot.core.UpdateProcessor
4+
import by.jprof.telegram.bot.votes.dao.VotesDAO
5+
import by.jprof.telegram.bot.votes.voting_processor.VotingProcessor
6+
import dev.inmo.tgbotapi.bot.RequestsExecutor
7+
import dev.inmo.tgbotapi.types.update.CallbackQueryUpdate
8+
import dev.inmo.tgbotapi.types.update.abstracts.Update
9+
10+
class QuizojiVoteUpdateProcessor(
11+
votesDAO: VotesDAO,
12+
bot: RequestsExecutor
13+
) : VotingProcessor(
14+
"QUIZOJI",
15+
votesDAO,
16+
{ throw UnsupportedOperationException("Votes should be constructed elsewhere!") },
17+
bot,
18+
), UpdateProcessor {
19+
override suspend fun process(update: Update) {
20+
when (update) {
21+
is CallbackQueryUpdate -> processCallbackQuery(update.data)
22+
}
23+
}
24+
}

quizoji/src/test/kotlin/by/jprof/telegram/bot/quizoji/QuizojiInlineQueryUpdateProcessorTest.kt

Lines changed: 119 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
package by.jprof.telegram.bot.quizoji
22

3+
import by.jprof.telegram.bot.quizoji.dao.QuizojiDAO
4+
import by.jprof.telegram.bot.quizoji.model.Quizoji
5+
import by.jprof.telegram.bot.votes.dao.VotesDAO
6+
import by.jprof.telegram.bot.votes.model.Votes
37
import dev.inmo.tgbotapi.bot.RequestsExecutor
48
import dev.inmo.tgbotapi.extensions.api.answers.answerInlineQuery
59
import dev.inmo.tgbotapi.types.InlineQueries.query.BaseInlineQuery
10+
import dev.inmo.tgbotapi.types.MessageEntity.textsources.RegularTextSource
11+
import dev.inmo.tgbotapi.types.message.content.TextContent
612
import dev.inmo.tgbotapi.types.update.InlineQueryUpdate
713
import dev.inmo.tgbotapi.types.update.MessageUpdate
814
import io.mockk.*
@@ -15,6 +21,12 @@ import org.junit.jupiter.api.extension.ExtendWith
1521

1622
@ExtendWith(MockKExtension::class)
1723
internal class QuizojiInlineQueryUpdateProcessorTest {
24+
@MockK
25+
private lateinit var quizojiDAO: QuizojiDAO
26+
27+
@MockK
28+
private lateinit var votesDAO: VotesDAO
29+
1830
@MockK(relaxed = true)
1931
private lateinit var bot: RequestsExecutor
2032

@@ -23,6 +35,8 @@ internal class QuizojiInlineQueryUpdateProcessorTest {
2335
@BeforeEach
2436
fun setUp() {
2537
sut = QuizojiInlineQueryUpdateProcessor(
38+
quizojiDAO = quizojiDAO,
39+
votesDAO = votesDAO,
2640
bot = bot,
2741
)
2842
}
@@ -36,7 +50,7 @@ internal class QuizojiInlineQueryUpdateProcessorTest {
3650
)
3751
)
3852

39-
verify { listOf(bot) wasNot called }
53+
verify { listOf(quizojiDAO, votesDAO, bot) wasNot called }
4054

4155
clearAllMocks()
4256
}
@@ -56,13 +70,13 @@ internal class QuizojiInlineQueryUpdateProcessorTest {
5670
)
5771
)
5872

59-
verify { listOf(bot) wasNot called }
73+
verify { listOf(quizojiDAO, votesDAO, bot) wasNot called }
6074

6175
clearAllMocks()
6276
}
6377

6478
@Test
65-
fun processQuizojiInilineQuery() = runBlocking {
79+
fun processNewQuizojiInilineQuery() = runBlocking {
6680
val inlineQuery = BaseInlineQuery(
6781
id = "QuizojiStartCommandUpdateProcessorTest",
6882
from = mockk(),
@@ -85,6 +99,108 @@ internal class QuizojiInlineQueryUpdateProcessorTest {
8599
switchPmParameter = "quizoji",
86100
)
87101
}
102+
verify { listOf(quizojiDAO, votesDAO) wasNot called }
103+
104+
clearAllMocks()
105+
}
106+
107+
@Test
108+
fun processUnknownQuizojiIDInilineQuery() = runBlocking {
109+
val inlineQuery = BaseInlineQuery(
110+
id = "QuizojiStartCommandUpdateProcessorTest",
111+
from = mockk(),
112+
query = "quizoji id",
113+
offset = "",
114+
chatType = null,
115+
)
116+
117+
coEvery { quizojiDAO.get("id") }.returns(null)
118+
119+
sut.process(
120+
InlineQueryUpdate(
121+
updateId = 1L,
122+
data = inlineQuery
123+
)
124+
)
125+
126+
coVerify(exactly = 1) {
127+
quizojiDAO.get("id")
128+
}
129+
130+
verify { listOf(votesDAO, bot) wasNot called }
131+
132+
clearAllMocks()
133+
}
134+
135+
@Test
136+
fun processUnexistingVotesQuizojiIDInilineQuery() = runBlocking {
137+
val inlineQuery = BaseInlineQuery(
138+
id = "QuizojiStartCommandUpdateProcessorTest",
139+
from = mockk(),
140+
query = "quizoji id",
141+
offset = "",
142+
chatType = null,
143+
)
144+
145+
coEvery { quizojiDAO.get("id") }.returns(Quizoji("id", TextContent("Question")))
146+
coEvery { votesDAO.get("QUIZOJI-id") }.returns(null)
147+
148+
sut.process(
149+
InlineQueryUpdate(
150+
updateId = 1L,
151+
data = inlineQuery
152+
)
153+
)
154+
155+
coVerify(exactly = 1) {
156+
quizojiDAO.get("id")
157+
}
158+
coVerify(exactly = 1) {
159+
votesDAO.get("QUIZOJI-id")
160+
}
161+
162+
verify { listOf(bot) wasNot called }
163+
164+
clearAllMocks()
165+
}
166+
167+
@Test
168+
fun processQuizojiIDInilineQuery() = runBlocking {
169+
val inlineQuery = BaseInlineQuery(
170+
id = "QuizojiStartCommandUpdateProcessorTest",
171+
from = mockk(),
172+
query = "quizoji id",
173+
offset = "",
174+
chatType = null,
175+
)
176+
177+
coEvery { quizojiDAO.get("id") }.returns(
178+
Quizoji(
179+
"id",
180+
TextContent("Question", listOf(RegularTextSource("Question")))
181+
)
182+
)
183+
coEvery { votesDAO.get("QUIZOJI-id") }.returns(Votes("QUIZOJI-id", listOf("1", "2"), mapOf("1" to "1")))
184+
185+
sut.process(
186+
InlineQueryUpdate(
187+
updateId = 1L,
188+
data = inlineQuery
189+
)
190+
)
191+
192+
coVerify(exactly = 1) {
193+
quizojiDAO.get("id")
194+
}
195+
coVerify(exactly = 1) {
196+
votesDAO.get("QUIZOJI-id")
197+
}
198+
// coVerify(exactly = 1) {
199+
// bot.answerInlineQuery(
200+
// inlineQuery = inlineQuery,
201+
// results = any()
202+
// )
203+
// }
88204

89205
clearAllMocks()
90206
}

quizoji/src/test/kotlin/by/jprof/telegram/bot/quizoji/QuizojiOptionUpdateProcessorTest.kt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import dev.inmo.tgbotapi.bot.RequestsExecutor
88
import dev.inmo.tgbotapi.extensions.api.send.sendMessage
99
import dev.inmo.tgbotapi.types.ChatId
1010
import dev.inmo.tgbotapi.types.CommonUser
11+
import dev.inmo.tgbotapi.types.MessageEntity.textsources.BotCommandTextSource
1112
import dev.inmo.tgbotapi.types.ParseMode.MarkdownV2
1213
import dev.inmo.tgbotapi.types.chat.PrivateChatImpl
1314
import dev.inmo.tgbotapi.types.chat.abstracts.ChannelChat
@@ -175,6 +176,42 @@ internal class QuizojiOptionUpdateProcessorTest {
175176
clearAllMocks()
176177
}
177178

179+
@Test
180+
fun processDoneCommand() = runBlocking {
181+
val chat = PrivateChatImpl(
182+
id = ChatId(1),
183+
)
184+
185+
coEvery { dialogStateDAO.get(1, 2) }.returns(WaitingForOptions(1, 2, TextContent("Test")))
186+
187+
sut.process(
188+
MessageUpdate(
189+
updateId = 1,
190+
data = PrivateContentMessageImpl(
191+
messageId = 1,
192+
user = CommonUser(id = ChatId(2), "Test"),
193+
chat = chat,
194+
content = TextContent(
195+
text = "/done",
196+
textSources = listOf(BotCommandTextSource("done"))
197+
),
198+
date = DateTime.now(),
199+
editDate = null,
200+
forwardInfo = null,
201+
replyTo = null,
202+
replyMarkup = null,
203+
senderBot = null,
204+
paymentInfo = null,
205+
)
206+
)
207+
)
208+
209+
coVerify(exactly = 1) { dialogStateDAO.get(1, 2) }
210+
verify { listOf(bot) wasNot called }
211+
212+
clearAllMocks()
213+
}
214+
178215
@Test
179216
fun process() = runBlocking {
180217
val chat = PrivateChatImpl(

runners/lambda/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ dependencies {
1414
implementation(project.projects.youtube.dynamodb)
1515
implementation(project.projects.kotlin.dynamodb)
1616
implementation(project.projects.dialogs.dynamodb)
17-
implementation(project.projects.quizoji)
17+
implementation(project.projects.quizoji.dynamodb)
1818
}

runners/lambda/src/main/kotlin/by/jprof/telegram/bot/runners/lambda/config/database.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ package by.jprof.telegram.bot.runners.lambda.config
22

33
import by.jprof.telegram.bot.dialogs.dao.DialogStateDAO
44
import by.jprof.telegram.bot.kotlin.dao.KotlinMentionsDAO
5+
import by.jprof.telegram.bot.quizoji.dao.QuizojiDAO
56
import by.jprof.telegram.bot.votes.dao.VotesDAO
67
import by.jprof.telegram.bot.youtube.dao.YouTubeChannelsWhitelistDAO
78
import org.koin.core.qualifier.named
89
import org.koin.dsl.module
910
import software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient
1011
import by.jprof.telegram.bot.dialogs.dynamodb.dao.DialogStateDAO as DynamoDBDialogStateDAO
1112
import by.jprof.telegram.bot.kotlin.dynamodb.dao.KotlinMentionsDAO as DynamoDBKotlinMentionsDAO
13+
import by.jprof.telegram.bot.quizoji.dynamodb.dao.QuizojiDAO as DynamoDBQuizojiDAO
1214
import by.jprof.telegram.bot.votes.dynamodb.dao.VotesDAO as DynamoDBVotesDAO
1315
import by.jprof.telegram.bot.youtube.dynamodb.dao.YouTubeChannelsWhitelistDAO as DynamoDBYouTubeChannelsWhitelistDAO
1416

@@ -44,4 +46,11 @@ val databaseModule = module {
4446
get(named(TABLE_DIALOG_STATES))
4547
)
4648
}
49+
50+
single<QuizojiDAO> {
51+
DynamoDBQuizojiDAO(
52+
get(),
53+
get(named(TABLE_QUIZOJIS))
54+
)
55+
}
4756
}

runners/lambda/src/main/kotlin/by/jprof/telegram/bot/runners/lambda/config/pipeline.kt

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ import by.jprof.telegram.bot.core.UpdateProcessor
55
import by.jprof.telegram.bot.jep.JEPUpdateProcessor
66
import by.jprof.telegram.bot.jep.JsoupJEPSummary
77
import by.jprof.telegram.bot.kotlin.KotlinMentionsUpdateProcessor
8-
import by.jprof.telegram.bot.quizoji.QuizojiInlineQueryUpdateProcessor
9-
import by.jprof.telegram.bot.quizoji.QuizojiOptionUpdateProcessor
10-
import by.jprof.telegram.bot.quizoji.QuizojiQuestionUpdateProcessor
11-
import by.jprof.telegram.bot.quizoji.QuizojiStartCommandUpdateProcessor
8+
import by.jprof.telegram.bot.quizoji.*
129
import by.jprof.telegram.bot.youtube.YouTubeUpdateProcessor
1310
import org.koin.core.qualifier.named
1411
import org.koin.dsl.module
@@ -44,6 +41,8 @@ val pipelineModule = module {
4441

4542
single<UpdateProcessor>(named("QuizojiInlineQueryUpdateProcessor")) {
4643
QuizojiInlineQueryUpdateProcessor(
44+
quizojiDAO = get(),
45+
votesDAO = get(),
4746
bot = get(),
4847
)
4948
}
@@ -68,4 +67,20 @@ val pipelineModule = module {
6867
bot = get(),
6968
)
7069
}
70+
71+
single<UpdateProcessor>(named("QuizojiDoneCommandUpdateProcessor")) {
72+
QuizojiDoneCommandUpdateProcessor(
73+
dialogStateDAO = get(),
74+
quizojiDAO = get(),
75+
votesDAO = get(),
76+
bot = get(),
77+
)
78+
}
79+
80+
single<UpdateProcessor>(named("QuizojiVoteUpdateProcessor")) {
81+
QuizojiVoteUpdateProcessor(
82+
votesDAO = get(),
83+
bot = get(),
84+
)
85+
}
7186
}

0 commit comments

Comments
 (0)