diff --git a/plugin/src/main/groovy/org/bot4j/telegram/message/HtmlBuilder.java b/plugin/src/main/groovy/org/bot4j/telegram/message/HtmlBuilder.java index bff96a6..8abe267 100644 --- a/plugin/src/main/groovy/org/bot4j/telegram/message/HtmlBuilder.java +++ b/plugin/src/main/groovy/org/bot4j/telegram/message/HtmlBuilder.java @@ -10,13 +10,15 @@ import java.util.Date; import java.util.TimeZone; -public class HtmlBuilder { +// @formatter:off +public class HtmlBuilder implements MessageBuilder { protected final StringBuilder message; public HtmlBuilder() { this.message = new StringBuilder(); } + @Override public HtmlBuilder icon(TelegramIconMode mode) { if (mode == null) { return this; @@ -25,6 +27,7 @@ public HtmlBuilder icon(TelegramIconMode mode) { return this.space(); } + @Override public HtmlBuilder icon(TelegramIconMode mode, int repeat) { if (mode == null) { return this; @@ -33,34 +36,42 @@ public HtmlBuilder icon(TelegramIconMode mode, int repeat) { return this.space(); } + @Override public HtmlBuilder timestamp(Date date, TimeZone timezone) { return this.code(Time4j.format(date, timezone)); } + @Override public HtmlBuilder timestamp(Date date, TimeZone timezone, String format) { return this.code(Time4j.format(date, timezone, format)); } + @Override public HtmlBuilder timestamp(Date date, TimezoneType timezone) { return this.code(Time4j.format(date, timezone)); } + @Override public HtmlBuilder timestamp(Date date, TimezoneType timezone, String format) { return this.code(Time4j.format(date, timezone, format)); } + @Override public HtmlBuilder timestamp(Date date, String format) { return this.code(Time4j.format(date, format)); } + @Override public HtmlBuilder timestamp(Date date) { return this.code(Time4j.format(date, TimeFormatText.BIBLIOGRAPHY_COMPLETE_EPOCH_PATTERN)); } + @Override public HtmlBuilder timestamp() { return this.timestamp(new Date()); } + @Override public HtmlBuilder vertical(String text) { message.append(String4j.repeat(Ascii.Symbol.VERTICAL_LINE, 2)) .append(text) @@ -68,6 +79,7 @@ public HtmlBuilder vertical(String text) { return this.space(); } + @Override public HtmlBuilder vertical(Object value) { if (value == null) { return this; @@ -75,6 +87,7 @@ public HtmlBuilder vertical(Object value) { return this.vertical(Class4j.isPrimitive(value.getClass()) ? value.toString() : Json4j.toJson(value)); } + @Override public HtmlBuilder bold(String text) { message.append(Ascii.Symbol.LESS_THAN_SIGN) .append(Ascii.Lowercase.LATIN_SMALL_LETTER_B) @@ -87,6 +100,7 @@ public HtmlBuilder bold(String text) { return this.space(); } + @Override public HtmlBuilder bold(Object value) { if (value == null) { return this; @@ -113,6 +127,7 @@ public HtmlBuilder strong(Object value) { return this.strong(Class4j.isPrimitive(value.getClass()) ? value.toString() : Json4j.toJson(value)); } + @Override public HtmlBuilder italic(String text) { message.append(Ascii.Symbol.LESS_THAN_SIGN) .append(Ascii.Lowercase.LATIN_SMALL_LETTER_I) @@ -125,6 +140,7 @@ public HtmlBuilder italic(String text) { return this.space(); } + @Override public HtmlBuilder italic(Object value) { if (value == null) { return this; @@ -151,6 +167,7 @@ public HtmlBuilder em(Object value) { return this.em(Class4j.isPrimitive(value.getClass()) ? value.toString() : Json4j.toJson(value)); } + @Override public HtmlBuilder underline(String text) { message.append(Ascii.Symbol.LESS_THAN_SIGN) .append(Ascii.Lowercase.LATIN_SMALL_LETTER_U) @@ -163,6 +180,7 @@ public HtmlBuilder underline(String text) { return this.space(); } + @Override public HtmlBuilder underline(Object value) { if (value == null) { return this; @@ -189,6 +207,7 @@ public HtmlBuilder ins(Object value) { return this.ins(Class4j.isPrimitive(value.getClass()) ? value.toString() : Json4j.toJson(value)); } + @Override public HtmlBuilder strikethrough(String text) { message.append(Ascii.Symbol.LESS_THAN_SIGN) .append(Ascii.Lowercase.LATIN_SMALL_LETTER_S) @@ -201,6 +220,7 @@ public HtmlBuilder strikethrough(String text) { return this.space(); } + @Override public HtmlBuilder strikethrough(Object value) { if (value == null) { return this; @@ -265,7 +285,8 @@ public HtmlBuilder spoiler(Object value) { return this.spoiler(Class4j.isPrimitive(value.getClass()) ? value.toString() : Json4j.toJson(value)); } - public HtmlBuilder inlineUrl(String text, String url) { + @Override + public HtmlBuilder link(String text, String url) { message.append(Ascii.Symbol.LESS_THAN_SIGN) .append(Ascii.Lowercase.LATIN_SMALL_LETTER_A) .append(Ascii.Punctuation.SPACE) @@ -284,6 +305,7 @@ public HtmlBuilder inlineUrl(String text, String url) { return this.space(); } + @Override public HtmlBuilder code(String text) { message.append(Ascii.Symbol.LESS_THAN_SIGN) .append("code") @@ -296,6 +318,7 @@ public HtmlBuilder code(String text) { return this.space(); } + @Override public HtmlBuilder code(Object value) { if (value == null) { return this; @@ -303,6 +326,7 @@ public HtmlBuilder code(Object value) { return this.code(Class4j.isPrimitive(value.getClass()) ? value.toString() : Json4j.toJson(value)); } + @Override public HtmlBuilder code(Path filename) { try { String data = Os4j.readFileKeepFormat(filename); @@ -312,6 +336,7 @@ public HtmlBuilder code(Path filename) { } } + @Override public HtmlBuilder preformatted(String text) { message.append(Ascii.Symbol.LESS_THAN_SIGN) .append("pre") @@ -324,6 +349,7 @@ public HtmlBuilder preformatted(String text) { return this.space(); } + @Override public HtmlBuilder preformatted(Object value) { if (value == null) { return this; @@ -331,6 +357,7 @@ public HtmlBuilder preformatted(Object value) { return this.preformatted(Class4j.isPrimitive(value.getClass()) ? value.toString() : Json4j.toJson(value)); } + @Override public HtmlBuilder preformatted(String lang, String text) { message.append(Ascii.Symbol.LESS_THAN_SIGN) .append("pre") @@ -356,6 +383,7 @@ public HtmlBuilder preformatted(String lang, String text) { return this.space(); } + @Override public HtmlBuilder preformatted(String lang, Object value) { if (value == null) { return this; @@ -363,6 +391,7 @@ public HtmlBuilder preformatted(String lang, Object value) { return this.preformatted(lang, Class4j.isPrimitive(value.getClass()) ? value.toString() : Json4j.toJson(value)); } + @Override public HtmlBuilder preformatted(String lang, Path filename) { try { String data = Os4j.readFileKeepFormat(filename); @@ -372,11 +401,13 @@ public HtmlBuilder preformatted(String lang, Path filename) { } } + @Override public HtmlBuilder text(String text) { message.append(text); return this.space(); } + @Override public HtmlBuilder text(Object value) { if (value == null) { return this; @@ -384,19 +415,23 @@ public HtmlBuilder text(Object value) { return this.text(Class4j.isPrimitive(value.getClass()) ? value.toString() : Json4j.toJson(value)); } + @Override public HtmlBuilder line() { return this.text(System.lineSeparator()); } + @Override public HtmlBuilder line(int repeat) { return this.text(String4j.repeat(System.lineSeparator(), repeat)); } + @Override public HtmlBuilder space() { message.append(Ascii.Punctuation.SPACE); return this; } + @Override public HtmlBuilder space(int repeat) { message.append(String4j.repeat(Ascii.Punctuation.SPACE, repeat)); return this; @@ -407,3 +442,4 @@ public String toString() { return this.message.toString(); } } +// @formatter:on diff --git a/plugin/src/main/groovy/org/bot4j/telegram/message/HttpMessageBuilder.java b/plugin/src/main/groovy/org/bot4j/telegram/message/HttpMessageBuilder.java new file mode 100644 index 0000000..b59c84d --- /dev/null +++ b/plugin/src/main/groovy/org/bot4j/telegram/message/HttpMessageBuilder.java @@ -0,0 +1,4 @@ +package org.bot4j.telegram.message; + +public class HttpMessageBuilder { +} diff --git a/plugin/src/main/groovy/org/bot4j/telegram/message/MarkdownBuilder.java b/plugin/src/main/groovy/org/bot4j/telegram/message/MarkdownBuilder.java index 389f998..294d0e2 100644 --- a/plugin/src/main/groovy/org/bot4j/telegram/message/MarkdownBuilder.java +++ b/plugin/src/main/groovy/org/bot4j/telegram/message/MarkdownBuilder.java @@ -10,13 +10,15 @@ import java.util.Date; import java.util.TimeZone; -public class MarkdownBuilder { +// @formatter:off +public class MarkdownBuilder implements MessageBuilder { protected final StringBuilder message; public MarkdownBuilder() { this.message = new StringBuilder(); } + @Override public MarkdownBuilder icon(TelegramIconMode mode) { if (mode == null) { return this; @@ -25,6 +27,7 @@ public MarkdownBuilder icon(TelegramIconMode mode) { return this.space(); } + @Override public MarkdownBuilder icon(TelegramIconMode mode, int repeat) { if (mode == null) { return this; @@ -33,34 +36,42 @@ public MarkdownBuilder icon(TelegramIconMode mode, int repeat) { return this.space(); } + @Override public MarkdownBuilder timestamp(Date date, TimeZone timezone) { return this.code(Time4j.format(date, timezone)); } + @Override public MarkdownBuilder timestamp(Date date, TimeZone timezone, String format) { return this.code(Time4j.format(date, timezone, format)); } + @Override public MarkdownBuilder timestamp(Date date, TimezoneType timezone) { return this.code(Time4j.format(date, timezone)); } + @Override public MarkdownBuilder timestamp(Date date, TimezoneType timezone, String format) { return this.code(Time4j.format(date, timezone, format)); } + @Override public MarkdownBuilder timestamp(Date date, String format) { return this.code(Time4j.format(date, format)); } + @Override public MarkdownBuilder timestamp(Date date) { return this.code(Time4j.format(date, TimeFormatText.BIBLIOGRAPHY_COMPLETE_EPOCH_PATTERN)); } + @Override public MarkdownBuilder timestamp() { return this.timestamp(new Date()); } + @Override public MarkdownBuilder vertical(String text) { message.append(String4j.repeat(Ascii.Symbol.VERTICAL_LINE, 2)) .append(text) @@ -68,6 +79,7 @@ public MarkdownBuilder vertical(String text) { return this.space(); } + @Override public MarkdownBuilder vertical(Object value) { if (value == null) { return this; @@ -75,6 +87,7 @@ public MarkdownBuilder vertical(Object value) { return this.vertical(Class4j.isPrimitive(value.getClass()) ? value.toString() : Json4j.toJson(value)); } + @Override public MarkdownBuilder bold(String text) { message.append(Ascii.Punctuation.ASTERISK) .append(text) @@ -82,6 +95,7 @@ public MarkdownBuilder bold(String text) { return this.space(); } + @Override public MarkdownBuilder bold(Object value) { if (value == null) { return this; @@ -89,6 +103,7 @@ public MarkdownBuilder bold(Object value) { return this.bold(Class4j.isPrimitive(value.getClass()) ? value.toString() : Json4j.toJson(value)); } + @Override public MarkdownBuilder italic(String text) { message.append(Ascii.Punctuation.LOW_LINE) .append(text) @@ -96,6 +111,7 @@ public MarkdownBuilder italic(String text) { return this.space(); } + @Override public MarkdownBuilder italic(Object value) { if (value == null) { return this; @@ -103,6 +119,7 @@ public MarkdownBuilder italic(Object value) { return this.italic(Class4j.isPrimitive(value.getClass()) ? value.toString() : Json4j.toJson(value)); } + @Override public MarkdownBuilder strikethrough(String text) { message.append(Ascii.Symbol.TILDE) .append(text) @@ -110,6 +127,7 @@ public MarkdownBuilder strikethrough(String text) { return this.space(); } + @Override public MarkdownBuilder strikethrough(Object value) { if (value == null) { return this; @@ -117,6 +135,7 @@ public MarkdownBuilder strikethrough(Object value) { return this.strikethrough(Class4j.isPrimitive(value.getClass()) ? value.toString() : Json4j.toJson(value)); } + @Override public MarkdownBuilder underline(String text) { message.append(String4j.repeat(Ascii.Punctuation.LOW_LINE, 2)) .append(text) @@ -124,6 +143,7 @@ public MarkdownBuilder underline(String text) { return this.space(); } + @Override public MarkdownBuilder underline(Object value) { if (value == null) { return this; @@ -131,6 +151,7 @@ public MarkdownBuilder underline(Object value) { return this.underline(Class4j.isPrimitive(value.getClass()) ? value.toString() : Json4j.toJson(value)); } + @Override public MarkdownBuilder code(String text) { message.append(Ascii.Symbol.GRAVE_ACCENT) .append(text) @@ -138,6 +159,7 @@ public MarkdownBuilder code(String text) { return this.space(); } + @Override public MarkdownBuilder code(Object value) { if (value == null) { return this; @@ -145,6 +167,7 @@ public MarkdownBuilder code(Object value) { return this.code(Class4j.isPrimitive(value.getClass()) ? value.toString() : Json4j.toJson(value)); } + @Override public MarkdownBuilder code(Path filename) { try { String data = Os4j.readFileKeepFormat(filename); @@ -154,6 +177,7 @@ public MarkdownBuilder code(Path filename) { } } + @Override public MarkdownBuilder preformatted(String text) { message.append(String4j.repeat(Ascii.Symbol.GRAVE_ACCENT, 3)) .append(text) @@ -161,6 +185,7 @@ public MarkdownBuilder preformatted(String text) { return this.space(); } + @Override public MarkdownBuilder preformatted(Object value) { if (value == null) { return this; @@ -168,6 +193,7 @@ public MarkdownBuilder preformatted(Object value) { return this.preformatted(Class4j.isPrimitive(value.getClass()) ? value.toString() : Json4j.toJson(value)); } + @Override public MarkdownBuilder preformatted(String lang, String text) { message.append(String4j.repeat(Ascii.Symbol.GRAVE_ACCENT, 3)) .append(lang) @@ -177,6 +203,7 @@ public MarkdownBuilder preformatted(String lang, String text) { return this.space(); } + @Override public MarkdownBuilder preformatted(String lang, Object value) { if (value == null) { return this; @@ -184,6 +211,7 @@ public MarkdownBuilder preformatted(String lang, Object value) { return this.preformatted(lang, Class4j.isPrimitive(value.getClass()) ? value.toString() : Json4j.toJson(value)); } + @Override public MarkdownBuilder preformatted(String lang, Path filename) { try { String data = Os4j.readFileKeepFormat(filename); @@ -193,6 +221,7 @@ public MarkdownBuilder preformatted(String lang, Path filename) { } } + @Override public MarkdownBuilder link(String text, String url) { message.append(Ascii.Punctuation.LEFT_SQUARE_BRACKET) .append(text) @@ -203,11 +232,13 @@ public MarkdownBuilder link(String text, String url) { return this.space(); } + @Override public MarkdownBuilder text(String text) { message.append(text); return this.space(); } + @Override public MarkdownBuilder text(Object value) { if (value == null) { return this; @@ -215,19 +246,23 @@ public MarkdownBuilder text(Object value) { return this.text(Class4j.isPrimitive(value.getClass()) ? value.toString() : Json4j.toJson(value)); } + @Override public MarkdownBuilder line() { return this.text(System.lineSeparator()); } + @Override public MarkdownBuilder line(int repeat) { return this.text(String4j.repeat(System.lineSeparator(), repeat)); } + @Override public MarkdownBuilder space() { message.append(Ascii.Punctuation.SPACE); return this; } + @Override public MarkdownBuilder space(int repeat) { message.append(String4j.repeat(Ascii.Punctuation.SPACE, repeat)); return this; @@ -238,3 +273,4 @@ public String toString() { return message.toString(); } } +// @formatter:on \ No newline at end of file diff --git a/plugin/src/main/groovy/org/bot4j/telegram/message/MessageBuilder.java b/plugin/src/main/groovy/org/bot4j/telegram/message/MessageBuilder.java new file mode 100644 index 0000000..2808964 --- /dev/null +++ b/plugin/src/main/groovy/org/bot4j/telegram/message/MessageBuilder.java @@ -0,0 +1,81 @@ +package org.bot4j.telegram.message; + +import org.bot4j.telegram.model.enums.TelegramIconMode; +import org.unify4j.model.enums.TimezoneType; + +import java.nio.file.Path; +import java.util.Date; +import java.util.TimeZone; + +public interface MessageBuilder { + + T icon(TelegramIconMode mode); + + T icon(TelegramIconMode mode, int repeat); + + T timestamp(Date date, TimeZone timezone); + + T timestamp(Date date, TimeZone timezone, String format); + + T timestamp(Date date, TimezoneType timezone); + + T timestamp(Date date, TimezoneType timezone, String format); + + T timestamp(Date date, String format); + + T timestamp(Date date); + + T timestamp(); + + T vertical(String text); + + T vertical(Object value); + + T bold(String text); + + T bold(Object value); + + T italic(String text); + + T italic(Object value); + + T strikethrough(String text); + + T strikethrough(Object value); + + T underline(String text); + + T underline(Object value); + + T code(String text); + + T code(Object value); + + T code(Path filename); + + T preformatted(String text); + + T preformatted(Object value); + + T preformatted(String lang, String text); + + T preformatted(String lang, Object value); + + T preformatted(String lang, Path filename); + + T link(String text, String url); + + T text(String text); + + T text(Object value); + + T line(); + + T line(int repeat); + + T space(); + + T space(int repeat); + + String toString(); +} diff --git a/plugin/src/main/groovy/org/bot4j/telegram/message/MessageFactory.java b/plugin/src/main/groovy/org/bot4j/telegram/message/MessageFactory.java index 0e4988d..e64985a 100644 --- a/plugin/src/main/groovy/org/bot4j/telegram/message/MessageFactory.java +++ b/plugin/src/main/groovy/org/bot4j/telegram/message/MessageFactory.java @@ -2,11 +2,21 @@ public abstract class MessageFactory { - public static MarkdownBuilder createMarkdownBuilder() { + /** + * Create a MarkdownBuilder instance. + * + * @return MarkdownBuilder instance + */ + public static MarkdownBuilder markdown() { return new MarkdownBuilder(); } - public static HtmlBuilder createHtmlBuilder() { + /** + * Create a HtmlBuilder instance. + * + * @return HtmlBuilder instance + */ + public static HtmlBuilder html() { return new HtmlBuilder(); } } diff --git a/plugin/src/main/groovy/org/bot4j/telegram/model/rd/AbstractTelegramClass.java b/plugin/src/main/groovy/org/bot4j/telegram/model/rd/AbstractTelegramClass.java index 96de884..5bea92f 100644 --- a/plugin/src/main/groovy/org/bot4j/telegram/model/rd/AbstractTelegramClass.java +++ b/plugin/src/main/groovy/org/bot4j/telegram/model/rd/AbstractTelegramClass.java @@ -2,6 +2,7 @@ import org.bot4j.telegram.message.HtmlBuilder; import org.bot4j.telegram.message.MarkdownBuilder; +import org.bot4j.telegram.message.MessageBuilder; import org.bot4j.telegram.model.builder.TelegramConnectionBuilder; import org.bot4j.telegram.model.builder.TelegramOptionBuilder; import org.bot4j.telegram.model.enums.TelegramTextMode; @@ -100,6 +101,14 @@ public T text(MarkdownBuilder builder) { } public T text(HtmlBuilder builder) { + if (builder == null) { + return this.self(); + } + this.parseMode = TelegramTextMode.HTML.name().toLowerCase(); + return this.text(builder.toString()); + } + + public T text(MessageBuilder builder) { if (builder == null) { return this.self(); } diff --git a/plugin/src/main/resources/sample.txt b/plugin/src/main/resources/sample.txt new file mode 100644 index 0000000..80e6792 --- /dev/null +++ b/plugin/src/main/resources/sample.txt @@ -0,0 +1 @@ +@Author: pnguyen215 \ No newline at end of file diff --git a/plugin/src/test/groovy/org/bot4j/HtmlBuilderTest.java b/plugin/src/test/groovy/org/bot4j/HtmlBuilderTest.java index 55ab2a9..b75374c 100644 --- a/plugin/src/test/groovy/org/bot4j/HtmlBuilderTest.java +++ b/plugin/src/test/groovy/org/bot4j/HtmlBuilderTest.java @@ -51,7 +51,7 @@ public void testVertical() { @Test public void testInlineUrl() { - HtmlBuilder builder = new HtmlBuilder().inlineUrl("OpenAI", "https://www.openai.com"); + HtmlBuilder builder = new HtmlBuilder().link("OpenAI", "https://www.openai.com"); assertEquals("OpenAI ", builder.toString()); } diff --git a/plugin/src/test/groovy/org/bot4j/Telegram4jTest.java b/plugin/src/test/groovy/org/bot4j/Telegram4jTest.java index 6e649c6..23a101f 100644 --- a/plugin/src/test/groovy/org/bot4j/Telegram4jTest.java +++ b/plugin/src/test/groovy/org/bot4j/Telegram4jTest.java @@ -2,6 +2,7 @@ import org.bot4j.telegram.common.Telegram4j; import org.bot4j.telegram.message.MarkdownBuilder; +import org.bot4j.telegram.message.MessageFactory; import org.junit.Before; import org.junit.Test; import org.unify4j.model.response.WrapResponse; @@ -16,7 +17,7 @@ public class Telegram4jTest { @Before public void setUp() { - MarkdownBuilder builder = new MarkdownBuilder() + MarkdownBuilder builder = MessageFactory.markdown() .text("Hello, World!"); telegram4j = new Telegram4j.Builder().botToken("6806983892:AAGcPZiuNktLFnyVWrRyOyYssECcVmNJSRo").chatId(-1002042977093L).text(builder).markdownSettings().build(); }