Task: Add telegram parse mode for markdown and makrdown v2 and html not support yet and fixed models and updated functions

This commit is contained in:
Sambo Chea 2021-05-21 15:19:50 +07:00
parent 10b44e9a33
commit 28b4988e6f
10 changed files with 71 additions and 14 deletions

View File

@ -81,7 +81,8 @@ public final class TelegramExampleJavaTests {
TelegramBotUtils.sendMessage(
chatId,
text,
token
token,
null
);
}
@ -94,7 +95,8 @@ public final class TelegramExampleJavaTests {
text,
"my paper.png",
Files.readAllBytes(file.toPath()),
token
token,
null
);
}

View File

@ -33,19 +33,23 @@ object TelegramBotUtils : Loggable {
text: String,
// config prefix for custom token
token: String = "",
parseMode: TelegramParseMode? = null,
): Any? {
validateTextAndChatId(text, chatId)
val requestBody = MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("text", text)
.addFormDataPart("chat_id", chatId)
.setType(MultipartBody.FORM)
.build()
if (parseMode != null) {
requestBody.addFormDataPart("parse_mode", parseMode.mode)
}
val url = getBotUrl(TelegramConfig.SEND_MESSAGE, token)
val request = Request.Builder()
.url(url)
.post(requestBody)
.post(requestBody.build())
.build()
val result = makeRequest(request)
@ -70,6 +74,7 @@ object TelegramBotUtils : Loggable {
document: ByteArray,
// config prefix for custom token
token: String = "",
parseMode: TelegramParseMode? = null,
): 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!")
@ -86,12 +91,15 @@ object TelegramBotUtils : Loggable {
.addFormDataPart("caption", text)
.addFormDataPart("chat_id", chatId)
.setType(MultipartBody.FORM)
.build()
if (parseMode != null) {
requestBody.addFormDataPart("parse_mode", parseMode.mode)
}
val url = getBotUrl(TelegramConfig.SEND_DOCUMENT, token)
val request = Request.Builder()
.url(url)
.post(requestBody)
.post(requestBody.build())
.build()
val result = makeRequest(request)

View File

@ -8,8 +8,13 @@ package com.cubetiqs.messaging.client.telegram
*/
open class TelegramChatMessage(
private val text: String? = null,
private val parseMode: TelegramParseMode? = null,
) : TelegramMessage {
override fun getText(): String {
return text ?: ""
}
override fun getParseMode(): TelegramParseMode? {
return parseMode
}
}

View File

@ -9,8 +9,13 @@ package com.cubetiqs.messaging.client.telegram
open class TelegramDocument(
val document: TelegramFileDocument? = null,
private val text: String? = null,
private val parseMode: TelegramParseMode? = null,
) : TelegramMessage {
override fun getText(): String {
return text ?: ""
}
override fun getParseMode(): TelegramParseMode? {
return parseMode ?: TelegramParseMode.MARKDOWN
}
}

View File

@ -11,4 +11,7 @@ import java.io.Serializable
@FunctionalInterface
fun interface TelegramMessage : Serializable {
fun getText(): String
fun getParseMode(): TelegramParseMode? {
return null
}
}

View File

@ -2,5 +2,6 @@ package com.cubetiqs.messaging.client.telegram
enum class TelegramParseMode (val mode: String) {
MARKDOWN("markdown"),
HTML("html");
MARKDOWN_V2("markdownv2"),
HTML("html"); // Not support for Okhttp
}

View File

@ -24,8 +24,16 @@ class TelegramProvider : MessageProvider, Loggable {
this._chatId = chatId
}
fun setMessage(message: String) = apply {
this._message = TelegramMessage { message }
fun setMessage(message: String, parseMode: TelegramParseMode? = null) = apply {
this._message = object : TelegramMessage {
override fun getText(): String {
return message
}
override fun getParseMode(): TelegramParseMode? {
return parseMode
}
}
}
fun setMessageToSend(message: TelegramMessage?) = apply {
@ -68,12 +76,14 @@ class TelegramProvider : MessageProvider, Loggable {
text = this._message?.getText() ?: "",
filename = (_filename ?: _file?.name) ?: Date().time.toString(),
document = Files.readAllBytes(_file!!.toPath()),
parseMode = this._message!!.getParseMode(),
)
} else {
TelegramBotUtils.sendMessage(
chatId = chatId,
token = this._token,
text = this._message!!.getText(),
parseMode = this._message!!.getParseMode(),
)
}

View File

@ -11,7 +11,7 @@ object WebClientUtils : Loggable {
private fun getClient(): OkHttpClient {
if (this.webClient == null) {
this.webClient = OkHttpClient()
this.webClient = OkHttpClient.Builder().build()
}
return this.webClient!!

View File

@ -19,7 +19,8 @@ public final class TelegramExampleJavaTests {
TelegramBotUtils.sendMessage(
chatId,
text,
token
token,
null
);
}
@ -32,7 +33,8 @@ public final class TelegramExampleJavaTests {
text,
"my paper.png",
Files.readAllBytes(file.toPath()),
token
token,
null
);
}

View File

@ -2,12 +2,13 @@ package com.cubetiqs.example
import com.cubetiqs.messaging.client.telegram.TelegramBotUtils
import com.cubetiqs.messaging.client.telegram.TelegramConfig
import com.cubetiqs.messaging.client.telegram.TelegramParseMode
import com.cubetiqs.messaging.client.telegram.TelegramProvider
import org.junit.jupiter.api.Test
import java.io.File
class TelegramExampleKotlinTests {
private val token = TelegramConfig.getToken()
private val token = "1825551011:AAEJg4Y9JtmT4aiW7nz0xOxneyPdruTv8WQ" //TelegramConfig.getToken()
private val chatId = TelegramConfig.getReceiver()
@Test
@ -56,4 +57,24 @@ class TelegramExampleKotlinTests {
.sendToChatId(chatId)
.send()
}
@Test
fun sendMessageProviderWithParseModeHtml() {
val text = "<b>Hello World<b> from Provider with HTML parse mode!"
TelegramProvider.init(
token = token,
).setMessage(text, TelegramParseMode.HTML)
.sendToChatId(chatId)
.send()
}
@Test
fun sendMessageProviderWithParseModeMarkdown() {
val text = "*Hello World* from Provider with MARKDOWN parse mode!"
TelegramProvider.init(
token = token,
).setMessage(text, TelegramParseMode.MARKDOWN)
.sendToChatId(chatId)
.send()
}
}