Skip to content

Commit

Permalink
✨ feat: add Markdown and HTML builder #5
Browse files Browse the repository at this point in the history
  • Loading branch information
pnguyen215 committed Jul 6, 2024
1 parent f188079 commit d9bd0dc
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 9 deletions.
Binary file modified libs/unify4j-v1.0.0.jar
Binary file not shown.
56 changes: 53 additions & 3 deletions plugin/src/main/groovy/org/bot4j/telegram/message/HtmlBuilder.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package org.bot4j.telegram.message;

import org.unify4j.common.Class4j;
import org.unify4j.common.Json4j;
import org.unify4j.common.String4j;
import org.unify4j.common.*;
import org.unify4j.model.c.Ascii;
import org.unify4j.model.enums.TimezoneType;
import org.unify4j.text.TimeFormatText;

import java.nio.file.Path;
import java.util.Date;
import java.util.TimeZone;

public class HtmlBuilder {
protected final StringBuilder message;
Expand All @@ -12,6 +16,34 @@ public HtmlBuilder() {
this.message = new StringBuilder();
}

public HtmlBuilder timestamp(Date date, TimeZone timezone) {
return this.code(Time4j.format(date, timezone));
}

public HtmlBuilder timestamp(Date date, TimeZone timezone, String format) {
return this.code(Time4j.format(date, timezone, format));
}

public HtmlBuilder timestamp(Date date, TimezoneType timezone) {
return this.code(Time4j.format(date, timezone));
}

public HtmlBuilder timestamp(Date date, TimezoneType timezone, String format) {
return this.code(Time4j.format(date, timezone, format));
}

public HtmlBuilder timestamp(Date date, String format) {
return this.code(Time4j.format(date, format));
}

public HtmlBuilder timestamp(Date date) {
return this.code(Time4j.format(date, TimeFormatText.BIBLIOGRAPHY_COMPLETE_EPOCH_PATTERN));
}

public HtmlBuilder timestamp() {
return this.timestamp(new Date());
}

public HtmlBuilder vertical(String text) {
message.append(String4j.repeat(Ascii.Symbol.VERTICAL_LINE, 2))
.append(text)
Expand Down Expand Up @@ -254,6 +286,15 @@ public HtmlBuilder code(Object value) {
return this.code(Class4j.isPrimitive(value.getClass()) ? value.toString() : Json4j.toJson(value));
}

public HtmlBuilder code(Path filename) {
try {
String data = Os4j.readFileKeepFormat(filename);
return this.code(data);
} catch (Exception e) {
return this;
}
}

public HtmlBuilder preformatted(String text) {
message.append(Ascii.Symbol.LESS_THAN_SIGN)
.append("pre")
Expand Down Expand Up @@ -305,6 +346,15 @@ public HtmlBuilder preformatted(String lang, Object value) {
return this.preformatted(lang, Class4j.isPrimitive(value.getClass()) ? value.toString() : Json4j.toJson(value));
}

public HtmlBuilder preformatted(String lang, Path filename) {
try {
String data = Os4j.readFileKeepFormat(filename);
return this.preformatted(lang, data);
} catch (Exception e) {
return this;
}
}

public HtmlBuilder text(String text) {
message.append(text);
return this.space();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package org.bot4j.telegram.message;

import org.unify4j.common.Class4j;
import org.unify4j.common.Json4j;
import org.unify4j.common.String4j;
import org.unify4j.common.*;
import org.unify4j.model.c.Ascii;
import org.unify4j.model.enums.TimezoneType;
import org.unify4j.text.TimeFormatText;

import java.nio.file.Path;
import java.util.Date;
import java.util.TimeZone;


public class MarkdownBuilder {
Expand All @@ -13,6 +17,34 @@ public MarkdownBuilder() {
this.message = new StringBuilder();
}

public MarkdownBuilder timestamp(Date date, TimeZone timezone) {
return this.code(Time4j.format(date, timezone));
}

public MarkdownBuilder timestamp(Date date, TimeZone timezone, String format) {
return this.code(Time4j.format(date, timezone, format));
}

public MarkdownBuilder timestamp(Date date, TimezoneType timezone) {
return this.code(Time4j.format(date, timezone));
}

public MarkdownBuilder timestamp(Date date, TimezoneType timezone, String format) {
return this.code(Time4j.format(date, timezone, format));
}

public MarkdownBuilder timestamp(Date date, String format) {
return this.code(Time4j.format(date, format));
}

public MarkdownBuilder timestamp(Date date) {
return this.code(Time4j.format(date, TimeFormatText.BIBLIOGRAPHY_COMPLETE_EPOCH_PATTERN));
}

public MarkdownBuilder timestamp() {
return this.timestamp(new Date());
}

public MarkdownBuilder vertical(String text) {
message.append(String4j.repeat(Ascii.Symbol.VERTICAL_LINE, 2))
.append(text)
Expand Down Expand Up @@ -97,6 +129,15 @@ public MarkdownBuilder code(Object value) {
return this.code(Class4j.isPrimitive(value.getClass()) ? value.toString() : Json4j.toJson(value));
}

public MarkdownBuilder code(Path filename) {
try {
String data = Os4j.readFileKeepFormat(filename);
return this.code(data);
} catch (Exception e) {
return this;
}
}

public MarkdownBuilder preformatted(String text) {
message.append(String4j.repeat(Ascii.Symbol.GRAVE_ACCENT, 3))
.append(text)
Expand Down Expand Up @@ -127,6 +168,15 @@ public MarkdownBuilder preformatted(String lang, Object value) {
return this.preformatted(lang, Class4j.isPrimitive(value.getClass()) ? value.toString() : Json4j.toJson(value));
}

public MarkdownBuilder preformatted(String lang, Path filename) {
try {
String data = Os4j.readFileKeepFormat(filename);
return this.preformatted(lang, data);
} catch (Exception e) {
return this;
}
}

public MarkdownBuilder link(String text, String url) {
message.append(Ascii.Punctuation.LEFT_SQUARE_BRACKET)
.append(text)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.bot4j.telegram.model.enums;

@SuppressWarnings({"all"})
public enum TelegramIconMode {
ERROR("\uD83D\uDD34"),
SUCCESS("\uD83D\uDFE2"),
WARN("\uD83D\uDFE1"),
TRACE("\uD83D\uDCDC"),
INFO("\uD83D\uDD35");

private final String icon;

TelegramIconMode(String icon) {
this.icon = icon;
}

public String getIcon() {
return icon;
}
}
6 changes: 3 additions & 3 deletions plugin/src/test/groovy/org/bot4j/Telegram4jTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public class Telegram4jTest {

@Before
public void setUp() {
MarkdownBuilder builder = new MarkdownBuilder();
builder.text("Hello, World!");
telegram4j = new Telegram4j.Builder().botToken("6806983892:AAGcPZiuNktLFnyVWrRyOyYssECcVmNJSRo").chatId(-1002042977093L).text(builder.toString()).markdownSettings().build();
MarkdownBuilder builder = new MarkdownBuilder()
.text("Hello, World!");
telegram4j = new Telegram4j.Builder().botToken("6806983892:AAGcPZiuNktLFnyVWrRyOyYssECcVmNJSRo").chatId(-1002042977093L).text(builder).markdownSettings().build();
}

@Test
Expand Down

0 comments on commit d9bd0dc

Please sign in to comment.