laravel-notification-channels / telegram

✈️ Telegram Notifications Channel for Laravel
https://laravel-notification-channels.com/telegram/
MIT License
990 stars 165 forks source link

Add escapedLine() method to TelegramMessage #168

Closed ziming closed 1 year ago

ziming commented 1 year ago

Often we pass variables in to the ->line() method, and those variables might contain characters that need to be escaped as Telegram tries to parse the line in markdown

The following example code below will result in a server exception

$email = 'hello_world@google.com'; // _ need to be escaped. Often this is fetched from the database.
TelegramMessage::create()->line('Is your email correct? ' . $email)->send();

This PR adds a new method called escapedLine that escape those characters.

For more information see:

https://stackoverflow.com/questions/40626896/telegram-does-not-escape-some-markdown-characters

https://github.com/telegraf/telegraf/issues/1242

irazasyed commented 1 year ago

Wouldn't it be just easier to switch the mode to HTML using the options method?

ziming commented 1 year ago

hmm I have never tried html mode. but I have some lines where I intentionally use markdown too.

if i change to html mode i assume I will need to add those ugly html opening and closing tags in my line methods?

hence this approach feels cleaner to me

irazasyed commented 1 year ago

Please add docs about this in README and create a PR, if possible!

Thanks