go-telegram-bot-api / telegram-bot-api

Golang bindings for the Telegram Bot API
https://go-telegram-bot-api.dev
MIT License
5.76k stars 889 forks source link

Fix EscapeText for MarkdownV2. #604

Open navossoc opened 1 year ago

navossoc commented 1 year ago

This pull request is pretty straight forward...

Telegram documentation says:

Any character with code between 1 and 126 inclusively can be escaped anywhere with a preceding '\' character, in which case it is treated as an ordinary character and not a part of the markup. This implies that '\' character usually must be escaped with a preceding '\' character.

Source: https://core.telegram.org/bots/api#markdownv2-style

So here goes a simple sample program to simulate the bug:

package main

import (
    "fmt"

    tg "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)

func main() {
    fmt.Println(tg.EscapeText(tg.ModeMarkdownV2, `a\b`))
    fmt.Println(tg.EscapeText(tg.ModeMarkdownV2, `a\(b)c`))
}

Before the fix:

a\b
a\\(b\)c

After the fix:

a\\b
a\\\(b\)c

In the first example: the message will be send like "ab" (the slash will disappear)

In the second example: the message will not be sent, it will error out something like this: Bad Request: can't parse entities: Character '(' is reserved and must be escaped with the preceding '\'

It's a small fix, but an important one.

[]'s

navossoc commented 1 year ago

Is this package still maintained?