Skip to content

Commit 68154fd

Browse files
authored
Merge pull request #4 from madhead/feature/YouTube
Votes for YouTube videos
2 parents 8bfd242 + 80d1125 commit 68154fd

File tree

36 files changed

+646
-71
lines changed

36 files changed

+646
-71
lines changed

.deploy/lambda/bin/JProfByBotStack.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,21 @@ import 'source-map-support/register';
33
import * as cdk from '@aws-cdk/core';
44
import { JProfByBotStack } from '../lib/JProfByBotStack';
55

6-
if (process.env.TELEGRAM_BOT_TOKEN == null) {
7-
throw new Error('Undefined TELEGRAM_BOT_TOKEN')
6+
if (process.env.TOKEN_TELEGRAM_BOT == null) {
7+
throw new Error('Undefined TOKEN_TELEGRAM_BOT')
8+
}
9+
10+
if (process.env.TOKEN_YOUTUBE_API == null) {
11+
throw new Error('Undefined TOKEN_YOUTUBE_API')
812
}
913

1014
const app = new cdk.App();
1115
new JProfByBotStack(
1216
app,
1317
'JProfByBotStack',
1418
{
15-
telegramToken: process.env.TELEGRAM_BOT_TOKEN,
19+
telegramToken: process.env.TOKEN_TELEGRAM_BOT,
20+
youtubeToken: process.env.TOKEN_YOUTUBE_API,
1621
env: {
1722
region: 'us-east-1'
1823
}

.deploy/lambda/lib/JProfByBotStack.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ export class JProfByBotStack extends cdk.Stack {
1515
billingMode: dynamodb.BillingMode.PAY_PER_REQUEST,
1616
removalPolicy: RemovalPolicy.DESTROY,
1717
});
18+
const youtubeChannelsWhitelistTable = new dynamodb.Table(this, 'jprof-by-bot-table-youtube-channels-whitelist', {
19+
tableName: 'jprof-by-bot-table-youtube-channels-whitelist',
20+
partitionKey: { name: 'id', type: dynamodb.AttributeType.STRING },
21+
billingMode: dynamodb.BillingMode.PAY_PER_REQUEST,
22+
});
1823

1924
const lambdaWebhook = new lambda.Function(this, 'jprof-by-bot-lambda-webhook', {
2025
functionName: 'jprof-by-bot-lambda-webhook',
@@ -26,11 +31,14 @@ export class JProfByBotStack extends cdk.Stack {
2631
environment: {
2732
'LOG_THRESHOLD': 'DEBUG',
2833
'TABLE_VOTES': votesTable.tableName,
29-
'TELEGRAM_BOT_TOKEN': props.telegramToken,
34+
'TABLE_YOUTUBE_CHANNELS_WHITELIST': youtubeChannelsWhitelistTable.tableName,
35+
'TOKEN_TELEGRAM_BOT': props.telegramToken,
36+
'TOKEN_YOUTUBE_API': props.youtubeToken,
3037
},
3138
});
3239

3340
votesTable.grantReadWriteData(lambdaWebhook);
41+
youtubeChannelsWhitelistTable.grantReadData(lambdaWebhook);
3442

3543
const api = new apigateway.RestApi(this, 'jprof-by-bot-api', {
3644
restApiName: 'jprof-by-bot-api',

.deploy/lambda/lib/JProfByBotStackProps.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ import * as cdk from '@aws-cdk/core';
22

33
export interface JProfByBotStackProps extends cdk.StackProps {
44
readonly telegramToken: string;
5+
readonly youtubeToken: string;
56
}

.github/workflows/default.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ jobs:
5555
job-id: db-test
5656
read-only: ${{ github.ref != 'refs/heads/master' }}
5757
- run: votes/dynamodb/src/test/resources/seed.sh
58+
- run: youtube/dynamodb/src/test/resources/seed.sh
5859
- run: ./gradlew clean dbTest
5960
- uses: actions/upload-artifact@v2
6061
if: always()

.github/workflows/deploy.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ jobs:
3939
- run: npx cdk deploy --outputs-file=cdk.out/outputs.json --require-approval=never
4040
working-directory: .deploy/lambda
4141
env:
42-
TELEGRAM_BOT_TOKEN: ${{ secrets.TELEGRAM_BOT_TOKEN }}
42+
TOKEN_TELEGRAM_BOT: ${{ secrets.TOKEN_TELEGRAM_BOT }}
43+
TOKEN_YOUTUBE_API: ${{ secrets.TOKEN_YOUTUBE_API }}
4344
- id: URL
4445
run: echo "::set-output name=URL::$(jq -r '.JProfByBotStack.URL' cdk.out/outputs.json)"
4546
working-directory: .deploy/lambda

core/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ plugins {
33
}
44

55
dependencies {
6-
api(libs.bundles.tgbotapi)
6+
api(libs.tgbotapi.core)
77
implementation(libs.log4j.api)
88

99
testImplementation(libs.junit.jupiter.api)

gradle/libs.versions.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ tgbotapi = "0.33.3"
1414

1515
jsoup = "1.13.1"
1616

17+
google-api-services-youtube = "v3-rev20210410-1.31.0"
18+
1719
log4j = "2.14.1"
1820

1921
junit5 = "5.7.1"
@@ -34,9 +36,12 @@ jackson-databind = { group = "com.fasterxml.jackson.core", name = "jackson-datab
3436

3537
tgbotapi-core = { group = "dev.inmo", name = "tgbotapi.core", version.ref = "tgbotapi" }
3638
tgbotapi-extensions-api = { group = "dev.inmo", name = "tgbotapi.extensions.api", version.ref = "tgbotapi" }
39+
tgbotapi-extensions-utils = { group = "dev.inmo", name = "tgbotapi.extensions.utils", version.ref = "tgbotapi" }
3740

3841
jsoup = { group = "org.jsoup", name = "jsoup", version.ref = "jsoup" }
3942

43+
google-api-services-youtube = { group = "com.google.apis", name = "google-api-services-youtube", version.ref = "google-api-services-youtube" }
44+
4045
log4j-api = { group = "org.apache.logging.log4j", name = "log4j-api", version.ref = "log4j" }
4146
log4j-core = { group = "org.apache.logging.log4j", name = "log4j-core", version.ref = "log4j" }
4247
log4j-slf4j-impl = { group = "org.apache.logging.log4j", name = "log4j-slf4j-impl", version.ref = "log4j" }

jep/build.gradle.kts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ plugins {
33
}
44

55
dependencies {
6-
api(project(":core"))
7-
api(project(":votes"))
8-
implementation(project(":votes:tgbotapi-extensions"))
6+
api(project.projects.core)
7+
api(project.projects.votes)
8+
api(project.projects.votes.votingProcessor)
9+
api(libs.tgbotapi.core)
10+
implementation(project.projects.votes.tgbotapiExtensions)
11+
implementation(libs.tgbotapi.extensions.api)
912
implementation(libs.log4j.api)
1013
implementation(libs.jsoup)
1114

jep/src/main/kotlin/by/jprof/telegram/bot/jep/JEPUpdateProcessor.kt

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,10 @@ import by.jprof.telegram.bot.core.UpdateProcessor
44
import by.jprof.telegram.bot.votes.dao.VotesDAO
55
import by.jprof.telegram.bot.votes.model.Votes
66
import by.jprof.telegram.bot.votes.tgbotapi_extensions.toInlineKeyboardMarkup
7+
import by.jprof.telegram.bot.votes.voting_processor.VotingProcessor
78
import dev.inmo.tgbotapi.CommonAbstracts.justTextSources
89
import dev.inmo.tgbotapi.bot.RequestsExecutor
9-
import dev.inmo.tgbotapi.extensions.api.answers.answerCallbackQuery
10-
import dev.inmo.tgbotapi.extensions.api.edit.ReplyMarkup.editMessageReplyMarkup
1110
import dev.inmo.tgbotapi.extensions.api.send.reply
12-
import dev.inmo.tgbotapi.types.CallbackQuery.CallbackQuery
13-
import dev.inmo.tgbotapi.types.CallbackQuery.MessageDataCallbackQuery
1411
import dev.inmo.tgbotapi.types.MessageEntity.textsources.TextLinkTextSource
1512
import dev.inmo.tgbotapi.types.MessageEntity.textsources.URLTextSource
1613
import dev.inmo.tgbotapi.types.ParseMode.MarkdownV2ParseMode
@@ -26,14 +23,21 @@ import kotlinx.coroutines.launch
2623
import kotlinx.coroutines.supervisorScope
2724
import org.apache.logging.log4j.LogManager
2825

26+
private fun votesConstructor(votesId: String): Votes = Votes(votesId, listOf("\uD83D\uDC4D", "\uD83D\uDC4E"))
27+
2928
class JEPUpdateProcessor(
3029
private val jepSummary: JEPSummary,
3130
private val votesDAO: VotesDAO,
3231
private val bot: RequestsExecutor,
33-
) : UpdateProcessor {
32+
) : VotingProcessor(
33+
"JEP",
34+
votesDAO,
35+
::votesConstructor,
36+
bot,
37+
), UpdateProcessor {
3438
companion object {
35-
val logger = LogManager.getLogger(JEPUpdateProcessor::class.java)!!
36-
val linkRegex = "https?://openjdk\\.java\\.net/jeps/(\\d+)/?".toRegex()
39+
private val logger = LogManager.getLogger(JEPUpdateProcessor::class.java)!!
40+
private val linkRegex = "https?://openjdk\\.java\\.net/jeps/(\\d+)/?".toRegex()
3741
}
3842

3943
override suspend fun process(update: Update) {
@@ -57,27 +61,6 @@ class JEPUpdateProcessor(
5761
}
5862
}
5963

60-
private suspend fun processCallbackQuery(callbackQuery: CallbackQuery) {
61-
logger.debug("Processing callback query: {}", callbackQuery)
62-
63-
(callbackQuery as? MessageDataCallbackQuery)?.data?.takeIf { it.startsWith("JEP") }?.let { data ->
64-
val (votesId, vote) = data.split(":").takeIf { it.size == 2 } ?: return
65-
val fromUserId = callbackQuery.user.id.chatId.toString()
66-
67-
logger.debug("Tracking {}'s '{}' vote for {}", fromUserId, vote, votesId)
68-
69-
val votes = votesDAO.get(votesId) ?: votesId.toVotes()
70-
val updatedVotes = votes.copy(votes = votes.votes + (fromUserId to vote))
71-
72-
votesDAO.save(updatedVotes)
73-
bot.answerCallbackQuery(callbackQuery)
74-
bot.editMessageReplyMarkup(
75-
message = callbackQuery.message,
76-
replyMarkup = updatedVotes.toInlineKeyboardMarkup()
77-
)
78-
}
79-
}
80-
8164
private fun extractJEPs(message: Message): List<String>? =
8265
(message as? ContentMessage<*>)?.let { contentMessage ->
8366
(contentMessage.content as? TextContent)?.let { content ->
@@ -107,7 +90,7 @@ class JEPUpdateProcessor(
10790
"Cast your vote for *JEP $jep* now ⤵️"
10891
}
10992
val votesId = jep.toVotesID()
110-
val votes = votesDAO.get(votesId) ?: votesId.toVotes()
93+
val votes = votesDAO.get(votesId) ?: votesConstructor(votesId)
11194

11295
bot.reply(
11396
to = message,
@@ -116,8 +99,4 @@ class JEPUpdateProcessor(
11699
replyMarkup = votes.toInlineKeyboardMarkup()
117100
)
118101
}
119-
120-
private fun String.toVotesID() = "JEP-$this"
121-
122-
private fun String.toVotes() = Votes(this, listOf("\uD83D\uDC4D", "\uD83D\uDC4E"))
123102
}

runners/lambda/build.gradle.kts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ dependencies {
88
implementation(libs.koin.core)
99
implementation(libs.bundles.tgbotapi)
1010
implementation(libs.bundles.log4j)
11-
implementation(project(":core"))
12-
implementation(project(":votes:dynamodb"))
13-
implementation(project(":jep"))
11+
implementation(project.projects.core)
12+
implementation(project.projects.votes.dynamodb)
13+
implementation(project.projects.jep)
14+
implementation(project.projects.youtube.dynamodb)
1415
}

0 commit comments

Comments
 (0)