|
| 1 | +package by.jprof.telegram.bot.english.urban_dictionary_daily |
| 2 | + |
| 3 | +import by.jprof.telegram.bot.english.language_rooms.dao.LanguageRoomDAO |
| 4 | +import by.jprof.telegram.bot.english.language_rooms.model.Language |
| 5 | +import by.jprof.telegram.bot.english.urban_dictionary.UrbanDictionaryClient |
| 6 | +import by.jprof.telegram.bot.english.urban_dictionary_daily.config.databaseModule |
| 7 | +import by.jprof.telegram.bot.english.urban_dictionary_daily.config.envModule |
| 8 | +import by.jprof.telegram.bot.english.urban_dictionary_daily.config.jsonModule |
| 9 | +import by.jprof.telegram.bot.english.urban_dictionary_daily.config.sfnModule |
| 10 | +import by.jprof.telegram.bot.english.urban_dictionary_daily.config.telegramModule |
| 11 | +import by.jprof.telegram.bot.english.urban_dictionary_daily.config.urbanDictionaryModule |
| 12 | +import by.jprof.telegram.bot.english.urban_dictionary_daily.model.Event |
| 13 | +import by.jprof.telegram.bot.english.urban_dictionary_definition_formatter.format |
| 14 | +import by.jprof.telegram.bot.english.urban_word_of_the_day.dao.UrbanWordOfTheDayDAO |
| 15 | +import by.jprof.telegram.bot.english.urban_word_of_the_day.model.UrbanWordOfTheDay |
| 16 | +import by.jprof.telegram.bot.pins.dto.Unpin |
| 17 | +import by.jprof.telegram.bot.pins.scheduler.UnpinScheduler |
| 18 | +import com.amazonaws.services.lambda.runtime.Context |
| 19 | +import com.amazonaws.services.lambda.runtime.RequestStreamHandler |
| 20 | +import dev.inmo.tgbotapi.bot.RequestsExecutor |
| 21 | +import dev.inmo.tgbotapi.extensions.api.chat.modify.pinChatMessage |
| 22 | +import dev.inmo.tgbotapi.extensions.api.send.sendMessage |
| 23 | +import dev.inmo.tgbotapi.types.ChatId |
| 24 | +import dev.inmo.tgbotapi.types.ChatIdWithThreadId |
| 25 | +import dev.inmo.tgbotapi.types.message.MarkdownV2 |
| 26 | +import java.io.InputStream |
| 27 | +import java.io.OutputStream |
| 28 | +import java.time.LocalDate |
| 29 | +import kotlinx.coroutines.runBlocking |
| 30 | +import kotlinx.serialization.ExperimentalSerializationApi |
| 31 | +import kotlinx.serialization.json.Json |
| 32 | +import kotlinx.serialization.json.decodeFromStream |
| 33 | +import org.apache.logging.log4j.LogManager |
| 34 | +import org.koin.core.component.KoinComponent |
| 35 | +import org.koin.core.component.inject |
| 36 | +import org.koin.core.context.startKoin |
| 37 | + |
| 38 | +@ExperimentalSerializationApi |
| 39 | +@Suppress("unused") |
| 40 | +class Handler : RequestStreamHandler, KoinComponent { |
| 41 | + companion object { |
| 42 | + private val logger = LogManager.getLogger(Handler::class.java) |
| 43 | + } |
| 44 | + |
| 45 | + init { |
| 46 | + startKoin { |
| 47 | + modules( |
| 48 | + envModule, |
| 49 | + jsonModule, |
| 50 | + urbanDictionaryModule, |
| 51 | + databaseModule, |
| 52 | + telegramModule, |
| 53 | + sfnModule, |
| 54 | + ) |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + private val json: Json by inject() |
| 59 | + private val urbanDictionaryClient: UrbanDictionaryClient by inject() |
| 60 | + private val urbanWordOfTheDayDAO: UrbanWordOfTheDayDAO by inject() |
| 61 | + private val languageRoomDAO: LanguageRoomDAO by inject() |
| 62 | + private val bot: RequestsExecutor by inject() |
| 63 | + private val unpinScheduler: UnpinScheduler by inject() |
| 64 | + |
| 65 | + override fun handleRequest(input: InputStream, output: OutputStream, context: Context) = runBlocking { |
| 66 | + val event = json.decodeFromStream<Event>(input) |
| 67 | + |
| 68 | + logger.debug("Parsed event: {}", event) |
| 69 | + |
| 70 | + val term = event.records.firstOrNull()?.ses?.mail?.subject ?: return@runBlocking |
| 71 | + val definition = urbanDictionaryClient.define(term).maxByOrNull { it.thumbsUp } ?: return@runBlocking |
| 72 | + |
| 73 | + logger.debug("Definition: {}", definition) |
| 74 | + |
| 75 | + val urbanWordOfTheDay = UrbanWordOfTheDay( |
| 76 | + date = LocalDate.now(), |
| 77 | + word = definition.word, |
| 78 | + definition = definition.definition, |
| 79 | + example = definition.example, |
| 80 | + permalink = definition.permalink, |
| 81 | + ) |
| 82 | + |
| 83 | + urbanWordOfTheDayDAO.save(urbanWordOfTheDay) |
| 84 | + languageRoomDAO.getAll().filter { it.language == Language.ENGLISH && it.urbanWordOfTheDay }.forEach { languageRoom -> |
| 85 | + val message = bot.sendMessage( |
| 86 | + chatId = languageRoom.threadId?.let { ChatIdWithThreadId(languageRoom.chatId, it) } ?: ChatId(languageRoom.chatId), |
| 87 | + text = urbanWordOfTheDay.format().also { logger.debug("Formatted message: {}", it) }, |
| 88 | + parseMode = MarkdownV2, |
| 89 | + disableWebPagePreview = true, |
| 90 | + ) |
| 91 | + |
| 92 | + bot.pinChatMessage(message, disableNotification = true) |
| 93 | + unpinScheduler.scheduleUnpin( |
| 94 | + Unpin().apply { |
| 95 | + chatId = languageRoom.chatId |
| 96 | + messageId = message.messageId |
| 97 | + ttl = (24 * 1.5 * 60 * 60).toLong() |
| 98 | + } |
| 99 | + ) |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments