Skip to content

Commit 21d1408

Browse files
committed
Non-English content update processor
1 parent 2ee7c6f commit 21d1408

File tree

7 files changed

+80
-0
lines changed

7 files changed

+80
-0
lines changed

english/README.adoc

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ This is an umbrella feature for several smaller sub-features:
55
* The bot sends https://www.urbandictionary.com[Urban Word of the Day] into English rooms.
66
* The bot explains emphasised text parts in English rooms.
77
* The bot tolerates no stupid questions.
8+
* The bot tolerates no other languages, but English in English rooms. Remember: every motherfucker must speak English from his heart in an English room!
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package by.jprof.telegram.bot.english
2+
3+
import by.jprof.telegram.bot.core.UpdateProcessor
4+
import by.jprof.telegram.bot.english.language_rooms.dao.LanguageRoomDAO
5+
import by.jprof.telegram.bot.english.language_rooms.model.Language
6+
import by.jprof.telegram.bot.english.language_rooms.model.Violence
7+
import dev.inmo.tgbotapi.bot.RequestsExecutor
8+
import dev.inmo.tgbotapi.extensions.api.delete
9+
import dev.inmo.tgbotapi.extensions.api.send.media.sendAnimation
10+
import dev.inmo.tgbotapi.extensions.api.send.media.sendPhoto
11+
import dev.inmo.tgbotapi.extensions.utils.asBaseMessageUpdate
12+
import dev.inmo.tgbotapi.extensions.utils.asContentMessage
13+
import dev.inmo.tgbotapi.extensions.utils.asTextContent
14+
import dev.inmo.tgbotapi.requests.abstracts.MultipartFile
15+
import dev.inmo.tgbotapi.types.update.abstracts.Update
16+
import io.ktor.utils.io.streams.asInput
17+
import kotlin.random.Random
18+
import org.apache.logging.log4j.LogManager
19+
20+
class MotherfuckingUpdateProcessor(
21+
private val languageRoomDAO: LanguageRoomDAO,
22+
private val bot: RequestsExecutor,
23+
) : UpdateProcessor {
24+
companion object {
25+
private val logger = LogManager.getLogger(MotherfuckingUpdateProcessor::class.java)!!
26+
}
27+
28+
override suspend fun process(update: Update) {
29+
val update = update.asBaseMessageUpdate() ?: return
30+
val roomId = update.data.chat.id
31+
val message = update.data.asContentMessage() ?: return
32+
val content = message.content.asTextContent() ?: return
33+
34+
if (
35+
languageRoomDAO.get(roomId.chatId, roomId.threadId)?.takeIf { it.language == Language.ENGLISH && it.violence == Violence.MOTHERFUCKER } == null
36+
) {
37+
return
38+
}
39+
40+
val latinLetters = content.text.count { it in 'a'..'z' || it in 'A'..'Z' }
41+
42+
logger.debug("Latin letters detected: {}, total letters: {}", latinLetters, content.text.length)
43+
44+
if (latinLetters.toDouble() / content.text.length < 2.toDouble() / 3) {
45+
when (val i = Random.nextInt(4)) {
46+
0, 1 -> bot.sendPhoto(
47+
chat = message.chat,
48+
fileId = MultipartFile(
49+
filename = "english$i.png",
50+
inputSource = {
51+
this::class.java.getResourceAsStream("/english$i.png").asInput()
52+
},
53+
),
54+
replyToMessageId = message.messageId,
55+
)
56+
57+
2, 3 -> bot.sendAnimation(
58+
chat = message.chat,
59+
animation = MultipartFile(
60+
filename = "english$i.gif",
61+
inputSource = {
62+
this::class.java.getResourceAsStream("/english$i.gif").asInput()
63+
},
64+
),
65+
replyToMessageId = message.messageId,
66+
)
67+
}
68+
bot.delete(message)
69+
}
70+
}
71+
}
183 KB
Loading
305 KB
Loading
312 KB
Loading
1.42 MB
Loading

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

+8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import by.jprof.telegram.bot.core.UpdateProcessor
55
import by.jprof.telegram.bot.currencies.CurrenciesUpdateProcessor
66
import by.jprof.telegram.bot.english.EnglishCommandUpdateProcessor
77
import by.jprof.telegram.bot.english.ExplainerUpdateProcessor
8+
import by.jprof.telegram.bot.english.MotherfuckingUpdateProcessor
89
import by.jprof.telegram.bot.english.UrbanWordOfTheDayUpdateProcessor
910
import by.jprof.telegram.bot.english.WhatWordUpdateProcessor
1011
import by.jprof.telegram.bot.eval.EvalUpdateProcessor
@@ -183,4 +184,11 @@ val pipelineModule = module {
183184
bot = get(),
184185
)
185186
}
187+
188+
single<UpdateProcessor>(named("MotherfuckingUpdateProcessor")) {
189+
MotherfuckingUpdateProcessor(
190+
languageRoomDAO = get(),
191+
bot = get(),
192+
)
193+
}
186194
}

0 commit comments

Comments
 (0)