mdparser for telegram bot API
First you need to get the package:
go get -u github.com/ALiwoto/mdparser
then you can use the package like this:
import "github.com/ALiwoto/mdparser"
func sendMessage(msg Message) {
md := mdparser.GetBold("This is a message")
md.Normal(":\n")
md.Italic("Italic\n")
md.Styled("Bold + Italic\n", mdparser.StyleBold, mdparser.StyleItalic)
md.Mono("Mono space\n")
md.CodeBlockLang("go", "fmt.Println(\"hello\")")
md.HyperLink("text", "https://google.com")
msg.Reply(md.ToString(), options{version: "MarkdownV2"})
}
All instance methods mutate the current markdown. If you need a copy first, clone explicitly:
base := mdparser.GetNormal("hello")
copy := base.Clone().Bold(" world")Telegram officially documents nested message entities in the Bot API docs:
Important rules from the official docs:
- Nested entities are supported, but if two entities overlap, one must fully contain the other.
bold,italic,underline,strike-through, andspoilermay be nested in each other.codeandpreare special and have stricter nesting limits.- Legacy
Markdownis limited; useMarkdownV2if you need modern formatting behavior.
That means combined formatting is real and officially supported. For example, bold + italic on the same words is valid when represented according to Telegram's nesting rules.
Current rule:
Clone()is the only copy operation.- All other instance methods mutate the receiver.
Removed methods:
AppendThisAppendNormalAppendBoldAppendItalicAppendMonoAppendUnderlineAppendStrikeAppendHyperLinkAppendMentionAppendSpoilerReplaceMdThisReplaceMdThisNElThisSpaceThisTabThis
Replacement pattern:
- Use the non-
Thismethod directly for mutation. - If old code expected copy-style behavior, call
Clone()first.
Examples:
md.AppendBold("x")->md.Clone().Bold("x")md.AppendBoldThis("x")->md.Bold("x")md.AppendThis(other)->md.Append(other)md.ReplaceMdThis(old, new)->md.ReplaceMd(old, new)md.ElThis()->md.El()
Behavior changes:
AppendReplaceMdReplaceMdNReplaceToNewReplaceToNewNElSpaceTab
These methods still exist, but they now mutate the current markdown. If older code relied on copy-style behavior, migrate to md.Clone().SomeMethod(...).