-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat: add Markdown builder for telegram message #3
- Loading branch information
1 parent
841c35c
commit 0f649bb
Showing
6 changed files
with
292 additions
and
3 deletions.
There are no files selected for viewing
Binary file not shown.
174 changes: 174 additions & 0 deletions
174
plugin/src/main/groovy/org/bot4j/telegram/message/MarkdownBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,5 @@ | |
|
||
public enum TelegramTextMode { | ||
|
||
Markdown, | ||
HTML | ||
Markdown, MarkdownV2, HTML | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
plugin/src/test/groovy/org/bot4j/MarkdownBuilderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters