Task: Add Text Formatter including java and kotlin and add send file for Telegram provider and enhanced and fixed bugs

This commit is contained in:
Sambo Chea 2021-05-21 09:38:15 +07:00
parent 9617e34f73
commit 901dc00c8e
8 changed files with 200 additions and 10 deletions

View File

@ -0,0 +1,48 @@
package com.cubetiqs.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public final class LogUtil {
private static final Logger log = getLogger(LogUtil.class);
public static void info(Object ob, Object... args) {
if (ob == null) {
log.info("null");
return;
}
log.info(ob.toString(), args);
}
public static void warn(Object ob, Object... args) {
if (ob == null) {
log.warn("null");
return;
}
log.warn(ob.toString(), args);
}
public static void error(Object ob, Object... args) {
if (ob == null) {
log.error("null");
return;
}
log.error(ob.toString(), args);
}
public static void debug(Object ob, Object... args) {
if (ob == null) {
log.debug("null");
return;
}
log.debug(ob.toString(), args);
}
public static Logger getLogger(Class<?> clazz) {
return LoggerFactory.getLogger(clazz);
}
}

View File

@ -0,0 +1,40 @@
package com.cubetiqs.util;
import java.util.stream.IntStream;
public class TextFormat {
private String text;
public TextFormat() {}
public TextFormat(String text) {
this.text = text;
}
public TextFormat setText(String text) {
this.text = text;
return this;
}
public String format(Object... args) {
IntStream.range(0, args.length)
.forEach(idx -> {
String replaced = args[idx].toString();
if (replaced == null) {
replaced = "";
}
this.text = this.text.replace("{" + idx + "}", replaced);
});
return this.text;
}
public static TextFormat create() {
return new TextFormat();
}
public static TextFormat withText(String text) {
return create().setText(text);
}
}

View File

@ -25,7 +25,7 @@ object TwilioUtils : Loggable {
// reset statistic for key of sms
fun resetCounter(key: String) {
if (limiters.containsKey(key)) {
limiters[key]!!.setRelease(0)
limiters[key]!!.set(0)
}
}

View File

@ -2,6 +2,9 @@ package com.cubetiqs.messaging.client.telegram
import com.cubetiqs.messaging.client.provider.MessageProvider
import com.cubetiqs.messaging.client.util.Loggable
import java.io.File
import java.nio.file.Files
import java.util.*
import kotlin.IllegalArgumentException
/**
@ -14,6 +17,8 @@ class TelegramProvider : MessageProvider, Loggable {
private var _token: String = ""
private var _chatId: String = ""
private var _message: TelegramMessage? = null
private var _file: File? = null
private var _filename: String? = null
fun sendToChatId(chatId: String) = apply {
this._chatId = chatId
@ -31,6 +36,14 @@ class TelegramProvider : MessageProvider, Loggable {
this._token = token
}
fun setFile(file: File?) = apply {
this._file = file
}
fun setFilename(filename: String?) = apply {
this._filename = filename
}
private fun send(
chatId: String,
message: TelegramMessage,
@ -43,14 +56,26 @@ class TelegramProvider : MessageProvider, Loggable {
this._message = message
}
if (_message?.getText().isNullOrEmpty()) return null
if (_file == null) {
if (_message?.getText().isNullOrEmpty()) return null
}
return try {
val response = TelegramBotUtils.sendMessage(
chatId = chatId,
token = this._token,
text = this._message!!.getText(),
)
val response = if (_file != null) {
TelegramBotUtils.sendDocument(
chatId = chatId,
token = this._token,
text = this._message?.getText() ?: "",
filename = _filename ?: Date().time.toString(),
document = Files.readAllBytes(_file!!.toPath()),
)
} else {
TelegramBotUtils.sendMessage(
chatId = chatId,
token = this._token,
text = this._message!!.getText(),
)
}
TelegramResponse(
response = response,
@ -62,15 +87,21 @@ class TelegramProvider : MessageProvider, Loggable {
}
override fun send(): Any? {
if (this._message?.getText().isNullOrEmpty()) {
throw IllegalArgumentException("message must be non-null or non-empty!")
if (this._file == null) {
if (this._message?.getText().isNullOrEmpty()) {
throw IllegalArgumentException("message must be non-null or non-empty!")
}
} else {
if (this._message == null) {
this._message = TelegramMessage { "" }
}
}
return send(this._chatId, this._message!!)
}
override fun toString(): String {
return "TelegramProvider(_token='$_token', _chatId='$_chatId', _message=$_message)"
return "TelegramProvider(_token='$_token', _chatId='$_chatId', _message=$_message, _filename=$_filename, _file=$_file)"
}
companion object {

View File

@ -0,0 +1,12 @@
package com.cubetiqs.messaging.client.util
import com.cubetiqs.util.TextFormat
fun String?.textFormat(): TextFormat? {
this ?: return null
return TextFormat(this)
}
fun String?.textFormat(vararg args: Any?): String? {
return this.textFormat()?.format(*args)
}

View File

@ -0,0 +1,17 @@
package com.cubetiqs.example;
import com.cubetiqs.util.LogUtil;
import com.cubetiqs.util.TextFormat;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class TextFormatJavaTests {
@Test
public void textFormatter() {
String text = "Hello, I'm {0} {1}";
String result = TextFormat.withText(text).format("Sambo", "Chea");
LogUtil.info("Result => {}", result);
Assertions.assertEquals("Hello, I'm Sambo Chea", result);
}
}

View File

@ -41,4 +41,19 @@ class TelegramExampleKotlinTests {
text = text,
)
}
@Test
fun sendMessageProviderWithFile() {
val text = "Hello World from Provider with File"
val provider = TelegramProvider.init(
token
)
provider
.setFile(File("src/main/resources/cubetiq.png"))
.setFilename("cubetiq.png")
.setMessage(text)
.sendToChatId(chatId)
.send()
}
}

View File

@ -0,0 +1,27 @@
package com.cubetiqs.example
import com.cubetiqs.messaging.client.util.textFormat
import org.junit.jupiter.api.Test
import com.cubetiqs.util.TextFormat
import com.cubetiqs.util.LogUtil
import org.junit.jupiter.api.Assertions
class TextFormatKotlinTests {
@Test
fun textFormatter() {
val text = "Hello, I'm {0} {1}"
val result = TextFormat.withText(text).format("Sambo", "Chea")
LogUtil.info("Result 1 => {}", result)
Assertions.assertEquals("Hello, I'm Sambo Chea", result)
}
@Test
fun textFormatterExtension() {
val text = "Hello, I'm {0} {1}"
val result = text.textFormat("Sambo", "Chea")
LogUtil.info("Result 2 => {}", result)
Assertions.assertEquals("Hello, I'm Sambo Chea", result)
}
}