Skip to content

Commit 643d285

Browse files
committed
WIP
1 parent 94b6b98 commit 643d285

File tree

25 files changed

+246
-42
lines changed

25 files changed

+246
-42
lines changed

.deploy/lambda/lib/JProfByBotStack.ts

+12-6
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,13 @@ export class JProfByBotStack extends cdk.Stack {
1515
constructor(scope: Construct, id: string, props: JProfByBotStackProps) {
1616
super(scope, id, props);
1717

18-
const secretPaymentProviderTokens = new secrets.Secret(this, 'jprof-by-bot-secret-payment-provider-tokens');
18+
const secretPaymentProviderTokens = new secrets.Secret(this, 'jprof-by-bot-secret-payment-provider-tokens', {
19+
secretName: 'jprof-by-bot-secret-payment-provider-tokens',
20+
secretObjectValue: {
21+
test: cdk.SecretValue.unsafePlainText('test'),
22+
production: cdk.SecretValue.unsafePlainText('production'),
23+
}
24+
});
1925

2026
const votesTable = new dynamodb.Table(this, 'jprof-by-bot-table-votes', {
2127
tableName: 'jprof-by-bot-table-votes',
@@ -107,7 +113,7 @@ export class JProfByBotStack extends cdk.Stack {
107113
code: lambda.Code.fromAsset('../../pins/unpin/build/libs/jprof_by_bot-pins-unpin-all.jar'),
108114
handler: 'by.jprof.telegram.bot.pins.unpin.Handler',
109115
environment: {
110-
'LOG_THRESHOLD': 'DEBUG',
116+
'LOG_THRESHOLD': 'INFO',
111117
'TABLE_PINS': pinsTable.tableName,
112118
'TOKEN_TELEGRAM_BOT': props.telegramToken,
113119
},
@@ -129,12 +135,12 @@ export class JProfByBotStack extends cdk.Stack {
129135
const layerLibGL = new lambda.LayerVersion(this, 'jprof-by-bot-lambda-layer-libGL', {
130136
layerVersionName: 'libGL',
131137
code: lambda.Code.fromAsset('layers/libGL.zip'),
132-
compatibleArchitectures: [Architecture.ARM_64],
138+
compatibleArchitectures: [Architecture.X86_64],
133139
});
134140
const layerLibfontconfig = new lambda.LayerVersion(this, 'jprof-by-bot-lambda-layer-libfontconfig', {
135141
layerVersionName: 'libfontconfig',
136142
code: lambda.Code.fromAsset('layers/libfontconfig.zip'),
137-
compatibleArchitectures: [Architecture.ARM_64],
143+
compatibleArchitectures: [Architecture.X86_64],
138144
});
139145
const layerParametersAndSecretsLambdaExtension = lambda.LayerVersion.fromLayerVersionArn(
140146
this,
@@ -158,7 +164,7 @@ export class JProfByBotStack extends cdk.Stack {
158164
code: lambda.Code.fromAsset('../../launchers/lambda/build/libs/jprof_by_bot-launchers-lambda-all.jar'),
159165
handler: 'by.jprof.telegram.bot.launchers.lambda.JProf',
160166
environment: {
161-
'LOG_THRESHOLD': 'DEBUG',
167+
'LOG_THRESHOLD': 'INFO',
162168
'TABLE_VOTES': votesTable.tableName,
163169
'TABLE_YOUTUBE_CHANNELS_WHITELIST': youtubeChannelsWhitelistTable.tableName,
164170
'TABLE_KOTLIN_MENTIONS': kotlinMentionsTable.tableName,
@@ -189,7 +195,7 @@ export class JProfByBotStack extends cdk.Stack {
189195
code: lambda.Code.fromAsset('../../english/urban-dictionary-daily/build/libs/jprof_by_bot-english-urban-dictionary-daily-all.jar'),
190196
handler: 'by.jprof.telegram.bot.english.urban_dictionary_daily.Handler',
191197
environment: {
192-
'LOG_THRESHOLD': 'DEBUG',
198+
'LOG_THRESHOLD': 'INFO',
193199
'TABLE_URBAN_WORDS_OF_THE_DAY': urbanWordsOfTheDayTable.tableName,
194200
'TABLE_LANGUAGE_ROOMS': languageRoomsTable.tableName,
195201
'TOKEN_TELEGRAM_BOT': props.telegramToken,

core/src/main/kotlin/by/jprof/telegram/bot/core/UpdateProcessingPipeline.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class UpdateProcessingPipeline(
2323
processors
2424
.map {
2525
launch(exceptionHandler(it)) {
26-
logger.debug("Processing update with ${it::class.simpleName}")
26+
logger.trace("Processing update with ${it::class.simpleName}")
2727
it.process(update)
2828
}
2929
}

deploy.sh

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env bash
2+
3+
source .env &&
4+
./gradlew clean shadowJar &&
5+
pushd .deploy/lambda &&
6+
npm install &&
7+
cdk deploy --outputs-file=cdk.out/outputs.json --require-approval=never &&
8+
popd

kotlin/src/main/kotlin/by/jprof/telegram/bot/kotlin/KotlinMentionsUpdateProcessor.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class KotlinMentionsUpdateProcessor(
5050
else -> return
5151
}
5252

53-
logger.info("Kotlin mentioned!")
53+
logger.debug("Kotlin mentioned!")
5454

5555
val now = Instant.now()
5656
val user = (message as? FromUserMessage)?.user ?: return
@@ -128,6 +128,6 @@ class KotlinMentionsUpdateProcessor(
128128
replyToMessageId = message.messageId,
129129
)
130130

131-
logger.info("Kotlin mention reported!")
131+
logger.debug("Kotlin mention reported!")
132132
}
133133
}

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,11 @@ class JProf : RequestHandler<APIGatewayV2HTTPEvent, APIGatewayV2HTTPResponse>, K
6666

6767
override fun handleRequest(input: APIGatewayV2HTTPEvent, context: Context): APIGatewayV2HTTPResponse {
6868
logger.debug("Incoming request: {}", input)
69+
logger.info(input.body)
6970

7071
val update = json.decodeFromString(UpdateDeserializationStrategy, input.body ?: return OK)
7172

72-
logger.debug("Parsed update: {}", update)
73+
logger.info("{}", update)
7374

7475
pipeline.process(update)
7576

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ val envModule = module {
4949
System.getenv(TIMEOUT)!!.toLong()
5050
}
5151

52-
single {
52+
single<ChatProviderTokens> {
5353
val json: Json = get()
5454
val secrets: SecretsManagerClient = get()
5555
val secret = secrets.getSecretValue {
5656
it.secretId(SECRET_PAYMENT_PROVIDER_TOKENS)
5757
}
5858

59-
json.decodeFromString<ChatProviderTokens>(secret.secretString())
59+
json.decodeFromString(secret.secretString())
6060
}
6161
}

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

+27-2
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@ import by.jprof.telegram.bot.quizoji.QuizojiStartCommandUpdateProcessor
2323
import by.jprof.telegram.bot.quizoji.QuizojiVoteUpdateProcessor
2424
import by.jprof.telegram.bot.shop.ForwardedPaymentStartCommandUpdateProcessor
2525
import by.jprof.telegram.bot.shop.PinsPreCheckoutQueryUpdateProcessor
26+
import by.jprof.telegram.bot.shop.PinsSuccessfulPaymentUpdateProcessor
2627
import by.jprof.telegram.bot.shop.RichCommandUpdateProcessor
2728
import by.jprof.telegram.bot.shop.RichPreCheckoutQueryUpdateProcessor
29+
import by.jprof.telegram.bot.shop.RichSuccessfulPaymentUpdateProcessor
2830
import by.jprof.telegram.bot.shop.SupportCommandUpdateProcessor
2931
import by.jprof.telegram.bot.shop.SupportPreCheckoutQueryUpdateProcessor
32+
import by.jprof.telegram.bot.shop.SupportSuccessfulPaymentUpdateProcessor
3033
import by.jprof.telegram.bot.times.TimeCommandUpdateProcessor
3134
import by.jprof.telegram.bot.times.TimeZoneCommandUpdateProcessor
3235
import by.jprof.telegram.bot.youtube.YouTubeUpdateProcessor
@@ -215,6 +218,13 @@ val pipelineModule = module {
215218
)
216219
}
217220

221+
single<UpdateProcessor>(named("RichSuccessfulPaymentUpdateProcessor")) {
222+
RichSuccessfulPaymentUpdateProcessor(
223+
bot = get(),
224+
json = get(),
225+
)
226+
}
227+
218228
single<UpdateProcessor>(named("SupportCommandUpdateProcessor")) {
219229
SupportCommandUpdateProcessor(
220230
bot = get(),
@@ -230,9 +240,10 @@ val pipelineModule = module {
230240
)
231241
}
232242

233-
single<UpdateProcessor>(named("ForwardedPaymentStartCommandUpdateProcessor")) {
234-
ForwardedPaymentStartCommandUpdateProcessor(
243+
single<UpdateProcessor>(named("SupportSuccessfulPaymentUpdateProcessor")) {
244+
SupportSuccessfulPaymentUpdateProcessor(
235245
bot = get(),
246+
json = get(),
236247
)
237248
}
238249

@@ -243,4 +254,18 @@ val pipelineModule = module {
243254
moniesDAO = get(),
244255
)
245256
}
257+
258+
single<UpdateProcessor>(named("PinsSuccessfulPaymentUpdateProcessor")) {
259+
PinsSuccessfulPaymentUpdateProcessor(
260+
bot = get(),
261+
json = get(),
262+
moniesDAO = get(),
263+
)
264+
}
265+
266+
single<UpdateProcessor>(named("ForwardedPaymentStartCommandUpdateProcessor")) {
267+
ForwardedPaymentStartCommandUpdateProcessor(
268+
bot = get(),
269+
)
270+
}
246271
}

pins/src/main/kotlin/by/jprof/telegram/bot/pins/PinCommandUpdateProcessor.kt

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import by.jprof.telegram.bot.pins.utils.negativeDuration
1717
import by.jprof.telegram.bot.pins.utils.tooManyPinnedMessages
1818
import by.jprof.telegram.bot.pins.utils.tooPositiveDuration
1919
import by.jprof.telegram.bot.pins.utils.unrecognizedDuration
20+
import by.jprof.telegram.bot.shop.payload.Payload
2021
import by.jprof.telegram.bot.shop.payload.PinsPayload
2122
import by.jprof.telegram.bot.shop.provider.ChatProviderTokens
2223
import dev.inmo.tgbotapi.bot.RequestsExecutor
@@ -49,7 +50,7 @@ class PinCommandUpdateProcessor(
4950

5051
override suspend fun process(update: Update) {
5152
pinRequestFinder(update)?.let { pin ->
52-
logger.info("Pin requested: {}", pin)
53+
logger.debug("Pin requested: {}", pin)
5354

5455
val monies = moniesDAO.get(pin.user.id.chatId, pin.chat.id.chatId) ?: Monies(pin.user.id.chatId, pin.chat.id.chatId)
5556
val pins = monies.pins ?: 0
@@ -119,7 +120,7 @@ class PinCommandUpdateProcessor(
119120
chatId = pin.request.chat.id,
120121
title = "168 пинов",
121122
description = "Неделя закрепа",
122-
payload = json.encodeToString(PinsPayload(
123+
payload = json.encodeToString<Payload>(PinsPayload(
123124
pins = 168,
124125
chat = pin.request.chat.id.chatId,
125126
)),

pins/src/main/kotlin/by/jprof/telegram/bot/pins/PinReplyUpdateProcessor.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class PinReplyUpdateProcessor(
2828
return
2929
}
3030

31-
logger.info("{} replied to {}", replier, pin)
31+
logger.debug("{} replied to {}", replier, pin)
3232

3333
val monies = moniesDAO.get(pin.userId, replyTo.chat.id.chatId) ?: Monies(pin.userId, replyTo.chat.id.chatId)
3434

shop/payload/src/main/kotlin/by/jprof/telegram/bot/shop/payload/Payload.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ package by.jprof.telegram.bot.shop.payload
33
import kotlinx.serialization.Serializable
44

55
@Serializable
6-
sealed class Payload
6+
sealed interface Payload

shop/payload/src/main/kotlin/by/jprof/telegram/bot/shop/payload/PinsPayload.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ import kotlinx.serialization.Serializable
88
data class PinsPayload(
99
val pins: Long,
1010
val chat: Long,
11-
) : Payload()
11+
) : Payload

shop/payload/src/main/kotlin/by/jprof/telegram/bot/shop/payload/RichPayload.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ import kotlinx.serialization.Serializable
88
data class RichPayload(
99
val status: String,
1010
val chat: Long,
11-
) : Payload()
11+
) : Payload

shop/payload/src/main/kotlin/by/jprof/telegram/bot/shop/payload/SupportPayload.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ import kotlinx.serialization.Serializable
77
@SerialName("support")
88
data class SupportPayload(
99
val chat: Long,
10-
) : Payload()
10+
) : Payload

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

+7-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package by.jprof.telegram.bot.shop
33
import by.jprof.telegram.bot.core.UpdateProcessor
44
import by.jprof.telegram.bot.monies.dao.MoniesDAO
55
import by.jprof.telegram.bot.monies.model.Monies
6+
import by.jprof.telegram.bot.shop.payload.Payload
67
import by.jprof.telegram.bot.shop.payload.PinsPayload
78
import by.jprof.telegram.bot.shop.utils.tooManyPins
89
import dev.inmo.tgbotapi.bot.RequestsExecutor
@@ -27,23 +28,24 @@ class PinsPreCheckoutQueryUpdateProcessor(
2728

2829
override suspend fun process(update: Update) {
2930
val preCheckoutQuery = update.asPreCheckoutQueryUpdate()?.data ?: return
30-
31-
logger.debug(preCheckoutQuery)
32-
3331
val payload = try {
34-
json.decodeFromString<PinsPayload>(preCheckoutQuery.invoicePayload)
32+
json.decodeFromString<Payload>(preCheckoutQuery.invoicePayload) as PinsPayload
3533
} catch (_: Exception) {
3634
return
3735
}
3836

39-
logger.debug(payload)
37+
logger.info("{}", payload)
4038

4139
val monies = moniesDAO.get(preCheckoutQuery.user.id.chatId, payload.chat) ?: Monies(preCheckoutQuery.user.id.chatId, payload.chat)
4240
val pins = monies.pins ?: 0
4341

4442
if (pins > 9999) {
43+
logger.info("{} already has enough ({}) pins!", preCheckoutQuery.user, pins)
44+
4545
bot.answerPreCheckoutQueryError(preCheckoutQuery, tooManyPins())
4646
} else {
47+
logger.info("Selling pins to {}", preCheckoutQuery.user)
48+
4749
bot.answerPreCheckoutQueryOk(preCheckoutQuery)
4850
}
4951
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package by.jprof.telegram.bot.shop
2+
3+
import by.jprof.telegram.bot.core.UpdateProcessor
4+
import by.jprof.telegram.bot.monies.dao.MoniesDAO
5+
import by.jprof.telegram.bot.monies.model.Money
6+
import by.jprof.telegram.bot.monies.model.Monies
7+
import by.jprof.telegram.bot.shop.payload.Payload
8+
import by.jprof.telegram.bot.shop.payload.PinsPayload
9+
import dev.inmo.tgbotapi.abstracts.FromUser
10+
import dev.inmo.tgbotapi.bot.RequestsExecutor
11+
import dev.inmo.tgbotapi.extensions.api.send.reply
12+
import dev.inmo.tgbotapi.extensions.utils.asMessageUpdate
13+
import dev.inmo.tgbotapi.extensions.utils.asPossiblyPaymentMessage
14+
import dev.inmo.tgbotapi.types.message.payments.SuccessfulPaymentEvent
15+
import dev.inmo.tgbotapi.types.update.abstracts.Update
16+
import dev.inmo.tgbotapi.utils.PreviewFeature
17+
import kotlinx.serialization.decodeFromString
18+
import kotlinx.serialization.json.Json
19+
import org.apache.logging.log4j.LogManager
20+
21+
@OptIn(PreviewFeature::class)
22+
class PinsSuccessfulPaymentUpdateProcessor(
23+
private val bot: RequestsExecutor,
24+
private val json: Json,
25+
private val moniesDAO: MoniesDAO,
26+
) : UpdateProcessor {
27+
companion object {
28+
private val logger = LogManager.getLogger(PinsSuccessfulPaymentUpdateProcessor::class.java)!!
29+
}
30+
31+
override suspend fun process(update: Update) {
32+
val message = update.asMessageUpdate()?.data?.asPossiblyPaymentMessage() ?: return
33+
val user = (message as? FromUser)?.user ?: return
34+
val payment = (message.paymentInfo as? SuccessfulPaymentEvent)?.payment ?: return
35+
val payload = try {
36+
json.decodeFromString<Payload>(payment.invoicePayload) as PinsPayload
37+
} catch (_: Exception) {
38+
return
39+
}
40+
41+
val monies = moniesDAO.get(user.id.chatId, payload.chat) ?: Monies(user.id.chatId, payload.chat)
42+
val pins = monies.pins ?: 0
43+
44+
moniesDAO.save(
45+
monies.copy(
46+
monies = monies.monies + (Money.PINS to pins)
47+
)
48+
)
49+
bot.reply(message, "Thank you for the purchase!")
50+
}
51+
}

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package by.jprof.telegram.bot.shop
22

33
import by.jprof.telegram.bot.core.UpdateProcessor
4+
import by.jprof.telegram.bot.shop.payload.Payload
45
import by.jprof.telegram.bot.shop.payload.RichPayload
56
import by.jprof.telegram.bot.shop.provider.ChatProviderTokens
67
import by.jprof.telegram.bot.shop.utils.notAShop
@@ -53,7 +54,7 @@ class RichCommandUpdateProcessor(
5354
chatId = message.chat.id,
5455
title = richInvoiceTitle(status),
5556
description = richInvoiceDescription(),
56-
payload = json.encodeToString(RichPayload(status = status, chat = message.chat.id.chatId)),
57+
payload = json.encodeToString<Payload>(RichPayload(status = status, chat = message.chat.id.chatId)),
5758
providerToken = chatProviderToken,
5859
currency = currency,
5960
prices = listOf(item),

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

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package by.jprof.telegram.bot.shop
22

33
import by.jprof.telegram.bot.core.UpdateProcessor
4+
import by.jprof.telegram.bot.shop.payload.Payload
45
import by.jprof.telegram.bot.shop.payload.RichPayload
56
import dev.inmo.tgbotapi.bot.RequestsExecutor
67
import dev.inmo.tgbotapi.extensions.api.answers.payments.answerPreCheckoutQueryOk
@@ -22,16 +23,13 @@ class RichPreCheckoutQueryUpdateProcessor(
2223

2324
override suspend fun process(update: Update) {
2425
val preCheckoutQuery = update.asPreCheckoutQueryUpdate()?.data ?: return
25-
26-
logger.debug(preCheckoutQuery)
27-
2826
val payload = try {
29-
json.decodeFromString<RichPayload>(preCheckoutQuery.invoicePayload)
27+
json.decodeFromString<Payload>(preCheckoutQuery.invoicePayload) as RichPayload
3028
} catch (_: Exception) {
3129
return
3230
}
3331

34-
logger.debug(payload)
32+
logger.info("{}", payload)
3533

3634
bot.answerPreCheckoutQueryOk(preCheckoutQuery)
3735
}

0 commit comments

Comments
 (0)