Skip to content

Instantly share code, notes, and snippets.

@QYG2297248353
Created April 23, 2025 10:53
Show Gist options
  • Select an option

  • Save QYG2297248353/79d4842e592ffbd9e5435692206b3f9a to your computer and use it in GitHub Desktop.

Select an option

Save QYG2297248353/79d4842e592ffbd9e5435692206b3f9a to your computer and use it in GitHub Desktop.
MarkdownV2Utils Telegram 消息构建器
import org.apache.commons.lang3.StringUtils;
import java.util.Set;
/**
* MarkdownV2 工具类
* <p>
* Telegram MarkdownV2
* 语法参考:https://core.telegram.org/bots/api#markdownv2-style
*
* @author ms
*/
public class MarkdownV2Utils {
/**
* 转义文本
* <p>
* 需要转义的字符
*/
private static final Set<Character> SPECIAL_CHARS = Set.of(
'_', '*', '[', ']', '(', ')', '~', '`', '>', '#', '+', '-', '=', '|', '{', '}', '.', '!'
);
/**
* 转义文本
*
* @param text 需要转义的文本
* @return 转义后的文本
*/
public static String escape(String text) {
if (StringUtils.isBlank(text)) {
return "";
}
StringBuilder sb = new StringBuilder();
for (char c : text.toCharArray()) {
if (SPECIAL_CHARS.contains(c)) {
sb.append("\\\\");
}
sb.append(c);
}
return sb.toString();
}
/**
* MarkdownV2 构建器
*
* @return MarkdownV2 构建器
*/
public static Builder builder() {
return new Builder();
}
public static class Builder {
private final StringBuilder sb = new StringBuilder();
/**
* 斜体文本
*
* @param text 斜体文本
* @return MarkdownV2 构建器
*/
public Builder italic(String text) {
return raw("_" + escape(text) + "_");
}
/**
* 原文
*
* @param text 原文
* @return MarkdownV2 构建器
*/
public Builder raw(String text) {
sb.append(text);
return this;
}
/**
* 下划线
*
* @param text 下划线
* @return MarkdownV2 构建器
*/
public Builder underline(String text) {
return raw("__" + escape(text) + "__");
}
/**
* 删除线
*
* @param text 删除线
* @return MarkdownV2 构建器
*/
public Builder strikethrough(String text) {
return raw("~" + escape(text) + "~");
}
/**
* 剧透文本
*
* @param text 剧透文本
* @return MarkdownV2 构建器
*/
public Builder spoiler(String text) {
return raw("||" + escape(text) + "||");
}
/**
* 行内代码
*
* @param text 行内代码
* @return MarkdownV2 构建器
*/
public Builder blockCode(String text) {
return raw("```\n" + escape(text) + "\n```");
}
/**
* 行内代码
*
* @param text 行内代码
* @param language 代码语言
* @return MarkdownV2 构建器
*/
public Builder blockCode(String text, String language) {
return raw("```" + language + "\n" + escape(text) + "\n```");
}
/**
* 引用块
*
* @param text 引用块
* @return MarkdownV2 构建器
*/
public Builder quotationBlock(String... text) {
for (String s : text) {
raw(">" + escape(s) + "\n");
}
return this;
}
/**
* 折叠引用块
*
* @param text 折叠引用块
* @return MarkdownV2 构建器
*/
public Builder quotationFoldBlock(String... text) {
raw("**>\n");
for (String s : text) {
raw(">" + escape(s) + "\n");
}
raw("||\n");
return this;
}
/**
* 链接文本
*
* @param text 链接文本
* @param url 链接地址
* @return MarkdownV2 构建器
*/
public Builder link(String text, String url) {
return raw("[" + escape(text) + "](" + url + ")");
}
/**
* 加粗文本
*
* @param text 加粗文本
* @return MarkdownV2 构建器
*/
public Builder bold(String text) {
return raw("*" + escape(text) + "*");
}
/**
* 指令描述 - 自定义
*
* @param cmd 指令
* @param desc 描述
* @return MarkdownV2 构建器
*/
public Builder command(String cmd, String desc) {
return inlineCode(cmd).newSpace().text(desc).newline();
}
/**
* 换行
*
* @return MarkdownV2 构建器
*/
public Builder newline() {
return raw("\n");
}
/**
* 转义文本
*
* @param text 转义文本
* @return MarkdownV2 构建器
*/
public Builder text(String text) {
sb.append(escape(text));
return this;
}
/**
* 空格
*
* @return MarkdownV2 构建器
*/
public Builder newSpace() {
return raw("\t\t\t\t\t\t");
}
/**
* 行内代码
*
* @param text 行内代码
* @return MarkdownV2 构建器
*/
public Builder inlineCode(String text) {
return raw("`" + escape(text) + "`");
}
/**
* 生成 MarkdownV2 文本
*
* @return MarkdownV2 文本
*/
public String build() {
return sb.toString();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment