Skip to content

Commit f277789

Browse files
committed
KTB-27 implement dictionary loading from file
1 parent daf8626 commit f277789

File tree

5 files changed

+59
-19
lines changed

5 files changed

+59
-19
lines changed

src/main/kotlin/ru/ifedorov/telegrambot/console/Main.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ import ru.ifedorov.telegrambot.trainer.LearnWordsTrainer
66

77
fun main() {
88
val chatId = 0L
9-
val userName = "from console trainer"
9+
val username = "from console trainer"
1010

1111
Runtime.getRuntime().addShutdownHook(Thread {
1212
DatabaseConnection.close()
1313
})
1414

1515
val trainer = try {
16-
LearnWordsTrainer(DatabaseUserDictionary(chatId, userName))
16+
LearnWordsTrainer(DatabaseUserDictionary(chatId, username))
1717
} catch (e: Exception) {
1818
println("Невозможно подключиться к БД. $e")
1919
return

src/main/kotlin/ru/ifedorov/telegrambot/data/db/DatabaseUserDictionary.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package ru.ifedorov.telegrambot.data.db
22

33
import ru.ifedorov.telegrambot.trainer.IUserDictionary
44
import ru.ifedorov.telegrambot.trainer.model.Word
5-
import java.lang.Exception
5+
import java.io.File
66
import java.sql.Connection
77
import java.sql.ResultSet
88

@@ -27,6 +27,10 @@ class DatabaseUserDictionary(
2727
}
2828
}
2929

30+
fun updateDictionaryFromFile(file: File) {
31+
dictionaryDataSource.updateDictionary(file)
32+
}
33+
3034
private fun createTablesIfNotExists() {
3135
DatabaseConnection.connection.createStatement().use { statement ->
3236
statement.executeUpdate(
@@ -137,7 +141,6 @@ class DatabaseUserDictionary(
137141
return learnedWords
138142
}
139143

140-
141144
override fun getUnlearnedWords(): List<Word> {
142145
val userId = getUserId()
143146
val unlearnedWords = mutableListOf<Word>()

src/main/kotlin/ru/ifedorov/telegrambot/data/db/DictionaryDataSource.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ class DictionaryDataSource() {
2323
)
2424

2525
val wordsList = wordsFile.readLines()
26+
.map { it.trim() }
27+
.filter { it.isNotEmpty() }
28+
2629
if (wordsList.isEmpty()) {
2730
throw IllegalStateException("Файл словаря пустой: ${wordsFile.absolutePath}")
2831
}

src/main/kotlin/ru/ifedorov/telegrambot/telegram/Telegram.kt

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import ru.ifedorov.telegrambot.telegram.service.*
66
import ru.ifedorov.telegrambot.telegram.service.entity.GetFileResponse
77
import ru.ifedorov.telegrambot.telegram.service.entity.Update
88
import ru.ifedorov.telegrambot.trainer.LearnWordsTrainer
9+
import java.io.File
910

1011
fun main(args: Array<String>) {
1112
val botToken = args[0]
@@ -40,8 +41,9 @@ fun handleUpdate(update: Update, trainers: HashMap<Long, LearnWordsTrainer>, bot
4041
val data = update.callbackQuery?.data
4142
val document = update.message?.document
4243

44+
val dictionary = DatabaseUserDictionary(chatId, username)
4345
val trainer = trainers.getOrPut(chatId) {
44-
LearnWordsTrainer(DatabaseUserDictionary(chatId, username))
46+
LearnWordsTrainer(dictionary)
4547
}
4648

4749
if (message == COMMAND_START || data == MENU_CALLBACK) {
@@ -60,15 +62,31 @@ fun handleUpdate(update: Update, trainers: HashMap<Long, LearnWordsTrainer>, bot
6062
botService.checkNextQuestionAndSend(trainer, chatId)
6163
}
6264

63-
data?.takeIf { it.startsWith(CALLBACK_DATA_ANSWER_PREFIX) }?.let {
64-
botService.checkAnswerAndSend(trainer, chatId, it)
65+
if (data == LOAD_NEW_WORDS_CALLBACK) {
66+
botService.sendMessage(
67+
chatId,
68+
"""
69+
Для загрузки новых слов с словарь отправьте в чат бота
70+
текстовый файл формата txt, который содержит
71+
"слово|перевод", с разделителем "|" например:
72+
73+
cat|кошка
74+
dog|собака
75+
76+
Можно добавлять несколько слов, каждое с новой строки.
77+
""".trimIndent()
78+
)
6579
}
6680

6781
if (data == RESET_CLICKED) {
6882
trainer.resetProgress()
6983
botService.sendMessage(chatId, "Прогресс сброшен")
7084
}
7185

86+
data?.takeIf { it.startsWith(CALLBACK_DATA_ANSWER_PREFIX) }?.let {
87+
botService.checkAnswerAndSend(trainer, chatId, it)
88+
}
89+
7290
document?.let { document ->
7391
val fileInfoResponse: GetFileResponse? = botService.getFileInfoFromTelegram(document.fileId)
7492

@@ -77,10 +95,20 @@ fun handleUpdate(update: Update, trainers: HashMap<Long, LearnWordsTrainer>, bot
7795
val fileName = document.fileName
7896

7997
botService.saveTelegramFileLocally(filePath, fileName)
80-
botService.sendMessage(chatId, "Файл $fileName успешно загружен на сервер")
98+
99+
try {
100+
dictionary.updateDictionaryFromFile(File(fileName))
101+
botService.sendMessage(chatId, "Словарь успешно обновлен из файла $fileName")
102+
} catch (e: Exception) {
103+
botService.sendMessage(
104+
chatId,
105+
"Ошибка при обновлении словаря из файла. Проверьте формат файла и его содержание"
106+
)
107+
println("Не удалось обновить словарь из файла $fileName. Ошибка: ${e.message}")
108+
}
81109

82110
} else {
83-
botService.sendMessage(chatId, "Ошибка при загрузки файла на сервер")
111+
botService.sendMessage(chatId, "Ошибка при сохранении файла")
84112
}
85113
}
86114
}

src/main/kotlin/ru/ifedorov/telegrambot/telegram/service/TelegramBotService.kt

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
package ru.ifedorov.telegrambot.telegram.service
22

3-
import ru.ifedorov.telegrambot.telegram.service.entity.InlineKeyboard
4-
import ru.ifedorov.telegrambot.telegram.service.entity.ReplyMarkup
5-
import ru.ifedorov.telegrambot.telegram.service.entity.Response
6-
import ru.ifedorov.telegrambot.telegram.service.entity.SendMessageRequest
3+
import kotlinx.serialization.json.Json
4+
import ru.ifedorov.telegrambot.telegram.service.entity.*
75
import ru.ifedorov.telegrambot.trainer.LearnWordsTrainer
86
import ru.ifedorov.telegrambot.trainer.model.Question
9-
import kotlinx.serialization.json.Json
10-
import ru.ifedorov.telegrambot.telegram.service.entity.GetFileRequest
11-
import ru.ifedorov.telegrambot.telegram.service.entity.GetFileResponse
127
import java.io.File
138
import java.io.InputStream
149
import java.net.URI
@@ -20,6 +15,7 @@ const val TELEGRAM_BASE_URL = "https://api.telegram.org/bot"
2015
const val BOT_FILE_URL = "https://api.telegram.org/file/bot"
2116
const val LEARN_WORDS_CALLBACK = "learn_words_clicked"
2217
const val STATISTICS_CALLBACK = "statistics_clicked"
18+
const val LOAD_NEW_WORDS_CALLBACK = "load_new_words_clicked"
2319
const val MENU_CALLBACK = "menu_clicked"
2420
const val RESET_CLICKED = "reset_clicked"
2521
const val COMMAND_START = "/start"
@@ -78,7 +74,7 @@ class TelegramBotService(
7874

7975
fun saveTelegramFileLocally(filePath: String, fileName: String) {
8076
val url = "$BOT_FILE_URL$botToken/$filePath"
81-
println(url)
77+
8278
val request: HttpRequest = HttpRequest.newBuilder()
8379
.uri(URI.create(url))
8480
.build()
@@ -87,8 +83,15 @@ class TelegramBotService(
8783
val response: HttpResponse<InputStream> = client.send(request, HttpResponse.BodyHandlers.ofInputStream())
8884
println("Status code: ${response.statusCode()}")
8985

90-
val body: InputStream = response.body()
91-
body.copyTo(File(fileName).outputStream(), 16 * 1024)
86+
if (response.statusCode() != 200) {
87+
error("Ошибка HTTP ${response.statusCode()}")
88+
}
89+
90+
response.body().use { inputStream ->
91+
File(fileName).outputStream().use { outputStream ->
92+
inputStream.copyTo(outputStream, 16 * 1024)
93+
}
94+
}
9295
}
9396
.onSuccess {
9497
println("Файл $fileName успешно сохранен")
@@ -115,6 +118,9 @@ class TelegramBotService(
115118
InlineKeyboard(text = "Изучить слова", callbackData = LEARN_WORDS_CALLBACK),
116119
InlineKeyboard(text = "Статистика", callbackData = STATISTICS_CALLBACK),
117120
),
121+
listOf(
122+
InlineKeyboard(text = "Загрузка новых слов в словарь", callbackData = LOAD_NEW_WORDS_CALLBACK)
123+
),
118124
listOf(
119125
InlineKeyboard(text = "Сбросить прогресс", callbackData = RESET_CLICKED),
120126
)

0 commit comments

Comments
 (0)