2021-05-20 18:43:52 +07:00
|
|
|
package com.cubetiqs.messaging.client.telegram
|
|
|
|
|
2021-05-20 22:31:10 +07:00
|
|
|
import com.cubetiqs.messaging.client.util.Loggable
|
2021-05-20 18:43:52 +07:00
|
|
|
import com.cubetiqs.messaging.client.webclient.WebClientUtils
|
|
|
|
import okhttp3.*
|
|
|
|
import okhttp3.MediaType.Companion.toMediaTypeOrNull
|
|
|
|
import okhttp3.RequestBody.Companion.asRequestBody
|
|
|
|
import java.nio.file.Files
|
|
|
|
|
2021-05-20 22:31:10 +07:00
|
|
|
object TelegramBotUtils : Loggable {
|
2021-05-20 18:43:52 +07:00
|
|
|
private fun getBotUrl(endpoint: String, token: String = ""): String {
|
|
|
|
return "${TelegramConfig.TELEGRAM_API}/bot${token.removePrefix("bot")}/${endpoint.removePrefix("/")}"
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun makeRequest(
|
|
|
|
request: Request,
|
|
|
|
): Response? {
|
2021-05-20 23:34:45 +07:00
|
|
|
log.info("Start send message via telegram bot...")
|
2021-05-20 18:43:52 +07:00
|
|
|
return try {
|
|
|
|
WebClientUtils.makeRequest(request)
|
|
|
|
} catch (ex: Exception) {
|
|
|
|
ex.printStackTrace()
|
2021-05-20 22:31:10 +07:00
|
|
|
log.error("Telegram make request error {}", ex.message)
|
2021-05-20 23:34:45 +07:00
|
|
|
throw TelegramSendException(ex)
|
2021-05-20 18:43:52 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@JvmStatic
|
|
|
|
fun sendMessage(
|
|
|
|
// usually chat id from chat group
|
|
|
|
chatId: String,
|
|
|
|
// message to send
|
|
|
|
text: String,
|
|
|
|
// config prefix for custom token
|
|
|
|
token: String = "",
|
|
|
|
): Any? {
|
2021-05-20 22:31:10 +07:00
|
|
|
validateTextAndChatId(text, chatId)
|
|
|
|
|
2021-05-20 18:43:52 +07:00
|
|
|
val requestBody = MultipartBody.Builder()
|
|
|
|
.addFormDataPart("text", text)
|
|
|
|
.addFormDataPart("chat_id", chatId)
|
|
|
|
.setType(MultipartBody.FORM)
|
|
|
|
.build()
|
|
|
|
|
|
|
|
val url = getBotUrl(TelegramConfig.SEND_MESSAGE, token)
|
|
|
|
val request = Request.Builder()
|
|
|
|
.url(url)
|
|
|
|
.post(requestBody)
|
|
|
|
.build()
|
|
|
|
|
|
|
|
val result = makeRequest(request)
|
2021-05-20 23:34:45 +07:00
|
|
|
log.info("Telegram complete sent message to {}", chatId)
|
2021-05-20 18:43:52 +07:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2021-05-20 22:31:10 +07:00
|
|
|
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!")
|
|
|
|
}
|
|
|
|
|
2021-05-20 18:43:52 +07:00
|
|
|
@JvmStatic
|
|
|
|
fun sendDocument(
|
|
|
|
// usually chat id from chat group
|
|
|
|
chatId: String,
|
|
|
|
// message to send
|
|
|
|
text: String,
|
|
|
|
// document extension (example: my_file.pdf, my_file.docx, etc)
|
|
|
|
filename: String,
|
|
|
|
// document in byte array
|
|
|
|
document: ByteArray,
|
|
|
|
// config prefix for custom token
|
|
|
|
token: String = "",
|
|
|
|
): Any? {
|
2021-05-20 22:31:10 +07:00
|
|
|
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!")
|
|
|
|
|
2021-05-20 18:43:52 +07:00
|
|
|
val ext = filename.split(".").lastOrNull() ?: "dat"
|
|
|
|
val tempFile = Files.createTempFile(filename.removeSuffix(ext), ".$ext")
|
|
|
|
Files.write(tempFile, document)
|
|
|
|
|
|
|
|
val requestBody = MultipartBody.Builder()
|
|
|
|
.addFormDataPart(
|
|
|
|
"document", filename,
|
|
|
|
tempFile.toFile().asRequestBody("application/octet-stream".toMediaTypeOrNull())
|
|
|
|
)
|
|
|
|
.addFormDataPart("caption", text)
|
|
|
|
.addFormDataPart("chat_id", chatId)
|
|
|
|
.setType(MultipartBody.FORM)
|
|
|
|
.build()
|
|
|
|
|
|
|
|
val url = getBotUrl(TelegramConfig.SEND_DOCUMENT, token)
|
|
|
|
val request = Request.Builder()
|
|
|
|
.url(url)
|
|
|
|
.post(requestBody)
|
|
|
|
.build()
|
|
|
|
|
|
|
|
val result = makeRequest(request)
|
2021-05-20 23:34:45 +07:00
|
|
|
log.info("Telegram complete sent message to {}", chatId)
|
2021-05-20 18:43:52 +07:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
}
|