irazasyed / telegram-bot-sdk

🤖 Telegram Bot API PHP SDK. Lets you build Telegram Bots easily! Supports Laravel out of the box.
https://telegram-bot-sdk.com
BSD 3-Clause "New" or "Revised" License
3.04k stars 671 forks source link

MarkdownV2 support? #1071

Closed fivepe closed 1 year ago

fivepe commented 1 year ago

PHP version

8.1

irazasyed/telegram-bot-sdk version

versions : * v3.6

Laravel version (if any)

No response

Code To Reproduce the bug

At telegram api docs present parse_mode = MarkdownV2. I tried to send some data and find, that needed new escape for support this format.

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

    public static function escapeMarkdownV2($text)
    {
        $markdown = [
            '#',
            '*',
            '_',
            '=',
            '.',
            '[',
            ']',
            '(',
            ')',
            // ... rest of markdown entities
        ];
        //'_‘, ’*‘, , ’~‘, ’`‘, ’>‘, ’#‘, ’+‘, ’-‘, ’=‘, ’|‘, ’{‘, ’}‘, ’.‘, ’!‘
        $replacements = [
            '\#',
            '\*',
            '\_',
            '\\=',
            '\.',
            '\[',
            '\]',
            '\(',
            '\)',
            // ... rest of corresponding escaped markdown
        ];

        return str_replace($markdown, $replacements, $text);
    }

can do something like this, as i think.

use it more easy.

  $telegram->sendMessage([
      'chat_id' => $group->chat_id,
      'text' => $msg,
     'disable_web_page_preview'=> true,
     'parse_mode' => 'MarkdownV2'
  ]);

Error stacktrace (if any)

No response

irazasyed commented 1 year ago

That's not how it should be. If you escape everything in the Markdown format, then what's the point of sending it in Markdown format? It'll send everything escaped and without formatting the way it should be.

You're supposed to manually escape certain special chars.