Skip to content

Commit 5139d2f

Browse files
committed
Handle /start quizoji
1 parent 468e35a commit 5139d2f

File tree

5 files changed

+233
-10
lines changed

5 files changed

+233
-10
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import dev.inmo.tgbotapi.extensions.utils.asInlineQueryUpdate
77
import dev.inmo.tgbotapi.types.update.abstracts.Update
88
import org.apache.logging.log4j.LogManager
99

10-
class QuizojiUpdateProcessor(
10+
class QuizojiInlineQueryUpdateProcessor(
1111
private val bot: RequestsExecutor,
1212
) : UpdateProcessor {
1313
companion object {
14-
private val logger = LogManager.getLogger(QuizojiUpdateProcessor::class.java)!!
14+
private val logger = LogManager.getLogger(QuizojiInlineQueryUpdateProcessor::class.java)!!
1515
}
1616

1717
override suspend fun process(update: Update) {
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package by.jprof.telegram.bot.quizoji
2+
3+
import by.jprof.telegram.bot.core.UpdateProcessor
4+
import dev.inmo.tgbotapi.bot.RequestsExecutor
5+
import dev.inmo.tgbotapi.extensions.api.send.sendMessage
6+
import dev.inmo.tgbotapi.extensions.utils.asMessageUpdate
7+
import dev.inmo.tgbotapi.extensions.utils.asPrivateChat
8+
import dev.inmo.tgbotapi.extensions.utils.asPrivateContentMessage
9+
import dev.inmo.tgbotapi.extensions.utils.asTextContent
10+
import dev.inmo.tgbotapi.types.update.abstracts.Update
11+
import org.apache.logging.log4j.LogManager
12+
13+
class QuizojiStartCommandUpdateProcessor(
14+
private val bot: RequestsExecutor,
15+
) : UpdateProcessor {
16+
companion object {
17+
private val logger = LogManager.getLogger(QuizojiStartCommandUpdateProcessor::class.java)!!
18+
}
19+
20+
override suspend fun process(update: Update) {
21+
val message = update.asMessageUpdate()?.data?.asPrivateContentMessage() ?: return
22+
val chat = message.chat.asPrivateChat() ?: return
23+
val content = message.content.asTextContent() ?: return
24+
25+
if (content.text != "/start quizoji") {
26+
return
27+
}
28+
29+
bot.sendMessage(
30+
chat = chat,
31+
text = "Let's create a Quizoji! First, send me the message. It can be anything — a text, photo, video, even a sticker."
32+
)
33+
}
34+
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ import org.junit.jupiter.api.Test
1414
import org.junit.jupiter.api.extension.ExtendWith
1515

1616
@ExtendWith(MockKExtension::class)
17-
internal class QuizojiUpdateProcessorTest {
17+
internal class QuizojiInlineQueryUpdateProcessorTest {
1818
@MockK(relaxed = true)
1919
private lateinit var bot: RequestsExecutor
2020

21-
lateinit var sut: QuizojiUpdateProcessor
21+
lateinit var sut: QuizojiInlineQueryUpdateProcessor
2222

2323
@BeforeEach
2424
fun setUp() {
25-
sut = QuizojiUpdateProcessor(
25+
sut = QuizojiInlineQueryUpdateProcessor(
2626
bot = bot,
2727
)
2828
}
@@ -47,7 +47,7 @@ internal class QuizojiUpdateProcessorTest {
4747
InlineQueryUpdate(
4848
updateId = 1L,
4949
data = BaseInlineQuery(
50-
id = "1",
50+
id = "QuizojiStartCommandUpdateProcessorTest",
5151
from = mockk(),
5252
query = "alien",
5353
offset = "",
@@ -63,7 +63,7 @@ internal class QuizojiUpdateProcessorTest {
6363
@Test
6464
fun processQuizojiInilineQuery() = runBlocking {
6565
val inlineQuery = BaseInlineQuery(
66-
id = "1",
66+
id = "QuizojiStartCommandUpdateProcessorTest",
6767
from = mockk(),
6868
query = "quizoji",
6969
offset = "",
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
package by.jprof.telegram.bot.quizoji
2+
3+
import com.soywiz.klock.DateTime
4+
import dev.inmo.tgbotapi.bot.RequestsExecutor
5+
import dev.inmo.tgbotapi.extensions.api.send.sendMessage
6+
import dev.inmo.tgbotapi.types.Bot
7+
import dev.inmo.tgbotapi.types.ChatId
8+
import dev.inmo.tgbotapi.types.chat.PrivateChatImpl
9+
import dev.inmo.tgbotapi.types.chat.abstracts.PrivateChat
10+
import dev.inmo.tgbotapi.types.message.PrivateContentMessageImpl
11+
import dev.inmo.tgbotapi.types.message.abstracts.ChannelContentMessage
12+
import dev.inmo.tgbotapi.types.message.content.TextContent
13+
import dev.inmo.tgbotapi.types.message.content.media.AudioContent
14+
import dev.inmo.tgbotapi.types.update.MessageUpdate
15+
import dev.inmo.tgbotapi.types.update.PollUpdate
16+
import io.mockk.*
17+
import io.mockk.impl.annotations.MockK
18+
import io.mockk.junit5.MockKExtension
19+
import kotlinx.coroutines.runBlocking
20+
import org.junit.jupiter.api.BeforeEach
21+
import org.junit.jupiter.api.Test
22+
import org.junit.jupiter.api.extension.ExtendWith
23+
24+
@ExtendWith(MockKExtension::class)
25+
internal class QuizojiStartCommandUpdateProcessorTest {
26+
@MockK(relaxed = true)
27+
private lateinit var bot: RequestsExecutor
28+
29+
lateinit var sut: QuizojiStartCommandUpdateProcessor
30+
31+
@BeforeEach
32+
fun setUp() {
33+
sut = QuizojiStartCommandUpdateProcessor(
34+
bot = bot,
35+
)
36+
}
37+
38+
@Test
39+
fun processNonMessageUpdate() = runBlocking {
40+
sut.process(
41+
PollUpdate(
42+
updateId = 1,
43+
data = mockk()
44+
)
45+
)
46+
47+
verify { listOf(bot) wasNot called }
48+
49+
clearAllMocks()
50+
}
51+
52+
@Test
53+
fun processNonPrivateContentMessage() = runBlocking {
54+
sut.process(
55+
MessageUpdate(
56+
updateId = 1,
57+
data = mockk<ChannelContentMessage<*>>()
58+
)
59+
)
60+
61+
verify { listOf(bot) wasNot called }
62+
63+
clearAllMocks()
64+
}
65+
66+
@Test
67+
fun processNonPrivateChat() = runBlocking {
68+
sut.process(
69+
MessageUpdate(
70+
updateId = 1,
71+
data = PrivateContentMessageImpl(
72+
messageId = 1,
73+
user = mockk(),
74+
chat = mockk<Bot>(),
75+
content = mockk(),
76+
date = DateTime.now(),
77+
editDate = null,
78+
forwardInfo = null,
79+
replyTo = null,
80+
replyMarkup = null,
81+
senderBot = null,
82+
paymentInfo = null,
83+
)
84+
)
85+
)
86+
87+
verify { listOf(bot) wasNot called }
88+
89+
clearAllMocks()
90+
}
91+
92+
@Test
93+
fun processNonTextContentMessage() = runBlocking {
94+
sut.process(
95+
MessageUpdate(
96+
updateId = 1,
97+
data = PrivateContentMessageImpl(
98+
messageId = 1,
99+
user = mockk(),
100+
chat = mockk<PrivateChat>(),
101+
content = mockk<AudioContent>(),
102+
date = DateTime.now(),
103+
editDate = null,
104+
forwardInfo = null,
105+
replyTo = null,
106+
replyMarkup = null,
107+
senderBot = null,
108+
paymentInfo = null,
109+
)
110+
)
111+
)
112+
113+
verify { listOf(bot) wasNot called }
114+
115+
clearAllMocks()
116+
}
117+
118+
@Test
119+
fun processWrongCommand() = runBlocking {
120+
sut.process(
121+
MessageUpdate(
122+
updateId = 1,
123+
data = PrivateContentMessageImpl(
124+
messageId = 1,
125+
user = mockk(),
126+
chat = mockk<PrivateChat>(),
127+
content = TextContent(
128+
text = "/start doing your morning exercise"
129+
),
130+
date = DateTime.now(),
131+
editDate = null,
132+
forwardInfo = null,
133+
replyTo = null,
134+
replyMarkup = null,
135+
senderBot = null,
136+
paymentInfo = null,
137+
)
138+
)
139+
)
140+
141+
verify { listOf(bot) wasNot called }
142+
143+
clearAllMocks()
144+
}
145+
146+
@Test
147+
fun process() = runBlocking {
148+
val chat = PrivateChatImpl(
149+
id = ChatId(1),
150+
)
151+
152+
sut.process(
153+
MessageUpdate(
154+
updateId = 1,
155+
data = PrivateContentMessageImpl(
156+
messageId = 1,
157+
user = mockk(),
158+
chat = chat,
159+
content = TextContent(
160+
text = "/start quizoji"
161+
),
162+
date = DateTime.now(),
163+
editDate = null,
164+
forwardInfo = null,
165+
replyTo = null,
166+
replyMarkup = null,
167+
senderBot = null,
168+
paymentInfo = null,
169+
)
170+
)
171+
)
172+
173+
coVerify(exactly = 1) {
174+
bot.sendMessage(
175+
chat = chat,
176+
text = "Let's create a Quizoji! First, send me the message. It can be anything — a text, photo, video, even a sticker."
177+
)
178+
}
179+
180+
clearAllMocks()
181+
}
182+
}

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ 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.QuizojiUpdateProcessor
8+
import by.jprof.telegram.bot.quizoji.QuizojiInlineQueryUpdateProcessor
9+
import by.jprof.telegram.bot.quizoji.QuizojiStartCommandUpdateProcessor
910
import by.jprof.telegram.bot.youtube.YouTubeUpdateProcessor
1011
import org.koin.core.qualifier.named
1112
import org.koin.dsl.module
@@ -39,8 +40,14 @@ val pipelineModule = module {
3940
)
4041
}
4142

42-
single<UpdateProcessor>(named("QuizojiUpdateProcessor")) {
43-
QuizojiUpdateProcessor(
43+
single<UpdateProcessor>(named("QuizojiInlineQueryUpdateProcessor")) {
44+
QuizojiInlineQueryUpdateProcessor(
45+
bot = get(),
46+
)
47+
}
48+
49+
single<UpdateProcessor>(named("QuizojiStartCommandUpdateProcessor")) {
50+
QuizojiStartCommandUpdateProcessor(
4451
bot = get(),
4552
)
4653
}

0 commit comments

Comments
 (0)