From 122db91ecb793a72fe7897c7c8a5de29b5fb2945 Mon Sep 17 00:00:00 2001 From: Sambo Chea Date: Thu, 20 May 2021 22:31:10 +0700 Subject: [PATCH] Task: Fixed telegram and updated the telegram sender and add loggable function and fixed web request and logging --- build.gradle.kts | 22 +++++++++++-------- .../client/provider/MessageProvider.kt | 6 +++++ .../messaging/client/sms/SmsProvider.kt | 6 +++++ .../client/telegram/TelegramBotUtils.kt | 21 +++++++++++++----- .../client/telegram/TelegramConfig.kt | 1 - .../client/telegram/TelegramProvider.kt | 7 +++--- .../messaging/client/util/Loggable.kt | 7 ++++++ .../client/webclient/WebClientUtils.kt | 4 +++- 8 files changed, 54 insertions(+), 20 deletions(-) create mode 100644 src/main/kotlin/com/cubetiqs/messaging/client/util/Loggable.kt diff --git a/build.gradle.kts b/build.gradle.kts index 3021a0b..ef1d1b0 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,7 +1,7 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile plugins { - id("java") + `java-library` kotlin("jvm") version "1.4.32" } @@ -14,6 +14,8 @@ repositories { } dependencies { + implementation(platform("org.jetbrains.kotlin:kotlin-bom")) + implementation("com.squareup.okhttp3:okhttp:4.9.0") implementation("org.jetbrains.kotlin:kotlin-reflect") @@ -40,13 +42,15 @@ tasks.test { } } -tasks { - val sourcesJar by creating(Jar::class) { - dependsOn(JavaPlugin.CLASSES_TASK_NAME) - from(sourceSets["main"].allSource) +tasks.withType { + manifest { + attributes(mapOf("Implementation-Title" to project.name, + "Implementation-Version" to project.version)) } - artifacts { - add("archives", sourcesJar) - } -} + from(sourceSets.main.get().output) + + from({ + configurations.runtimeClasspath.get().filter { it.name.endsWith("jar") }.map { zipTree(it) } + }) +} \ No newline at end of file diff --git a/src/main/kotlin/com/cubetiqs/messaging/client/provider/MessageProvider.kt b/src/main/kotlin/com/cubetiqs/messaging/client/provider/MessageProvider.kt index c437c2c..8c222f9 100644 --- a/src/main/kotlin/com/cubetiqs/messaging/client/provider/MessageProvider.kt +++ b/src/main/kotlin/com/cubetiqs/messaging/client/provider/MessageProvider.kt @@ -1,5 +1,11 @@ package com.cubetiqs.messaging.client.provider +/** + * Message Provider + * + * @author sombochea + * @since 1.0 + */ @FunctionalInterface fun interface MessageProvider { fun send(): Any? diff --git a/src/main/kotlin/com/cubetiqs/messaging/client/sms/SmsProvider.kt b/src/main/kotlin/com/cubetiqs/messaging/client/sms/SmsProvider.kt index 9419fd5..03df496 100644 --- a/src/main/kotlin/com/cubetiqs/messaging/client/sms/SmsProvider.kt +++ b/src/main/kotlin/com/cubetiqs/messaging/client/sms/SmsProvider.kt @@ -2,5 +2,11 @@ package com.cubetiqs.messaging.client.sms import com.cubetiqs.messaging.client.provider.MessageProvider +/** + * Sms Provider + * + * @author sombochea + * @since 1.0 + */ interface SmsProvider : MessageProvider { } \ No newline at end of file diff --git a/src/main/kotlin/com/cubetiqs/messaging/client/telegram/TelegramBotUtils.kt b/src/main/kotlin/com/cubetiqs/messaging/client/telegram/TelegramBotUtils.kt index cb0afe1..2bdae0b 100644 --- a/src/main/kotlin/com/cubetiqs/messaging/client/telegram/TelegramBotUtils.kt +++ b/src/main/kotlin/com/cubetiqs/messaging/client/telegram/TelegramBotUtils.kt @@ -1,12 +1,13 @@ package com.cubetiqs.messaging.client.telegram +import com.cubetiqs.messaging.client.util.Loggable import com.cubetiqs.messaging.client.webclient.WebClientUtils import okhttp3.* import okhttp3.MediaType.Companion.toMediaTypeOrNull import okhttp3.RequestBody.Companion.asRequestBody import java.nio.file.Files -object TelegramBotUtils { +object TelegramBotUtils : Loggable { private fun getBotUrl(endpoint: String, token: String = ""): String { return "${TelegramConfig.TELEGRAM_API}/bot${token.removePrefix("bot")}/${endpoint.removePrefix("/")}" } @@ -14,12 +15,12 @@ object TelegramBotUtils { private fun makeRequest( request: Request, ): Response? { - println(javaClass.canonicalName + " => Start send message via telegram bot...") + log.debug("Start send message via telegram bot...") return try { WebClientUtils.makeRequest(request) } catch (ex: Exception) { ex.printStackTrace() - println("Make request error @${ex.message}") + log.error("Telegram make request error {}", ex.message) null } } @@ -33,6 +34,8 @@ object TelegramBotUtils { // config prefix for custom token token: String = "", ): Any? { + validateTextAndChatId(text, chatId) + val requestBody = MultipartBody.Builder() .addFormDataPart("text", text) .addFormDataPart("chat_id", chatId) @@ -46,10 +49,15 @@ object TelegramBotUtils { .build() val result = makeRequest(request) - println(javaClass.canonicalName + " => Complete sent message to $chatId...") + log.debug("Telegram complete sent message to {}", chatId) return result } + private fun validateTextAndChatId(text: String, chatId: String) { + if (text.isEmpty() || text.isBlank()) throw IllegalArgumentException("Message is required to send to receiver!") + if (chatId.isBlank() || chatId.isEmpty()) throw IllegalArgumentException("Chat ID is required to receive the message!") + } + @JvmStatic fun sendDocument( // usually chat id from chat group @@ -63,6 +71,9 @@ object TelegramBotUtils { // config prefix for custom token token: String = "", ): Any? { + if (document.isEmpty()) throw IllegalArgumentException("Document is required to attach in message!") + if (chatId.isBlank() || chatId.isEmpty()) throw IllegalArgumentException("Chat ID is required to receive the message!") + val ext = filename.split(".").lastOrNull() ?: "dat" val tempFile = Files.createTempFile(filename.removeSuffix(ext), ".$ext") Files.write(tempFile, document) @@ -84,7 +95,7 @@ object TelegramBotUtils { .build() val result = makeRequest(request) - println(javaClass.canonicalName + " => Complete sent document to $chatId...") + log.debug("Telegram complete sent message to {}", chatId) return result } } \ No newline at end of file diff --git a/src/main/kotlin/com/cubetiqs/messaging/client/telegram/TelegramConfig.kt b/src/main/kotlin/com/cubetiqs/messaging/client/telegram/TelegramConfig.kt index 49ba010..d41c035 100644 --- a/src/main/kotlin/com/cubetiqs/messaging/client/telegram/TelegramConfig.kt +++ b/src/main/kotlin/com/cubetiqs/messaging/client/telegram/TelegramConfig.kt @@ -6,7 +6,6 @@ object TelegramConfig { const val TELEGRAM_API = "https://api.telegram.org" const val SEND_DOCUMENT = "/sendDocument" const val SEND_MESSAGE = "/sendMessage" - const val POS_SYSTEM_ID = "" private const val CUBETIQ_TELEGRAM_TOKEN = "CUBETIQ_TELEGRAM_TOKEN" private const val CUBETIQ_TELEGRAM_RECEIVER = "CUBETIQ_TELEGRAM_RECEIVER" diff --git a/src/main/kotlin/com/cubetiqs/messaging/client/telegram/TelegramProvider.kt b/src/main/kotlin/com/cubetiqs/messaging/client/telegram/TelegramProvider.kt index 8dd62a1..c76e019 100644 --- a/src/main/kotlin/com/cubetiqs/messaging/client/telegram/TelegramProvider.kt +++ b/src/main/kotlin/com/cubetiqs/messaging/client/telegram/TelegramProvider.kt @@ -1,7 +1,7 @@ package com.cubetiqs.messaging.client.telegram import com.cubetiqs.messaging.client.provider.MessageProvider -import org.slf4j.LoggerFactory +import com.cubetiqs.messaging.client.util.Loggable import kotlin.IllegalArgumentException /** @@ -10,8 +10,7 @@ import kotlin.IllegalArgumentException * @author sombochea * @since 1.0 */ -class TelegramProvider : MessageProvider { - private val log = LoggerFactory.getLogger(this::class.java) +class TelegramProvider : MessageProvider, Loggable { private var _token: String = "" private var _chatId: String = "" private var _message: TelegramMessage? = null @@ -32,7 +31,7 @@ class TelegramProvider : MessageProvider { this._token = token } - fun send( + private fun send( chatId: String, message: TelegramMessage, ): TelegramResponse? { diff --git a/src/main/kotlin/com/cubetiqs/messaging/client/util/Loggable.kt b/src/main/kotlin/com/cubetiqs/messaging/client/util/Loggable.kt new file mode 100644 index 0000000..7a0798e --- /dev/null +++ b/src/main/kotlin/com/cubetiqs/messaging/client/util/Loggable.kt @@ -0,0 +1,7 @@ +package com.cubetiqs.messaging.client.util + +import org.slf4j.LoggerFactory + +interface Loggable { + val log get() = LoggerFactory.getLogger(this::class.java) +} \ No newline at end of file diff --git a/src/main/kotlin/com/cubetiqs/messaging/client/webclient/WebClientUtils.kt b/src/main/kotlin/com/cubetiqs/messaging/client/webclient/WebClientUtils.kt index 4020d57..f6d80aa 100644 --- a/src/main/kotlin/com/cubetiqs/messaging/client/webclient/WebClientUtils.kt +++ b/src/main/kotlin/com/cubetiqs/messaging/client/webclient/WebClientUtils.kt @@ -1,10 +1,11 @@ package com.cubetiqs.messaging.client.webclient +import com.cubetiqs.messaging.client.util.Loggable import okhttp3.OkHttpClient import okhttp3.Request import okhttp3.Response -object WebClientUtils { +object WebClientUtils : Loggable { private var webClient: OkHttpClient? = null private fun getClient(): OkHttpClient { @@ -17,6 +18,7 @@ object WebClientUtils { @JvmStatic fun makeRequest(request: Request): Response { + log.debug("Web is make request to: {} with method: {}", request.url, request.method) val call = getClient().newCall(request) var response: Response? = null return try {