Browse Source

added supported languages

Vadik Sirekanyan 5 năm trước cách đây
mục cha
commit
1dab2bb764

+ 8 - 6
src/main/kotlin/com/sirekanyan/andersrobot/AndersRobot.kt

@@ -5,6 +5,7 @@ import com.sirekanyan.andersrobot.config.Config
 import com.sirekanyan.andersrobot.config.ConfigKey.*
 import com.sirekanyan.andersrobot.extensions.*
 import com.sirekanyan.andersrobot.repository.CityRepositoryImpl
+import com.sirekanyan.andersrobot.repository.supportedLanguages
 import org.telegram.telegrambots.bots.DefaultAbsSender
 import org.telegram.telegrambots.bots.DefaultBotOptions
 import org.telegram.telegrambots.meta.api.objects.Update
@@ -25,6 +26,7 @@ class AndersRobot : DefaultAbsSender(DefaultBotOptions()), LongPollingBot {
     override fun onUpdateReceived(update: Update) {
         val message = update.message
         val chatId = message.chatId
+        val language = message.from?.languageCode?.takeIf { it in supportedLanguages }
         println("${message.from?.id} (chat $chatId) => ${message.text}")
         val isBetterAccuracy = message.chatId == 314085103L || message.chatId == 106547051L
         val accuracy = if (isBetterAccuracy) 1 else 0
@@ -33,7 +35,7 @@ class AndersRobot : DefaultAbsSender(DefaultBotOptions()), LongPollingBot {
         val delCityCommand = getDelCityCommand(message.text)
         when {
             !cityCommand.isNullOrEmpty() -> {
-                val temperature = weather.getTemperature(cityCommand)
+                val temperature = weather.getTemperature(cityCommand, language)
                 if (temperature == null) {
                     sendText(chatId, "Не знаю такого города")
                 } else {
@@ -47,12 +49,12 @@ class AndersRobot : DefaultAbsSender(DefaultBotOptions()), LongPollingBot {
                 }
             }
             !addCityCommand.isNullOrEmpty() -> {
-                val temperature = weather.getTemperature(addCityCommand)
+                val temperature = weather.getTemperature(addCityCommand, language)
                 if (temperature == null) {
                     sendText(chatId, "Не знаю такого города")
                 } else {
                     repository.putCity(chatId, addCityCommand)
-                    showWeather(chatId, accuracy)
+                    showWeather(chatId, accuracy, language)
                 }
             }
             !delCityCommand.isNullOrEmpty() -> {
@@ -66,15 +68,15 @@ class AndersRobot : DefaultAbsSender(DefaultBotOptions()), LongPollingBot {
                 sendText(chatId, "Можешь звать меня просто Андерс")
             }
             isWeatherCommand(message.text) -> {
-                showWeather(chatId, accuracy)
+                showWeather(chatId, accuracy, language)
             }
         }
     }
 
-    private fun showWeather(chatId: Long, accuracy: Int) {
+    private fun showWeather(chatId: Long, accuracy: Int, language: String?) {
         val dbCities = repository.getCities(chatId)
         val cities = if (dbCities.isEmpty()) listOf("Moscow") else dbCities
-        val temperatures = weather.getTemperatures(cities, accuracy)
+        val temperatures = weather.getTemperatures(cities, accuracy, language)
         if (temperatures.isNotEmpty()) {
             sendText(chatId, temperatures.joinToString("\n"))
         }

+ 8 - 5
src/main/kotlin/com/sirekanyan/andersrobot/api/WeatherApi.kt

@@ -17,23 +17,26 @@ class WeatherApi {
     private val apiKey = Config[WEATHER_API_KEY]
     private val comparator = compareBy<Weather> { it.sys.country.toLowerCase().replace("ru", "aa") }.thenBy { it.name }
 
-    fun getTemperature(city: String): Weather? = runBlocking {
-        getWeather(city)
+    fun getTemperature(city: String, language: String?): Weather? = runBlocking {
+        getWeather(city, language)
     }
 
-    fun getTemperatures(cities: List<String>, accuracy: Int): List<String> = runBlocking {
-        cities.map { city -> async { getWeather(city) } }
+    fun getTemperatures(cities: List<String>, accuracy: Int, language: String?): List<String> = runBlocking {
+        cities.map { city -> async { getWeather(city, language) } }
             .mapNotNull { it.await() }
             .sortedWith(comparator)
             .map { it.format(accuracy) }
     }
 
-    private suspend fun getWeather(city: String): Weather? =
+    private suspend fun getWeather(city: String, language: String?): Weather? =
         try {
             println("getting $city")
             val response: String = httpClient.get(WEATHER_URL) {
                 parameter("q", city)
                 parameter("units", "metric")
+                if (language != null) {
+                    parameter("lang", language)
+                }
                 parameter("appid", apiKey)
             }
             val json = Json { ignoreUnknownKeys = true }

+ 13 - 0
src/main/kotlin/com/sirekanyan/andersrobot/repository/Languages.kt

@@ -0,0 +1,13 @@
+package com.sirekanyan.andersrobot.repository
+
+/**
+ * Multilingual support
+ * https://openweathermap.org/current#multi
+ */
+val supportedLanguages = setOf(
+    "af", "al", "ar", "az", "bg", "ca", "cz", "da", "de", "el", "en", "eu",
+    "fa", "fi", "fr", "gl", "he", "hi", "hr", "hu", "id", "it", "ja", "kr",
+    "la", "lt", "mk", "no", "nl", "pl", "pt", "pt_br", "ro", "ru", "sv", "se",
+    "sk", "sl", "sp", "es", "sr", "th", "tr", "ua", "uk", "vi", "zh_cn",
+    "zh_tw", "zu",
+)