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
|
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
|
||||||
|
|
||||||
plugins {
|
plugins {
|
||||||
id("java")
|
`java-library`
|
||||||
kotlin("jvm") version "1.4.32"
|
kotlin("jvm") version "1.4.32"
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -14,6 +14,8 @@ repositories {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
|
implementation(platform("org.jetbrains.kotlin:kotlin-bom"))
|
||||||
|
|
||||||
implementation("com.squareup.okhttp3:okhttp:4.9.0")
|
implementation("com.squareup.okhttp3:okhttp:4.9.0")
|
||||||
|
|
||||||
implementation("org.jetbrains.kotlin:kotlin-reflect")
|
implementation("org.jetbrains.kotlin:kotlin-reflect")
|
||||||
@ -40,13 +42,15 @@ tasks.test {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
tasks {
|
tasks.withType<Jar> {
|
||||||
val sourcesJar by creating(Jar::class) {
|
manifest {
|
||||||
dependsOn(JavaPlugin.CLASSES_TASK_NAME)
|
attributes(mapOf("Implementation-Title" to project.name,
|
||||||
from(sourceSets["main"].allSource)
|
"Implementation-Version" to project.version))
|
||||||
}
|
}
|
||||||
|
|
||||||
artifacts {
|
from(sourceSets.main.get().output)
|
||||||
add("archives", sourcesJar)
|
|
||||||
}
|
from({
|
||||||
}
|
configurations.runtimeClasspath.get().filter { it.name.endsWith("jar") }.map { zipTree(it) }
|
||||||
|
})
|
||||||
|
}
|
@ -1,5 +1,11 @@
|
|||||||
package com.cubetiqs.messaging.client.provider
|
package com.cubetiqs.messaging.client.provider
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Message Provider
|
||||||
|
*
|
||||||
|
* @author sombochea
|
||||||
|
* @since 1.0
|
||||||
|
*/
|
||||||
@FunctionalInterface
|
@FunctionalInterface
|
||||||
fun interface MessageProvider {
|
fun interface MessageProvider {
|
||||||
fun send(): Any?
|
fun send(): Any?
|
||||||
|
@ -2,5 +2,11 @@ package com.cubetiqs.messaging.client.sms
|
|||||||
|
|
||||||
import com.cubetiqs.messaging.client.provider.MessageProvider
|
import com.cubetiqs.messaging.client.provider.MessageProvider
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sms Provider
|
||||||
|
*
|
||||||
|
* @author sombochea
|
||||||
|
* @since 1.0
|
||||||
|
*/
|
||||||
interface SmsProvider : MessageProvider {
|
interface SmsProvider : MessageProvider {
|
||||||
}
|
}
|
@ -1,12 +1,13 @@
|
|||||||
package com.cubetiqs.messaging.client.telegram
|
package com.cubetiqs.messaging.client.telegram
|
||||||
|
|
||||||
|
import com.cubetiqs.messaging.client.util.Loggable
|
||||||
import com.cubetiqs.messaging.client.webclient.WebClientUtils
|
import com.cubetiqs.messaging.client.webclient.WebClientUtils
|
||||||
import okhttp3.*
|
import okhttp3.*
|
||||||
import okhttp3.MediaType.Companion.toMediaTypeOrNull
|
import okhttp3.MediaType.Companion.toMediaTypeOrNull
|
||||||
import okhttp3.RequestBody.Companion.asRequestBody
|
import okhttp3.RequestBody.Companion.asRequestBody
|
||||||
import java.nio.file.Files
|
import java.nio.file.Files
|
||||||
|
|
||||||
object TelegramBotUtils {
|
object TelegramBotUtils : Loggable {
|
||||||
private fun getBotUrl(endpoint: String, token: String = ""): String {
|
private fun getBotUrl(endpoint: String, token: String = ""): String {
|
||||||
return "${TelegramConfig.TELEGRAM_API}/bot${token.removePrefix("bot")}/${endpoint.removePrefix("/")}"
|
return "${TelegramConfig.TELEGRAM_API}/bot${token.removePrefix("bot")}/${endpoint.removePrefix("/")}"
|
||||||
}
|
}
|
||||||
@ -14,12 +15,12 @@ object TelegramBotUtils {
|
|||||||
private fun makeRequest(
|
private fun makeRequest(
|
||||||
request: Request,
|
request: Request,
|
||||||
): Response? {
|
): Response? {
|
||||||
println(javaClass.canonicalName + " => Start send message via telegram bot...")
|
log.debug("Start send message via telegram bot...")
|
||||||
return try {
|
return try {
|
||||||
WebClientUtils.makeRequest(request)
|
WebClientUtils.makeRequest(request)
|
||||||
} catch (ex: Exception) {
|
} catch (ex: Exception) {
|
||||||
ex.printStackTrace()
|
ex.printStackTrace()
|
||||||
println("Make request error @${ex.message}")
|
log.error("Telegram make request error {}", ex.message)
|
||||||
null
|
null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -33,6 +34,8 @@ object TelegramBotUtils {
|
|||||||
// config prefix for custom token
|
// config prefix for custom token
|
||||||
token: String = "",
|
token: String = "",
|
||||||
): Any? {
|
): Any? {
|
||||||
|
validateTextAndChatId(text, chatId)
|
||||||
|
|
||||||
val requestBody = MultipartBody.Builder()
|
val requestBody = MultipartBody.Builder()
|
||||||
.addFormDataPart("text", text)
|
.addFormDataPart("text", text)
|
||||||
.addFormDataPart("chat_id", chatId)
|
.addFormDataPart("chat_id", chatId)
|
||||||
@ -46,10 +49,15 @@ object TelegramBotUtils {
|
|||||||
.build()
|
.build()
|
||||||
|
|
||||||
val result = makeRequest(request)
|
val result = makeRequest(request)
|
||||||
println(javaClass.canonicalName + " => Complete sent message to $chatId...")
|
log.debug("Telegram complete sent message to {}", chatId)
|
||||||
return result
|
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
|
@JvmStatic
|
||||||
fun sendDocument(
|
fun sendDocument(
|
||||||
// usually chat id from chat group
|
// usually chat id from chat group
|
||||||
@ -63,6 +71,9 @@ object TelegramBotUtils {
|
|||||||
// config prefix for custom token
|
// config prefix for custom token
|
||||||
token: String = "",
|
token: String = "",
|
||||||
): Any? {
|
): 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 ext = filename.split(".").lastOrNull() ?: "dat"
|
||||||
val tempFile = Files.createTempFile(filename.removeSuffix(ext), ".$ext")
|
val tempFile = Files.createTempFile(filename.removeSuffix(ext), ".$ext")
|
||||||
Files.write(tempFile, document)
|
Files.write(tempFile, document)
|
||||||
@ -84,7 +95,7 @@ object TelegramBotUtils {
|
|||||||
.build()
|
.build()
|
||||||
|
|
||||||
val result = makeRequest(request)
|
val result = makeRequest(request)
|
||||||
println(javaClass.canonicalName + " => Complete sent document to $chatId...")
|
log.debug("Telegram complete sent message to {}", chatId)
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -6,7 +6,6 @@ object TelegramConfig {
|
|||||||
const val TELEGRAM_API = "https://api.telegram.org"
|
const val TELEGRAM_API = "https://api.telegram.org"
|
||||||
const val SEND_DOCUMENT = "/sendDocument"
|
const val SEND_DOCUMENT = "/sendDocument"
|
||||||
const val SEND_MESSAGE = "/sendMessage"
|
const val SEND_MESSAGE = "/sendMessage"
|
||||||
const val POS_SYSTEM_ID = ""
|
|
||||||
private const val CUBETIQ_TELEGRAM_TOKEN = "CUBETIQ_TELEGRAM_TOKEN"
|
private const val CUBETIQ_TELEGRAM_TOKEN = "CUBETIQ_TELEGRAM_TOKEN"
|
||||||
private const val CUBETIQ_TELEGRAM_RECEIVER = "CUBETIQ_TELEGRAM_RECEIVER"
|
private const val CUBETIQ_TELEGRAM_RECEIVER = "CUBETIQ_TELEGRAM_RECEIVER"
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package com.cubetiqs.messaging.client.telegram
|
package com.cubetiqs.messaging.client.telegram
|
||||||
|
|
||||||
import com.cubetiqs.messaging.client.provider.MessageProvider
|
import com.cubetiqs.messaging.client.provider.MessageProvider
|
||||||
import org.slf4j.LoggerFactory
|
import com.cubetiqs.messaging.client.util.Loggable
|
||||||
import kotlin.IllegalArgumentException
|
import kotlin.IllegalArgumentException
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -10,8 +10,7 @@ import kotlin.IllegalArgumentException
|
|||||||
* @author sombochea
|
* @author sombochea
|
||||||
* @since 1.0
|
* @since 1.0
|
||||||
*/
|
*/
|
||||||
class TelegramProvider : MessageProvider {
|
class TelegramProvider : MessageProvider, Loggable {
|
||||||
private val log = LoggerFactory.getLogger(this::class.java)
|
|
||||||
private var _token: String = ""
|
private var _token: String = ""
|
||||||
private var _chatId: String = ""
|
private var _chatId: String = ""
|
||||||
private var _message: TelegramMessage? = null
|
private var _message: TelegramMessage? = null
|
||||||
@ -32,7 +31,7 @@ class TelegramProvider : MessageProvider {
|
|||||||
this._token = token
|
this._token = token
|
||||||
}
|
}
|
||||||
|
|
||||||
fun send(
|
private fun send(
|
||||||
chatId: String,
|
chatId: String,
|
||||||
message: TelegramMessage,
|
message: TelegramMessage,
|
||||||
): TelegramResponse? {
|
): 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
|
package com.cubetiqs.messaging.client.webclient
|
||||||
|
|
||||||
|
import com.cubetiqs.messaging.client.util.Loggable
|
||||||
import okhttp3.OkHttpClient
|
import okhttp3.OkHttpClient
|
||||||
import okhttp3.Request
|
import okhttp3.Request
|
||||||
import okhttp3.Response
|
import okhttp3.Response
|
||||||
|
|
||||||
object WebClientUtils {
|
object WebClientUtils : Loggable {
|
||||||
private var webClient: OkHttpClient? = null
|
private var webClient: OkHttpClient? = null
|
||||||
|
|
||||||
private fun getClient(): OkHttpClient {
|
private fun getClient(): OkHttpClient {
|
||||||
@ -17,6 +18,7 @@ object WebClientUtils {
|
|||||||
|
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun makeRequest(request: Request): Response {
|
fun makeRequest(request: Request): Response {
|
||||||
|
log.debug("Web is make request to: {} with method: {}", request.url, request.method)
|
||||||
val call = getClient().newCall(request)
|
val call = getClient().newCall(request)
|
||||||
var response: Response? = null
|
var response: Response? = null
|
||||||
return try {
|
return try {
|
||||||
|
Loading…
Reference in New Issue
Block a user