diff --git a/src/main/java/com/cubetiqs/util/LogUtil.java b/src/main/java/com/cubetiqs/util/LogUtil.java new file mode 100644 index 0000000..cbcf73d --- /dev/null +++ b/src/main/java/com/cubetiqs/util/LogUtil.java @@ -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); + } +} diff --git a/src/main/java/com/cubetiqs/util/TextFormat.java b/src/main/java/com/cubetiqs/util/TextFormat.java new file mode 100644 index 0000000..7d74704 --- /dev/null +++ b/src/main/java/com/cubetiqs/util/TextFormat.java @@ -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); + } +} diff --git a/src/main/kotlin/com/cubetiqs/messaging/client/sms/twlio/TwilioUtils.kt b/src/main/kotlin/com/cubetiqs/messaging/client/sms/twlio/TwilioUtils.kt index ad5431d..fe8e4bf 100644 --- a/src/main/kotlin/com/cubetiqs/messaging/client/sms/twlio/TwilioUtils.kt +++ b/src/main/kotlin/com/cubetiqs/messaging/client/sms/twlio/TwilioUtils.kt @@ -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) } } diff --git a/src/main/kotlin/com/cubetiqs/messaging/client/telegram/TelegramProvider.kt b/src/main/kotlin/com/cubetiqs/messaging/client/telegram/TelegramProvider.kt index c76e019..ce90757 100644 --- a/src/main/kotlin/com/cubetiqs/messaging/client/telegram/TelegramProvider.kt +++ b/src/main/kotlin/com/cubetiqs/messaging/client/telegram/TelegramProvider.kt @@ -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 { diff --git a/src/main/kotlin/com/cubetiqs/messaging/client/util/StringExtension.kt b/src/main/kotlin/com/cubetiqs/messaging/client/util/StringExtension.kt new file mode 100644 index 0000000..0bc7733 --- /dev/null +++ b/src/main/kotlin/com/cubetiqs/messaging/client/util/StringExtension.kt @@ -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) +} \ No newline at end of file diff --git a/src/test/java/com/cubetiqs/example/TextFormatJavaTests.java b/src/test/java/com/cubetiqs/example/TextFormatJavaTests.java new file mode 100644 index 0000000..849ea96 --- /dev/null +++ b/src/test/java/com/cubetiqs/example/TextFormatJavaTests.java @@ -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); + } +} diff --git a/src/test/kotlin/com/cubetiqs/example/TelegramExampleKotlinTests.kt b/src/test/kotlin/com/cubetiqs/example/TelegramExampleKotlinTests.kt index cf647b2..680f6d0 100644 --- a/src/test/kotlin/com/cubetiqs/example/TelegramExampleKotlinTests.kt +++ b/src/test/kotlin/com/cubetiqs/example/TelegramExampleKotlinTests.kt @@ -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() + } } \ No newline at end of file diff --git a/src/test/kotlin/com/cubetiqs/example/TextFormatKotlinTests.kt b/src/test/kotlin/com/cubetiqs/example/TextFormatKotlinTests.kt new file mode 100644 index 0000000..7e360ad --- /dev/null +++ b/src/test/kotlin/com/cubetiqs/example/TextFormatKotlinTests.kt @@ -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) + } +} \ No newline at end of file