Skip to content

Commit

Permalink
♻️ refactor: update message builder #5
Browse files Browse the repository at this point in the history
  • Loading branch information
pnguyen215 committed Jul 6, 2024
1 parent d9bd0dc commit 75cfea6
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 4 deletions.
17 changes: 17 additions & 0 deletions plugin/src/main/groovy/org/bot4j/telegram/message/HtmlBuilder.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.bot4j.telegram.message;

import org.bot4j.telegram.model.enums.TelegramIconMode;
import org.unify4j.common.*;
import org.unify4j.model.c.Ascii;
import org.unify4j.model.enums.TimezoneType;
Expand All @@ -16,6 +17,22 @@ public HtmlBuilder() {
this.message = new StringBuilder();
}

public HtmlBuilder icon(TelegramIconMode mode) {
if (mode == null) {
return this;
}
message.append(mode.getIcon());
return this.space();
}

public HtmlBuilder icon(TelegramIconMode mode, int repeat) {
if (mode == null) {
return this;
}
message.append(String4j.repeat(mode.getIcon(), repeat));
return this.space();
}

public HtmlBuilder timestamp(Date date, TimeZone timezone) {
return this.code(Time4j.format(date, timezone));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.bot4j.telegram.message;

import org.bot4j.telegram.model.enums.TelegramIconMode;
import org.unify4j.common.*;
import org.unify4j.model.c.Ascii;
import org.unify4j.model.enums.TimezoneType;
Expand All @@ -9,14 +10,29 @@
import java.util.Date;
import java.util.TimeZone;


public class MarkdownBuilder {
protected final StringBuilder message;

public MarkdownBuilder() {
this.message = new StringBuilder();
}

public MarkdownBuilder icon(TelegramIconMode mode) {
if (mode == null) {
return this;
}
message.append(mode.getIcon());
return this.space();
}

public MarkdownBuilder icon(TelegramIconMode mode, int repeat) {
if (mode == null) {
return this;
}
message.append(String4j.repeat(mode.getIcon(), repeat));
return this.space();
}

public MarkdownBuilder timestamp(Date date, TimeZone timezone) {
return this.code(Time4j.format(date, timezone));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.bot4j.telegram.message;

public abstract class MessageFactory {

public static MarkdownBuilder createMarkdownBuilder() {
return new MarkdownBuilder();
}

public static HtmlBuilder createHtmlBuilder() {
return new HtmlBuilder();
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
package org.bot4j.telegram.model.enums;

@SuppressWarnings({"all"})
// @formatter:off
public enum TelegramIconMode {
ERROR("\uD83D\uDD34"),
SUCCESS("\uD83D\uDFE2"),
WARN("\uD83D\uDFE1"),
TRACE("\uD83D\uDCDC"),
INFO("\uD83D\uDD35");
INFO("\uD83D\uDD35"),
SOS("\uD83C\uDD98"),
MESSAGE("\uD83D\uDCAC"),
TS_1("\uD83C\uDFAF"),
TS_2("\uD83D\uDDD3"),
NOTIFY("\uD83D\uDCE3"),
TADA("\uD83C\uDF89"),
SETTING("\uD83D\uDD27"),
ROCKET("\uD83D\uDE80"),
KEY("\uD83D\uDD11"),
BOT("\uD83E\uDD16"),
USER("\uD83E\uDDD1\uD83C\uDFFB\u200D\uD83D\uDCBB"),
BUG_1("\uD83D\uDC1E"),
BUG_2("\uD83D\uDC7E"),
BUG_3("\uD83E\uDEB2"),
BUG_4("\uD83D\uDC1B");

private final String icon;

Expand All @@ -18,3 +34,4 @@ public String getIcon() {
return icon;
}
}
// @formatter:on
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package org.bot4j.telegram.model.enums;

// @formatter:off
public enum TelegramTextMode {

Markdown, MarkdownV2, HTML
Markdown,
MarkdownV2,
HTML
}
// @formatter:on

0 comments on commit 75cfea6

Please sign in to comment.