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

View File

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

View File

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

View File

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

View File

@ -11,4 +11,7 @@ import java.io.Serializable
@FunctionalInterface @FunctionalInterface
fun interface TelegramMessage : Serializable { fun interface TelegramMessage : Serializable {
fun getText(): String 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) { enum class TelegramParseMode (val mode: String) {
MARKDOWN("markdown"), 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 this._chatId = chatId
} }
fun setMessage(message: String) = apply { fun setMessage(message: String, parseMode: TelegramParseMode? = null) = apply {
this._message = TelegramMessage { message } this._message = object : TelegramMessage {
override fun getText(): String {
return message
}
override fun getParseMode(): TelegramParseMode? {
return parseMode
}
}
} }
fun setMessageToSend(message: TelegramMessage?) = apply { fun setMessageToSend(message: TelegramMessage?) = apply {
@ -68,12 +76,14 @@ class TelegramProvider : MessageProvider, Loggable {
text = this._message?.getText() ?: "", text = this._message?.getText() ?: "",
filename = (_filename ?: _file?.name) ?: Date().time.toString(), filename = (_filename ?: _file?.name) ?: Date().time.toString(),
document = Files.readAllBytes(_file!!.toPath()), document = Files.readAllBytes(_file!!.toPath()),
parseMode = this._message!!.getParseMode(),
) )
} else { } else {
TelegramBotUtils.sendMessage( TelegramBotUtils.sendMessage(
chatId = chatId, chatId = chatId,
token = this._token, token = this._token,
text = this._message!!.getText(), text = this._message!!.getText(),
parseMode = this._message!!.getParseMode(),
) )
} }

View File

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

View File

@ -19,7 +19,8 @@ public final class TelegramExampleJavaTests {
TelegramBotUtils.sendMessage( TelegramBotUtils.sendMessage(
chatId, chatId,
text, text,
token token,
null
); );
} }
@ -32,7 +33,8 @@ public final class TelegramExampleJavaTests {
text, text,
"my paper.png", "my paper.png",
Files.readAllBytes(file.toPath()), 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.TelegramBotUtils
import com.cubetiqs.messaging.client.telegram.TelegramConfig import com.cubetiqs.messaging.client.telegram.TelegramConfig
import com.cubetiqs.messaging.client.telegram.TelegramParseMode
import com.cubetiqs.messaging.client.telegram.TelegramProvider import com.cubetiqs.messaging.client.telegram.TelegramProvider
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import java.io.File import java.io.File
class TelegramExampleKotlinTests { class TelegramExampleKotlinTests {
private val token = TelegramConfig.getToken() private val token = "1825551011:AAEJg4Y9JtmT4aiW7nz0xOxneyPdruTv8WQ" //TelegramConfig.getToken()
private val chatId = TelegramConfig.getReceiver() private val chatId = TelegramConfig.getReceiver()
@Test @Test
@ -56,4 +57,24 @@ class TelegramExampleKotlinTests {
.sendToChatId(chatId) .sendToChatId(chatId)
.send() .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()
}
} }