Skip to content

Commit

Permalink
✨ feat: add Markdown builder for telegram message #3
Browse files Browse the repository at this point in the history
  • Loading branch information
pnguyen215 committed Jul 4, 2024
1 parent 841c35c commit 0f649bb
Show file tree
Hide file tree
Showing 6 changed files with 292 additions and 3 deletions.
Binary file modified libs/unify4j-v1.0.0.jar
Binary file not shown.
174 changes: 174 additions & 0 deletions plugin/src/main/groovy/org/bot4j/telegram/message/MarkdownBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
package org.bot4j.telegram.message;

import org.unify4j.common.Class4j;
import org.unify4j.common.Json4j;
import org.unify4j.common.String4j;
import org.unify4j.model.c.Ascii;


public class MarkdownBuilder {
protected final StringBuilder message;

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

public MarkdownBuilder vertical(String text) {
message.append(String4j.repeat(Ascii.Symbol.VERTICAL_LINE, 2))
.append(text)
.append(String4j.repeat(Ascii.Symbol.VERTICAL_LINE, 2));
return this.space();
}

public MarkdownBuilder vertical(Object value) {
if (value == null) {
return this;
}
return this.vertical(Class4j.isPrimitive(value.getClass()) ? value.toString() : Json4j.toJson(value));
}

public MarkdownBuilder bold(String text) {
message.append(Ascii.Punctuation.ASTERISK)
.append(text)
.append(Ascii.Punctuation.ASTERISK);
return this.space();
}

public MarkdownBuilder bold(Object value) {
if (value == null) {
return this;
}
return this.bold(Class4j.isPrimitive(value.getClass()) ? value.toString() : Json4j.toJson(value));
}

public MarkdownBuilder italic(String text) {
message.append(Ascii.Punctuation.LOW_LINE)
.append(text)
.append(Ascii.Punctuation.LOW_LINE);
return this.space();
}

public MarkdownBuilder italic(Object value) {
if (value == null) {
return this;
}
return this.italic(Class4j.isPrimitive(value.getClass()) ? value.toString() : Json4j.toJson(value));
}

public MarkdownBuilder strikethrough(String text) {
message.append(Ascii.Symbol.TILDE)
.append(text)
.append(Ascii.Symbol.TILDE);
return this.space();
}

public MarkdownBuilder strikethrough(Object value) {
if (value == null) {
return this;
}
return this.strikethrough(Class4j.isPrimitive(value.getClass()) ? value.toString() : Json4j.toJson(value));
}

public MarkdownBuilder underline(String text) {
message.append(String4j.repeat(Ascii.Punctuation.LOW_LINE, 2))
.append(text)
.append(String4j.repeat(Ascii.Punctuation.LOW_LINE, 2));
return this.space();
}

public MarkdownBuilder underline(Object value) {
if (value == null) {
return this;
}
return this.underline(Class4j.isPrimitive(value.getClass()) ? value.toString() : Json4j.toJson(value));
}

public MarkdownBuilder code(String text) {
message.append(Ascii.Symbol.GRAVE_ACCENT)
.append(text)
.append(Ascii.Symbol.GRAVE_ACCENT);
return this.space();
}

public MarkdownBuilder code(Object value) {
if (value == null) {
return this;
}
return this.code(Class4j.isPrimitive(value.getClass()) ? value.toString() : Json4j.toJson(value));
}

public MarkdownBuilder preformatted(String text) {
message.append(String4j.repeat(Ascii.Symbol.GRAVE_ACCENT, 3))
.append(text)
.append(String4j.repeat(Ascii.Symbol.GRAVE_ACCENT, 3));
return this.space();
}

public MarkdownBuilder preformatted(Object value) {
if (value == null) {
return this;
}
return this.preformatted(Class4j.isPrimitive(value.getClass()) ? value.toString() : Json4j.toJson(value));
}

public MarkdownBuilder preformatted(String lang, String text) {
message.append(String4j.repeat(Ascii.Symbol.GRAVE_ACCENT, 3))
.append(lang)
.append(Ascii.Punctuation.SPACE)
.append(text)
.append(String4j.repeat(Ascii.Symbol.GRAVE_ACCENT, 3));
return this.space();
}

public MarkdownBuilder preformatted(String lang, Object value) {
if (value == null) {
return this;
}
return this.preformatted(lang, Class4j.isPrimitive(value.getClass()) ? value.toString() : Json4j.toJson(value));
}

public MarkdownBuilder link(String text, String url) {
message.append(Ascii.Punctuation.LEFT_SQUARE_BRACKET)
.append(text)
.append(Ascii.Punctuation.RIGHT_SQUARE_BRACKET)
.append(Ascii.Punctuation.LEFT_PARENTHESIS)
.append(url)
.append(Ascii.Punctuation.RIGHT_PARENTHESIS);
return this.space();
}

public MarkdownBuilder text(String text) {
message.append(text);
return this.space();
}

public MarkdownBuilder text(Object value) {
if (value == null) {
return this;
}
return this.text(Class4j.isPrimitive(value.getClass()) ? value.toString() : Json4j.toJson(value));
}

public MarkdownBuilder line() {
return this.text(System.lineSeparator());
}

public MarkdownBuilder line(int repeat) {
return this.text(String4j.repeat(System.lineSeparator(), repeat));
}

public MarkdownBuilder space() {
message.append(Ascii.Punctuation.SPACE);
return this;
}

public MarkdownBuilder space(int repeat) {
message.append(String4j.repeat(Ascii.Punctuation.SPACE, repeat));
return this;
}

@Override
public String toString() {
return message.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@

public enum TelegramTextMode {

Markdown,
HTML
Markdown, MarkdownV2, HTML
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.bot4j.telegram.model.rd;

import org.bot4j.telegram.message.MarkdownBuilder;
import org.bot4j.telegram.model.builder.TelegramConnectionBuilder;
import org.bot4j.telegram.model.builder.TelegramOptionBuilder;
import org.bot4j.telegram.model.enums.TelegramTextMode;
Expand Down Expand Up @@ -90,6 +91,13 @@ public T text(Object value) {
return this.self();
}

public T text(MarkdownBuilder builder) {
if (builder == null) {
return this.self();
}
return this.text(builder.toString());
}

public T parseMode(String mode) {
this.parseMode = mode;
return this.self();
Expand All @@ -103,6 +111,10 @@ public T markdownSettings() {
return this.parseMode(TelegramTextMode.Markdown);
}

public T markdownV2Settings() {
return this.parseMode(TelegramTextMode.MarkdownV2);
}

public T htmlSettings() {
return this.parseMode(TelegramTextMode.HTML);
}
Expand Down
101 changes: 101 additions & 0 deletions plugin/src/test/groovy/org/bot4j/MarkdownBuilderTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package org.bot4j;

import org.bot4j.telegram.message.MarkdownBuilder;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class MarkdownBuilderTest {

private MarkdownBuilder builder;

@Before
public void setUp() {
builder = new MarkdownBuilder();
}

@Test
public void testVertical() {
String result = builder.vertical("text").toString();
assertEquals("||text|| ", result);
}

@Test
public void testBold() {
String result = builder.bold("text").toString();
assertEquals("*text* ", result);
}

@Test
public void testItalic() {
String result = builder.italic("text").toString();
assertEquals("_text_ ", result);
}

@Test
public void testStrikethrough() {
String result = builder.strikethrough("text").toString();
assertEquals("~text~ ", result);
}

@Test
public void testUnderline() {
String result = builder.underline("text").toString();
assertEquals("__text__ ", result);
}

@Test
public void testCode() {
String result = builder.code("text").toString();
assertEquals("`text` ", result);
}

@Test
public void testPreformatted() {
String result = builder.preformatted("text").toString();
assertEquals("```text``` ", result);
}

@Test
public void testPreformattedWithLang() {
String result = builder.preformatted("java", "text").toString();
assertEquals("```java text``` ", result);
}

@Test
public void testLink() {
String result = builder.link("text", "http://example.com").toString();
assertEquals("[text](http://example.com) ", result);
}

@Test
public void testText() {
String result = builder.text("text").toString();
assertEquals("text ", result);
}

@Test
public void testLine() {
String result = builder.line().toString();
assertEquals(System.lineSeparator() + " ", result);
}

@Test
public void testLineWithRepeat() {
String result = builder.line(2).toString();
assertEquals(System.lineSeparator().repeat(2) + " ", result);
}

@Test
public void testSpace() {
String result = builder.space().toString();
assertEquals(" ", result);
}

@Test
public void testSpaceWithRepeat() {
String result = builder.space(2).toString();
assertEquals(" ", result);
}
}
5 changes: 4 additions & 1 deletion plugin/src/test/groovy/org/bot4j/Telegram4jTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.bot4j;

import org.bot4j.telegram.common.Telegram4j;
import org.bot4j.telegram.message.MarkdownBuilder;
import org.junit.Before;
import org.junit.Test;
import org.unify4j.model.response.WrapResponse;
Expand All @@ -15,7 +16,9 @@ public class Telegram4jTest {

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

@Test
Expand Down

0 comments on commit 0f649bb

Please sign in to comment.