Task: Fixed telegram and updated the telegram sender and add loggable function and fixed web request and logging
This commit is contained in:
parent
6dfb2685c8
commit
122db91ecb
@ -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<Jar> {
|
||||
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) }
|
||||
})
|
||||
}
|
@ -1,5 +1,11 @@
|
||||
package com.cubetiqs.messaging.client.provider
|
||||
|
||||
/**
|
||||
* Message Provider
|
||||
*
|
||||
* @author sombochea
|
||||
* @since 1.0
|
||||
*/
|
||||
@FunctionalInterface
|
||||
fun interface MessageProvider {
|
||||
fun send(): Any?
|
||||
|
@ -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 {
|
||||
}
|
@ -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
|
||||
}
|
||||
}
|
@ -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"
|
||||
|
||||
|
@ -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? {
|
||||
|
@ -0,0 +1,7 @@
|
||||
package com.cubetiqs.messaging.client.util
|
||||
|
||||
import org.slf4j.LoggerFactory
|
||||
|
||||
interface Loggable {
|
||||
val log get() = LoggerFactory.getLogger(this::class.java)
|
||||
}
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user