-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
23 changed files
with
210 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
...a/src/main/kotlin/ru/vityaman/lms/botalka/app/spring/client/telegram/SpringTelegramBot.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package ru.vityaman.lms.botalka.app.spring.client.telegram | ||
|
||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.runBlocking | ||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.boot.context.event.ApplicationReadyEvent | ||
import org.springframework.context.annotation.Profile | ||
import org.springframework.context.event.EventListener | ||
import org.springframework.stereotype.Service | ||
import ru.vityaman.lms.botalka.core.external.telegram.BasicTelegramBot | ||
import ru.vityaman.lms.botalka.core.external.telegram.TelegramBot | ||
import ru.vityaman.lms.botalka.core.external.telegram.TelegramChat | ||
import ru.vityaman.lms.botalka.core.external.telegram.TelegramMessage | ||
|
||
@Service | ||
@Profile("production") | ||
class SpringTelegramBot( | ||
@Value("\${external.service.telegram.token}") | ||
token: String, | ||
|
||
@Value("\${external.service.telegram.admin-chat-id}") | ||
adminChatId: Long, | ||
) : TelegramBot by BasicTelegramBot(token) { | ||
private val scope = CoroutineScope(Dispatchers.Default) | ||
private val adminChat = TelegramChat(adminChatId) | ||
|
||
init { | ||
scope.launch { | ||
start() | ||
} | ||
} | ||
|
||
@EventListener(ApplicationReadyEvent::class) | ||
fun notifyAdmin() = runBlocking { | ||
send(adminChat, TelegramMessage("Restarted instance")) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
botalka/src/main/kotlin/ru/vityaman/lms/botalka/app/spring/task/SpringPublicationConfig.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package ru.vityaman.lms.botalka.app.spring.task | ||
|
||
class SpringPublicationConfig { | ||
object BeanName { | ||
const val KAFKA_CONSUMER = "kafkaPublicationConsumer" | ||
const val TELEGRAM_CONSUMER = "telegramPublicationConsumer" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
.../main/kotlin/ru/vityaman/lms/botalka/app/spring/task/SpringTelegramPublicationConsumer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package ru.vityaman.lms.botalka.app.spring.task | ||
|
||
import org.springframework.beans.factory.annotation.Qualifier | ||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.stereotype.Component | ||
import ru.vityaman.lms.botalka.core.external.telegram.TelegramBot | ||
import ru.vityaman.lms.botalka.core.external.telegram.TelegramChat | ||
import ru.vityaman.lms.botalka.core.publication.PublicationConsumer | ||
import ru.vityaman.lms.botalka.core.publication.telegram.TelegramPublicationConsumer | ||
|
||
@Component | ||
@Qualifier(SpringPublicationConfig.BeanName.TELEGRAM_CONSUMER) | ||
class SpringTelegramPublicationConsumer( | ||
telegram: TelegramBot, | ||
|
||
@Value("\${external.service.telegram.admin-chat-id}") | ||
adminChatId: Long, | ||
) : PublicationConsumer by | ||
TelegramPublicationConsumer(telegram, TelegramChat(adminChatId)) |
42 changes: 42 additions & 0 deletions
42
botalka/src/main/kotlin/ru/vityaman/lms/botalka/core/external/telegram/BasicTelegramBot.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package ru.vityaman.lms.botalka.core.external.telegram | ||
|
||
import com.github.kotlintelegrambot.bot | ||
import com.github.kotlintelegrambot.dispatch | ||
import com.github.kotlintelegrambot.dispatcher.text | ||
import com.github.kotlintelegrambot.entities.ChatId | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
|
||
class BasicTelegramBot(accessToken: String) : TelegramBot { | ||
private val bot = bot { | ||
token = accessToken | ||
dispatch { | ||
text { | ||
send( | ||
TelegramChat(message.chat.id), | ||
TelegramMessage( | ||
text = mapOf( | ||
"Chat" to message.chat.id, | ||
"From" to message.from?.id, | ||
"Text" to message.text, | ||
).entries.joinToString(", ") { | ||
"${it.key}: ${it.value}" | ||
}, | ||
), | ||
) | ||
} | ||
} | ||
} | ||
|
||
override suspend fun send(chat: TelegramChat, message: TelegramMessage) { | ||
withContext(Dispatchers.IO) { | ||
bot.sendMessage(ChatId.fromId(chat.id), text = message.text).get() | ||
} | ||
} | ||
|
||
override suspend fun start() { | ||
withContext(Dispatchers.IO) { | ||
bot.startPolling() | ||
} | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
botalka/src/main/kotlin/ru/vityaman/lms/botalka/core/external/telegram/TelegramBot.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package ru.vityaman.lms.botalka.core.external.telegram | ||
|
||
interface TelegramBot { | ||
suspend fun send(chat: TelegramChat, message: TelegramMessage) | ||
suspend fun start() | ||
} |
3 changes: 3 additions & 0 deletions
3
botalka/src/main/kotlin/ru/vityaman/lms/botalka/core/external/telegram/TelegramChat.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package ru.vityaman.lms.botalka.core.external.telegram | ||
|
||
data class TelegramChat(val id: Long) |
3 changes: 3 additions & 0 deletions
3
botalka/src/main/kotlin/ru/vityaman/lms/botalka/core/external/telegram/TelegramMessage.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package ru.vityaman.lms.botalka.core.external.telegram | ||
|
||
data class TelegramMessage(val text: String) |
28 changes: 28 additions & 0 deletions
28
...n/kotlin/ru/vityaman/lms/botalka/core/publication/telegram/TelegramPublicationConsumer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package ru.vityaman.lms.botalka.core.publication.telegram | ||
|
||
import kotlinx.coroutines.delay | ||
import ru.vityaman.lms.botalka.core.external.telegram.TelegramBot | ||
import ru.vityaman.lms.botalka.core.external.telegram.TelegramChat | ||
import ru.vityaman.lms.botalka.core.external.telegram.TelegramMessage | ||
import ru.vityaman.lms.botalka.core.model.Homework | ||
import ru.vityaman.lms.botalka.core.publication.PublicationConsumer | ||
import kotlin.time.Duration.Companion.seconds | ||
|
||
class TelegramPublicationConsumer( | ||
private val telegram: TelegramBot, | ||
private val adminChat: TelegramChat, | ||
) : PublicationConsumer { | ||
override suspend fun accept(value: Homework) { | ||
val text = buildString { | ||
append("Published homework '${value.title.text}'!\n") | ||
append("\n") | ||
append("${value.description}\n") | ||
append("\n") | ||
append("MaxScore: ${value.maxScore.value}\n") | ||
append("Deadline: ${value.deadlineMoment}\n") | ||
append("Id: ${value.id.number}\n") | ||
} | ||
telegram.send(adminChat, TelegramMessage(text)) | ||
delay(1.seconds) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
...lka/src/test/kotlin/ru/vityaman/lms/botalka/app/spring/client/telegram/FakeTelegramBot.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package ru.vityaman.lms.botalka.app.spring.client.telegram | ||
|
||
import org.slf4j.LoggerFactory | ||
import org.springframework.context.annotation.Primary | ||
import org.springframework.stereotype.Service | ||
import ru.vityaman.lms.botalka.core.external.telegram.TelegramBot | ||
import ru.vityaman.lms.botalka.core.external.telegram.TelegramChat | ||
import ru.vityaman.lms.botalka.core.external.telegram.TelegramMessage | ||
|
||
@Primary | ||
@Service | ||
class FakeTelegramBot : TelegramBot { | ||
private val log = LoggerFactory.getLogger("FakeTelegramBot") | ||
|
||
override suspend fun send(chat: TelegramChat, message: TelegramMessage) { | ||
log.debug("Received a {} from {}", message, chat) | ||
} | ||
|
||
override suspend fun start() { | ||
log.debug("Started") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,4 +9,5 @@ version = "0.0.1" | |
|
||
repositories { | ||
mavenCentral() | ||
maven(url = "https://jitpack.io") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#!/usr/bin/sh | ||
|
||
set -e | ||
|
||
cd "$(dirname "$0")"/.. || exit | ||
|
||
echo "[up] Setting up local variables..." | ||
. ./infra/env/local.sh | ||
|
||
echo "[up] Setting up secrets..." | ||
. ./infra/env/secret.sh | ||
|
||
docker compose down |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters