skip to Main Content

I’m currently working on with telegram Bot API, but I have to validate the markdown syntax to prevent parse errors. But telegram bot api’s markdown doesn’t follow the regular markdown syntax so I’m kind of struggling how to do it. Is there a proper way to validate it? Or is there such kind of library that I can use?

2

Answers


  1. Probably not the answer OP was expecting, but still sharing so others may find this useful.

    I’ve been trying to ‘validate’ the MarkDown messages to prevent the Bad Request: can't parse entities: error received by Telegram. For example, the same issue this user encountered

    Unfortunately I was unable to parse this with 100% accuracy, probably because (as you already mentioned) Telegram doesn’t use the default Markdown syntax.


    My ‘solution’ as I’ve implemented in quite some bots, and is working decent.

    After sending a message (I’ve created a custom function to prevent duplicate code), check if Telegram responded with the Bad Request: can't parse entities error, If so, send the same message again, but this time with HTML parse_mode, this way there won’t be any parse errors.

    Not the most clean solution, but it gets the message to the user, and that was my greatest concern.

    Login or Signup to reply.
  2. I have implemented this validation function (Golang). Works pretty good for telegram’s markdown:

    func GetUnclosedTag(markdown string) string {
        // order is important!
        var tags = []string{
            "```",
            "`",
            "*",
            "_",
        }
        var currentTag = ""
    
        markdownRunes := []rune(markdown)
    
        var i = 0
    outer:
        for i < len(markdownRunes) {
            // skip escaped characters (only outside tags)
            if markdownRunes[i] == '\' && currentTag == "" {
                i += 2
                continue
            }
            if currentTag != "" {
                if strings.HasPrefix(string(markdownRunes[i:]), currentTag) {
                    // turn a tag off
                    i += len(currentTag)
                    currentTag = ""
                    continue
                }
            } else {
                for _, tag := range tags {
                    if strings.HasPrefix(string(markdownRunes[i:]), tag) {
                        // turn a tag on
                        currentTag = tag
                        i += len(currentTag)
                        continue outer
                    }
                }
            }
            i++
        }
    
        return currentTag
    }
    func IsValid(markdown string) bool {
        return GetUnclosedTag(markdown) == ""
    }
    
    func FixMarkdown(markdown string) string {
        tag := GetUnclosedTag(markdown)
        if tag == "" {
            return markdown
        }
        return markdown + tag
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search