Skip to content

Commit 25c647b

Browse files
committed
WIP
1 parent 643d285 commit 25c647b

File tree

9 files changed

+33
-25
lines changed

9 files changed

+33
-25
lines changed

.deploy/lambda/lib/JProfByBotStack.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ export class JProfByBotStack extends cdk.Stack {
113113
code: lambda.Code.fromAsset('../../pins/unpin/build/libs/jprof_by_bot-pins-unpin-all.jar'),
114114
handler: 'by.jprof.telegram.bot.pins.unpin.Handler',
115115
environment: {
116+
'JAVA_TOOL_OPTIONS': '-Dsoftware.amazon.awssdk.http.async.service.impl=software.amazon.awssdk.http.nio.netty.NettySdkAsyncHttpService',
116117
'LOG_THRESHOLD': 'INFO',
117118
'TABLE_PINS': pinsTable.tableName,
118119
'TOKEN_TELEGRAM_BOT': props.telegramToken,
@@ -160,10 +161,11 @@ export class JProfByBotStack extends cdk.Stack {
160161
timeout: lambdaWebhookTimeout,
161162
maxEventAge: cdk.Duration.minutes(5),
162163
retryAttempts: 0,
163-
memorySize: 512,
164+
memorySize: 768,
164165
code: lambda.Code.fromAsset('../../launchers/lambda/build/libs/jprof_by_bot-launchers-lambda-all.jar'),
165166
handler: 'by.jprof.telegram.bot.launchers.lambda.JProf',
166167
environment: {
168+
'JAVA_TOOL_OPTIONS': '-Dsoftware.amazon.awssdk.http.async.service.impl=software.amazon.awssdk.http.nio.netty.NettySdkAsyncHttpService',
167169
'LOG_THRESHOLD': 'INFO',
168170
'TABLE_VOTES': votesTable.tableName,
169171
'TABLE_YOUTUBE_CHANNELS_WHITELIST': youtubeChannelsWhitelistTable.tableName,
@@ -195,6 +197,7 @@ export class JProfByBotStack extends cdk.Stack {
195197
code: lambda.Code.fromAsset('../../english/urban-dictionary-daily/build/libs/jprof_by_bot-english-urban-dictionary-daily-all.jar'),
196198
handler: 'by.jprof.telegram.bot.english.urban_dictionary_daily.Handler',
197199
environment: {
200+
'JAVA_TOOL_OPTIONS': '-Dsoftware.amazon.awssdk.http.async.service.impl=software.amazon.awssdk.http.nio.netty.NettySdkAsyncHttpService',
198201
'LOG_THRESHOLD': 'INFO',
199202
'TABLE_URBAN_WORDS_OF_THE_DAY': urbanWordsOfTheDayTable.tableName,
200203
'TABLE_LANGUAGE_ROOMS': languageRoomsTable.tableName,

build.gradle.kts

+6
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ subprojects {
2929
withType<Jar> {
3030
// Workaround for https://stackoverflow.com/q/42174572/750510
3131
archiveBaseName.set(rootProject.name + "-" + this.project.path.removePrefix(":").replace(":", "-"))
32+
manifest {
33+
attributes["Multi-Release"] = true
34+
}
3235
}
3336
withType<Test> {
3437
useJUnitPlatform {
@@ -40,6 +43,9 @@ subprojects {
4043
}
4144
withType<ShadowJar> {
4245
transform(Log4j2PluginsCacheFileTransformer::class.java)
46+
manifest {
47+
attributes["Multi-Release"] = true
48+
}
4349
}
4450
}
4551
}

english/src/main/kotlin/by/jprof/telegram/bot/english/EnglishCommandUpdateProcessor.kt

+4-1
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@ import dev.inmo.tgbotapi.requests.abstracts.MultipartFile
1818
import dev.inmo.tgbotapi.types.chat.member.AdministratorChatMember
1919
import dev.inmo.tgbotapi.types.message.MarkdownV2ParseMode
2020
import dev.inmo.tgbotapi.types.update.abstracts.Update
21+
import dev.inmo.tgbotapi.utils.PreviewFeature
22+
import dev.inmo.tgbotapi.utils.RiskFeature
2123
import io.ktor.utils.io.streams.asInput
2224
import org.apache.logging.log4j.LogManager
2325

26+
@OptIn(PreviewFeature::class, RiskFeature::class)
2427
class EnglishCommandUpdateProcessor(
2528
private val languageRoomDAO: LanguageRoomDAO,
2629
private val bot: RequestsExecutor,
@@ -30,7 +33,7 @@ class EnglishCommandUpdateProcessor(
3033
}
3134

3235
override suspend fun process(update: Update) {
33-
val update = update.asBaseMessageUpdate() ?: return
36+
@Suppress("NAME_SHADOWING") val update = update.asBaseMessageUpdate() ?: return
3437
val message = update.data.asContentMessage() ?: return
3538
val content = message.content.asTextContent() ?: return
3639
val (_, argument) = (content.textSources + null)

english/src/main/kotlin/by/jprof/telegram/bot/english/ExplainerUpdateProcessor.kt

+5-3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import dev.inmo.tgbotapi.types.message.textsources.link
2828
import dev.inmo.tgbotapi.types.message.textsources.regular
2929
import dev.inmo.tgbotapi.types.message.textsources.underline
3030
import dev.inmo.tgbotapi.types.update.abstracts.Update
31+
import dev.inmo.tgbotapi.utils.PreviewFeature
3132
import java.time.Duration
3233
import kotlinx.coroutines.CoroutineScope
3334
import kotlinx.coroutines.Deferred
@@ -36,6 +37,7 @@ import kotlinx.coroutines.supervisorScope
3637
import kotlinx.coroutines.time.withTimeoutOrNull
3738
import org.apache.logging.log4j.LogManager
3839

40+
@OptIn(PreviewFeature::class)
3941
class ExplainerUpdateProcessor(
4042
private val languageRoomDAO: LanguageRoomDAO,
4143
private val urbanDictionaryClient: UrbanDictionaryClient,
@@ -47,7 +49,7 @@ class ExplainerUpdateProcessor(
4749
}
4850

4951
override suspend fun process(update: Update) {
50-
val update = update.asBaseMessageUpdate() ?: return
52+
@Suppress("NAME_SHADOWING") val update = update.asBaseMessageUpdate() ?: return
5153
val roomId = update.data.chat.id
5254
val message = update.data.asContentMessage() ?: return
5355
val content = message.content.asTextContent() ?: return
@@ -60,7 +62,7 @@ class ExplainerUpdateProcessor(
6062

6163
val emphasizedWords = extractEmphasizedWords(content)
6264

63-
logger.debug("Emphasized words: $emphasizedWords")
65+
logger.info("Emphasized words: $emphasizedWords")
6466

6567
val explanations = fetchExplanations(emphasizedWords)
6668

@@ -151,7 +153,7 @@ class ExplainerUpdateProcessor(
151153

152154
private fun StringBuilder.dictionaryDotDevExplanations(dictionaryDotDevExplanations: Collection<Word>?) {
153155
dictionaryDotDevExplanations?.let { definitions ->
154-
definitions.take(3).forEachIndexed { index, definition ->
156+
definitions.take(3).forEachIndexed { _, definition ->
155157
val link = definition.sourceUrls?.firstOrNull()
156158

157159
if (link != null) {

english/src/main/kotlin/by/jprof/telegram/bot/english/MotherfuckingUpdateProcessor.kt

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ import dev.inmo.tgbotapi.extensions.utils.asContentMessage
1313
import dev.inmo.tgbotapi.extensions.utils.asTextContent
1414
import dev.inmo.tgbotapi.requests.abstracts.MultipartFile
1515
import dev.inmo.tgbotapi.types.update.abstracts.Update
16+
import dev.inmo.tgbotapi.utils.PreviewFeature
1617
import io.ktor.utils.io.streams.asInput
1718
import kotlin.random.Random
1819
import org.apache.logging.log4j.LogManager
1920

21+
@OptIn(PreviewFeature::class)
2022
class MotherfuckingUpdateProcessor(
2123
private val languageRoomDAO: LanguageRoomDAO,
2224
private val bot: RequestsExecutor,
@@ -26,7 +28,7 @@ class MotherfuckingUpdateProcessor(
2628
}
2729

2830
override suspend fun process(update: Update) {
29-
val update = update.asBaseMessageUpdate() ?: return
31+
@Suppress("NAME_SHADOWING") val update = update.asBaseMessageUpdate() ?: return
3032
val roomId = update.data.chat.id
3133
val message = update.data.asContentMessage() ?: return
3234
val content = message.content.asTextContent() ?: return

english/src/main/kotlin/by/jprof/telegram/bot/english/UrbanWordOfTheDayUpdateProcessor.kt

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ import dev.inmo.tgbotapi.extensions.utils.asContentMessage
1414
import dev.inmo.tgbotapi.extensions.utils.asTextContent
1515
import dev.inmo.tgbotapi.types.message.MarkdownV2
1616
import dev.inmo.tgbotapi.types.update.abstracts.Update
17+
import dev.inmo.tgbotapi.utils.PreviewFeature
1718
import java.time.LocalDate
1819
import org.apache.logging.log4j.LogManager
1920

21+
@OptIn(PreviewFeature::class)
2022
class UrbanWordOfTheDayUpdateProcessor(
2123
private val languageRoomDAO: LanguageRoomDAO,
2224
private val urbanWordOfTheDayDAO: UrbanWordOfTheDayDAO,
@@ -27,7 +29,7 @@ class UrbanWordOfTheDayUpdateProcessor(
2729
}
2830

2931
override suspend fun process(update: Update) {
30-
val update = update.asBaseMessageUpdate() ?: return
32+
@Suppress("NAME_SHADOWING") val update = update.asBaseMessageUpdate() ?: return
3133
val roomId = update.data.chat.id
3234
val message = update.data.asContentMessage() ?: return
3335
val content = message.content.asTextContent() ?: return

english/src/main/kotlin/by/jprof/telegram/bot/english/WhatWordUpdateProcessor.kt

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ import dev.inmo.tgbotapi.extensions.utils.asContentMessage
1111
import dev.inmo.tgbotapi.extensions.utils.asTextContent
1212
import dev.inmo.tgbotapi.requests.abstracts.MultipartFile
1313
import dev.inmo.tgbotapi.types.update.abstracts.Update
14+
import dev.inmo.tgbotapi.utils.PreviewFeature
1415
import io.ktor.utils.io.streams.asInput
1516
import org.apache.logging.log4j.LogManager
1617

18+
@OptIn(PreviewFeature::class)
1719
class WhatWordUpdateProcessor(
1820
private val languageRoomDAO: LanguageRoomDAO,
1921
private val bot: RequestsExecutor,
@@ -23,7 +25,7 @@ class WhatWordUpdateProcessor(
2325
}
2426

2527
override suspend fun process(update: Update) {
26-
val update = update.asBaseMessageUpdate() ?: return
28+
@Suppress("NAME_SHADOWING") val update = update.asBaseMessageUpdate() ?: return
2729
val roomId = update.data.chat.id
2830
val message = update.data.asContentMessage() ?: return
2931
val content = message.content.asTextContent() ?: return

shop/src/main/kotlin/by/jprof/telegram/bot/shop/SupportSuccessfulPaymentUpdateProcessor.kt

-7
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,15 @@ class SupportSuccessfulPaymentUpdateProcessor(
2525
}
2626

2727
override suspend fun process(update: Update) {
28-
logger.info("1")
2928
val message = update.asMessageUpdate()?.data?.asPossiblyPaymentMessage() ?: return
30-
logger.info("2")
3129
val user = (message as? FromUser)?.user ?: return
32-
logger.info("3")
3330
val payment = (message.paymentInfo as? SuccessfulPaymentEvent)?.payment ?: return
34-
logger.info("4")
3531
val payload = try {
36-
logger.info("5")
3732
json.decodeFromString<Payload>(payment.invoicePayload) as SupportPayload
3833
} catch (_: Exception) {
39-
logger.info("6")
4034
return
4135
}
4236

43-
logger.info("7")
4437
bot.reply(message, "Thank you for the donation!")
4538
}
4639
}

youtube/src/main/kotlin/by/jprof/telegram/bot/youtube/YouTubeUpdateProcessor.kt

+5-10
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class YouTubeUpdateProcessor(
4343
companion object {
4444
private val logger = LogManager.getLogger(YouTubeUpdateProcessor::class.java)!!
4545
private val linkRegex =
46-
"^.*((youtu.be/)|(v/)|(/u/\\w/)|(embed/)|(watch\\?))\\??v?=?([^#&?]*).*".toRegex()
46+
"https?://(?:m.)?(?:www\\.)?youtu(?:\\.be/|(?:be-nocookie|be)\\.com/(?:watch|\\w+\\?(?:feature=\\w+.\\w+&)?v=|v/|e/|embed/|user/(?:[\\w#]+/)+))(?<id>[^&#?\\n]+)".toRegex()
4747
private const val ACCEPTED_DISPLAY_LEN = 500
4848
}
4949

@@ -55,11 +55,9 @@ class YouTubeUpdateProcessor(
5555
}
5656

5757
private suspend fun processMessage(message: Message) {
58-
logger.debug("Processing message: {}", message)
59-
6058
val youTubeVideos = extractYoutubeVideos(message) ?: return
6159

62-
logger.debug("YouTube videos: {}", youTubeVideos)
60+
logger.info("Detected {} YouTube videos: {}", youTubeVideos.size, youTubeVideos)
6361

6462
supervisorScope {
6563
youTubeVideos
@@ -76,20 +74,17 @@ class YouTubeUpdateProcessor(
7674
.mapNotNull {
7775
(it as? URLTextSource)?.source ?: (it as? TextLinkTextSource)?.url
7876
}
79-
.mapNotNull {
80-
linkRegex.matchEntire(it)?.destructured
77+
.mapNotNull { url ->
78+
linkRegex.matchEntire(url)?.groups?.get("id")?.value?.takeUnless { it.isBlank() }
8179
}
82-
.map { (_, _, _, _, _, _, id) -> id }
8380
}
8481
}
8582

8683
private suspend fun replyToYouTubeVideo(video: String, message: Message) {
87-
logger.debug("YouTube video ID: {}", video)
88-
8984
val response = withContext(Dispatchers.IO) {
9085
youTube.videos().list(listOf("snippet", "statistics")).setId(listOf(video)).execute()
9186
}
92-
val videoDetails = response.items.first()
87+
val videoDetails = response.items.firstOrNull() ?: return
9388
val snippet = videoDetails.snippet
9489
val channelId = snippet.channelId
9590

0 commit comments

Comments
 (0)