Skip to content

Commit f9c892c

Browse files
committed
/ud command update processor
1 parent b1def5f commit f9c892c

File tree

9 files changed

+110
-0
lines changed

9 files changed

+110
-0
lines changed

.deploy/lambda/lib/JProfByBotStack.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ export class JProfByBotStack extends cdk.Stack {
153153
'TABLE_PINS': pinsTable.tableName,
154154
'TABLE_TIMEZONES': timezonesTable.tableName,
155155
'TABLE_LANGUAGE_ROOMS': languageRoomsTable.tableName,
156+
'TABLE_URBAN_WORDS_OF_THE_DAY': urbanWordsOfTheDayTable.tableName,
156157
'STATE_MACHINE_UNPINS': stateMachineUnpin.stateMachineArn,
157158
'TOKEN_TELEGRAM_BOT': props.telegramToken,
158159
'TOKEN_YOUTUBE_API': props.youtubeToken,
@@ -209,6 +210,7 @@ export class JProfByBotStack extends cdk.Stack {
209210
languageRoomsTable.grantReadData(lambdaDailyUrbanDictionary);
210211

211212
urbanWordsOfTheDayTable.grantWriteData(lambdaDailyUrbanDictionary);
213+
urbanWordsOfTheDayTable.grantReadData(lambdaWebhook);
212214

213215
stateMachineUnpin.grantStartExecution(lambdaWebhook);
214216
stateMachineUnpin.grantStartExecution(lambdaDailyUrbanDictionary);

english/README.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
= English
2+
3+
This is an umbrella feature for several smaller sub-features:
4+
5+
* The bot sends https://www.urbandictionary.com[Urban Word of the Day] into English rooms.

english/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ plugins {
55
dependencies {
66
api(project.projects.core)
77
implementation(projects.english.languageRooms)
8+
implementation(projects.english.urbanWordOfTheDay)
9+
implementation(projects.english.urbanWordOfTheDayFormatter)
810
implementation(libs.log4j.api)
911

1012
testImplementation(libs.junit.jupiter.api)
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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.urban_dictionary_definition_formatter.format
7+
import by.jprof.telegram.bot.english.urban_word_of_the_day.dao.UrbanWordOfTheDayDAO
8+
import by.jprof.telegram.bot.english.utils.downTo
9+
import dev.inmo.tgbotapi.bot.RequestsExecutor
10+
import dev.inmo.tgbotapi.extensions.api.send.reply
11+
import dev.inmo.tgbotapi.extensions.utils.asBaseMessageUpdate
12+
import dev.inmo.tgbotapi.extensions.utils.asBotCommandTextSource
13+
import dev.inmo.tgbotapi.extensions.utils.asContentMessage
14+
import dev.inmo.tgbotapi.extensions.utils.asTextContent
15+
import dev.inmo.tgbotapi.types.message.MarkdownV2
16+
import dev.inmo.tgbotapi.types.update.abstracts.Update
17+
import java.time.LocalDate
18+
import org.apache.logging.log4j.LogManager
19+
20+
class UrbanWordOfTheDayUpdateProcessor(
21+
private val languageRoomDAO: LanguageRoomDAO,
22+
private val urbanWordOfTheDayDAO: UrbanWordOfTheDayDAO,
23+
private val bot: RequestsExecutor,
24+
) : UpdateProcessor {
25+
companion object {
26+
private val logger = LogManager.getLogger(UrbanWordOfTheDayUpdateProcessor::class.java)!!
27+
}
28+
29+
override suspend fun process(update: Update) {
30+
val update = update.asBaseMessageUpdate() ?: return
31+
val roomId = update.data.chat.id
32+
val message = update.data.asContentMessage() ?: return
33+
val content = message.content.asTextContent() ?: return
34+
35+
if (
36+
content.textSources.none {
37+
it.asBotCommandTextSource()?.command?.lowercase() in listOf("urban", "ud", "urbanword", "urbanwordoftheday")
38+
}
39+
) {
40+
return
41+
}
42+
43+
if (
44+
languageRoomDAO.get(roomId.chatId, roomId.threadId)?.takeIf { it.language == Language.ENGLISH } == null
45+
) {
46+
return
47+
}
48+
49+
val urbanWordOfTheDay = (LocalDate.now() downTo LocalDate.now().minusDays(5)).asSequence().firstNotNullOfOrNull {
50+
urbanWordOfTheDayDAO.get(it)
51+
}
52+
53+
if (urbanWordOfTheDay != null) {
54+
bot.reply(
55+
to = message,
56+
text = urbanWordOfTheDay.format(),
57+
parseMode = MarkdownV2,
58+
disableWebPagePreview = true,
59+
)
60+
} else {
61+
bot.reply(
62+
to = message,
63+
text = "No recent words of the day \uD83D\uDE22",
64+
parseMode = MarkdownV2,
65+
disableWebPagePreview = true,
66+
)
67+
}
68+
}
69+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package by.jprof.telegram.bot.english.utils
2+
3+
import java.time.LocalDate
4+
5+
infix fun LocalDate.downTo(other: LocalDate): Iterator<LocalDate> = iterator {
6+
var current = this@downTo
7+
8+
while (current >= other) {
9+
yield(current)
10+
current = current.minusDays(1)
11+
}
12+
}

launchers/lambda/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ dependencies {
2525
implementation(project.projects.times)
2626
implementation(project.projects.english)
2727
implementation(project.projects.english.languageRooms.dynamodb)
28+
implementation(project.projects.english.urbanWordOfTheDay.dynamodb)
2829
}

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

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

33
import by.jprof.telegram.bot.dialogs.dao.DialogStateDAO
44
import by.jprof.telegram.bot.english.language_rooms.dao.LanguageRoomDAO
5+
import by.jprof.telegram.bot.english.urban_word_of_the_day.dao.UrbanWordOfTheDayDAO
56
import by.jprof.telegram.bot.kotlin.dao.KotlinMentionsDAO
67
import by.jprof.telegram.bot.monies.dao.MoniesDAO
78
import by.jprof.telegram.bot.pins.dao.PinDAO
@@ -15,6 +16,7 @@ import org.koin.dsl.module
1516
import software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient
1617
import by.jprof.telegram.bot.dialogs.dynamodb.dao.DialogStateDAO as DynamoDBDialogStateDAO
1718
import by.jprof.telegram.bot.english.language_rooms.dynamodb.dao.LanguageRoomDAO as DynamoDBLanguageRoomDAO
19+
import by.jprof.telegram.bot.english.urban_word_of_the_day.dynamodb.dao.UrbanWordOfTheDayDAO as DynamoDBUrbanWordOfTheDayDAO
1820
import by.jprof.telegram.bot.kotlin.dynamodb.dao.KotlinMentionsDAO as DynamoDBKotlinMentionsDAO
1921
import by.jprof.telegram.bot.monies.dynamodb.dao.MoniesDAO as DynamoDBMoniesDAO
2022
import by.jprof.telegram.bot.pins.dynamodb.dao.PinDAO as DynamoDBPinDAO
@@ -91,4 +93,11 @@ val databaseModule = module {
9193
get(named(TABLE_LANGUAGE_ROOMS))
9294
)
9395
}
96+
97+
single<UrbanWordOfTheDayDAO> {
98+
DynamoDBUrbanWordOfTheDayDAO(
99+
get(),
100+
get(named(TABLE_URBAN_WORDS_OF_THE_DAY))
101+
)
102+
}
94103
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const val TABLE_MONIES = "TABLE_MONIES"
1414
const val TABLE_PINS = "TABLE_PINS"
1515
const val TABLE_TIMEZONES = "TABLE_TIMEZONES"
1616
const val TABLE_LANGUAGE_ROOMS = "TABLE_LANGUAGE_ROOMS"
17+
const val TABLE_URBAN_WORDS_OF_THE_DAY = "TABLE_URBAN_WORDS_OF_THE_DAY"
1718
const val STATE_MACHINE_UNPINS = "STATE_MACHINE_UNPINS"
1819

1920
val envModule = module {
@@ -29,6 +30,7 @@ val envModule = module {
2930
TABLE_PINS,
3031
TABLE_TIMEZONES,
3132
TABLE_LANGUAGE_ROOMS,
33+
TABLE_URBAN_WORDS_OF_THE_DAY,
3234
STATE_MACHINE_UNPINS,
3335
).forEach { variable ->
3436
single(named(variable)) {

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import by.jprof.telegram.bot.core.UpdateProcessingPipeline
44
import by.jprof.telegram.bot.core.UpdateProcessor
55
import by.jprof.telegram.bot.currencies.CurrenciesUpdateProcessor
66
import by.jprof.telegram.bot.english.EnglishCommandUpdateProcessor
7+
import by.jprof.telegram.bot.english.UrbanWordOfTheDayUpdateProcessor
78
import by.jprof.telegram.bot.eval.EvalUpdateProcessor
89
import by.jprof.telegram.bot.jep.JEPUpdateProcessor
910
import by.jprof.telegram.bot.jep.JsoupJEPSummary
@@ -153,4 +154,12 @@ val pipelineModule = module {
153154
bot = get(),
154155
)
155156
}
157+
158+
single<UpdateProcessor>(named("UrbanWordOfTheDayUpdateProcessor")) {
159+
UrbanWordOfTheDayUpdateProcessor(
160+
languageRoomDAO = get(),
161+
urbanWordOfTheDayDAO = get(),
162+
bot = get(),
163+
)
164+
}
156165
}

0 commit comments

Comments
 (0)