Jelajahi Sumber

Replaced telegrambots library

Vadik Sirekanyan 1 tahun lalu
induk
melakukan
e81328b748

+ 9 - 4
build.gradle.kts

@@ -13,10 +13,15 @@ version = "1.0"
 
 repositories {
     mavenCentral()
+    mavenLocal {
+        content {
+            includeModule("org.sirekanyan", "telegram-bots")
+        }
+    }
 }
 
 dependencies {
-    implementation("org.telegram:telegrambots:6.9.7.1")
+    implementation("org.sirekanyan:telegram-bots:6.9.7.1")
     implementation("io.ktor:ktor-client-cio:3.0.2")
     implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.7.3")
     implementation("org.jetbrains.exposed:exposed-jdbc:0.57.0")
@@ -47,12 +52,12 @@ distributions {
 kotlin {
     jvmToolchain(17)
     compilerOptions {
-        jvmTarget = JvmTarget.JVM_1_8
+        jvmTarget = JvmTarget.JVM_11
         allWarningsAsErrors = true
     }
 }
 
 java {
-    sourceCompatibility = JavaVersion.VERSION_1_8
-    targetCompatibility = JavaVersion.VERSION_1_8
+    sourceCompatibility = JavaVersion.VERSION_11
+    targetCompatibility = JavaVersion.VERSION_11
 }

+ 0 - 1
src/main/kotlin/com/sirekanyan/andersrobot/AndersRobot.kt

@@ -1,7 +1,6 @@
 package com.sirekanyan.andersrobot
 
 import com.sirekanyan.andersrobot.command.*
-import com.sirekanyan.andersrobot.config.Config
 import com.sirekanyan.andersrobot.config.ConfigKey.*
 import com.sirekanyan.andersrobot.extensions.logError
 import com.sirekanyan.andersrobot.extensions.logInfo

+ 16 - 15
src/main/kotlin/com/sirekanyan/andersrobot/Controller.kt

@@ -5,14 +5,15 @@ import com.sirekanyan.andersrobot.api.Weather
 import com.sirekanyan.andersrobot.api.WeatherApi
 import com.sirekanyan.andersrobot.command.Command
 import com.sirekanyan.andersrobot.extensions.logError
-import com.sirekanyan.andersrobot.extensions.sendPhoto
 import com.sirekanyan.andersrobot.extensions.sendSticker
-import com.sirekanyan.andersrobot.extensions.sendText
 import com.sirekanyan.andersrobot.image.generateImage
 import com.sirekanyan.andersrobot.image.plotForecast
 import com.sirekanyan.andersrobot.repository.CityRepository
 import com.sirekanyan.andersrobot.repository.supportedLanguages
 import org.jetbrains.letsPlot.export.ggsave
+import org.sirekanyan.telegrambots.extensions.sendMessage
+import org.sirekanyan.telegrambots.extensions.sendPhoto
+import org.telegram.telegrambots.meta.api.objects.InputFile
 import org.telegram.telegrambots.meta.api.objects.Location
 import org.telegram.telegrambots.meta.api.objects.Update
 import org.telegram.telegrambots.meta.bots.AbsSender
@@ -37,7 +38,7 @@ class Controller(
     fun onLocationCommand(location: Location) {
         val weather = api.getWeather(location, language)
         if (weather == null) {
-            sender.sendText(chatId, "Не знаю такого места")
+            sender.sendMessage(chatId, "Не знаю такого места")
         } else {
             showWeather(weather)
         }
@@ -46,7 +47,7 @@ class Controller(
     fun onCityCommand(city: String) {
         val weather = api.getWeather(city, language)
         if (weather == null) {
-            sender.sendText(chatId, "Не знаю такого города")
+            sender.sendMessage(chatId, "Не знаю такого города")
         } else {
             showWeather(weather)
         }
@@ -55,7 +56,7 @@ class Controller(
     fun onAddCity(city: String) {
         val weather = api.getWeather(city, language)
         if (weather == null) {
-            sender.sendText(chatId, "Не знаю такого города")
+            sender.sendMessage(chatId, "Не знаю такого города")
         } else {
             repository.putCity(chatId, weather.id)
             showWeathers()
@@ -65,16 +66,16 @@ class Controller(
     fun onDeleteCity(city: String) {
         val temperature = api.getWeather(city, language)
         when {
-            temperature == null -> sender.sendText(chatId, "Не знаю такого города")
-            repository.deleteCity(chatId, temperature.id) -> sender.sendText(chatId, "Удалено")
-            else -> sender.sendText(chatId, "Нет такого города")
+            temperature == null -> sender.sendMessage(chatId, "Не знаю такого города")
+            repository.deleteCity(chatId, temperature.id) -> sender.sendMessage(chatId, "Удалено")
+            else -> sender.sendMessage(chatId, "Нет такого города")
         }
     }
 
     fun onForecastCommand(city: String) {
         val forecast = api.getForecast(city, language)
         if (forecast == null) {
-            sender.sendText(chatId, "Не знаю такого города")
+            sender.sendMessage(chatId, "Не знаю такого города")
         } else {
             showForecast(forecast)
         }
@@ -82,7 +83,7 @@ class Controller(
 
     @Suppress("UNUSED_PARAMETER")
     fun onCelsiusCommand(command: Command) {
-        sender.sendText(chatId, "Можешь звать меня просто Андерс")
+        sender.sendMessage(chatId, "Можешь звать меня просто Андерс")
     }
 
     @Suppress("UNUSED_PARAMETER")
@@ -92,14 +93,14 @@ class Controller(
 
     fun onCityMissing(command: Command) {
         delayedCommands[chatId] = command
-        sender.sendText(chatId, "Какой город?")
+        sender.sendMessage(chatId, "Какой город?")
     }
 
     private fun showWeather(weather: Weather) {
         weather.findStickerFile()?.let { icon ->
             sender.sendSticker(chatId, icon)
         }
-        sender.sendText(chatId, "${weather.name} ${weather.temperature}")
+        sender.sendMessage(chatId, "${weather.name} ${weather.temperature}")
     }
 
     private fun showWeathers() {
@@ -114,17 +115,17 @@ class Controller(
         try {
             val file = File("weather-$chatId.png")
             ImageIO.write(generateImage(weathers), "png", file)
-            sender.sendPhoto(chatId, file)
+            sender.sendPhoto(chatId, InputFile(file))
         } catch (exception: Exception) {
             sender.logError("Cannot send image to $chatId", exception)
-            sender.sendText(chatId, weathers.joinToString("\n") { it.format() })
+            sender.sendMessage(chatId, weathers.joinToString("\n") { it.format() })
         }
     }
 
     private fun showForecast(forecast: Forecast) {
         val city = forecast.city.name
         ggsave(plotForecast(forecast, locale), "$city.png")
-        sender.sendPhoto(chatId, File("lets-plot-images/$city.png"))
+        sender.sendPhoto(chatId, InputFile(File("lets-plot-images/$city.png")))
     }
 
 }

+ 0 - 1
src/main/kotlin/com/sirekanyan/andersrobot/ControllerFactory.kt

@@ -1,7 +1,6 @@
 package com.sirekanyan.andersrobot
 
 import com.sirekanyan.andersrobot.api.WeatherApi
-import com.sirekanyan.andersrobot.config.Config
 import com.sirekanyan.andersrobot.config.ConfigKey.DB_URL
 import com.sirekanyan.andersrobot.repository.CityRepositoryImpl
 import org.telegram.telegrambots.meta.api.objects.Update

+ 4 - 0
src/main/kotlin/com/sirekanyan/andersrobot/Main.kt

@@ -2,9 +2,13 @@
 
 package com.sirekanyan.andersrobot
 
+import com.sirekanyan.andersrobot.config.ConfigKey
+import org.sirekanyan.telegrambots.BotConfig
 import org.telegram.telegrambots.meta.TelegramBotsApi
 import org.telegram.telegrambots.updatesreceivers.DefaultBotSession
 
+val Config = BotConfig(ConfigKey.entries)
+
 fun main() {
     TelegramBotsApi(DefaultBotSession::class.java).registerBot(AndersRobot())
 }

+ 1 - 2
src/main/kotlin/com/sirekanyan/andersrobot/api/WeatherApi.kt

@@ -1,13 +1,12 @@
 package com.sirekanyan.andersrobot.api
 
-import com.sirekanyan.andersrobot.config.Config
+import com.sirekanyan.andersrobot.Config
 import com.sirekanyan.andersrobot.config.ConfigKey.WEATHER_API_KEY
 import io.ktor.client.*
 import io.ktor.client.request.*
 import io.ktor.client.statement.*
 import kotlinx.coroutines.async
 import kotlinx.coroutines.runBlocking
-import kotlinx.serialization.decodeFromString
 import kotlinx.serialization.json.Json
 import org.telegram.telegrambots.meta.api.objects.Location
 

+ 0 - 42
src/main/kotlin/com/sirekanyan/andersrobot/config/Config.kt

@@ -1,42 +0,0 @@
-package com.sirekanyan.andersrobot.config
-
-import java.io.File
-import java.util.*
-
-private val IS_DEBUG = System.getProperty("debug") != null
-private val CONFIG_FILE = if (IS_DEBUG) "bot.properties.debug" else "bot.properties"
-
-object Config {
-
-    private val properties = initProperties(initFile())
-
-    private fun initFile(): File {
-        val file = File(CONFIG_FILE)
-        if (!file.exists()) {
-            file.bufferedWriter().use { writer ->
-                ConfigKey.values().forEach { key ->
-                    writer.appendLine("${key.name}=")
-                }
-            }
-        }
-        return file
-    }
-
-    private fun initProperties(file: File): Properties {
-        val properties = Properties()
-        file.inputStream().use { configFile ->
-            properties.load(configFile)
-        }
-        val missingProperties = ConfigKey.values().filter { key ->
-            properties.getProperty(key.toString()).isNullOrBlank()
-        }
-        if (missingProperties.any()) {
-            throw IllegalArgumentException("Missing $missingProperties in $CONFIG_FILE file")
-        }
-        return properties
-    }
-
-    operator fun get(key: ConfigKey): String =
-        properties.getProperty(key.toString())
-
-}

+ 3 - 1
src/main/kotlin/com/sirekanyan/andersrobot/config/ConfigKey.kt

@@ -1,6 +1,8 @@
 package com.sirekanyan.andersrobot.config
 
-enum class ConfigKey {
+import org.sirekanyan.telegrambots.BotConfigKey
+
+enum class ConfigKey : BotConfigKey {
     BOT_USERNAME,
     BOT_TOKEN,
     ADMIN_ID,

+ 0 - 9
src/main/kotlin/com/sirekanyan/andersrobot/extensions/AbsSender.kt

@@ -1,22 +1,13 @@
 package com.sirekanyan.andersrobot.extensions
 
-import org.telegram.telegrambots.meta.api.methods.send.SendMessage
-import org.telegram.telegrambots.meta.api.methods.send.SendPhoto
 import org.telegram.telegrambots.meta.api.methods.send.SendSticker
 import org.telegram.telegrambots.meta.api.objects.InputFile
-import org.telegram.telegrambots.meta.api.objects.Message
 import org.telegram.telegrambots.meta.bots.AbsSender
 import java.io.File
 import java.util.concurrent.ConcurrentHashMap
 
 private val cachedFileIds: MutableMap<File, String> = ConcurrentHashMap()
 
-fun AbsSender.sendText(chatId: Long, text: String): Message =
-    execute(SendMessage(chatId.toString(), text))
-
-fun AbsSender.sendPhoto(chatId: Long, file: File): Message =
-    execute(SendPhoto(chatId.toString(), InputFile(file)))
-
 fun AbsSender.sendSticker(chatId: Long, file: File) {
     fun send(f: InputFile) = execute(SendSticker(chatId.toString(), f))
     val cachedFileId = cachedFileIds[file]