serilog-contrib / Serilog.Sinks.Telegram.Alternative

Serilog.Sinks.Telegram.Alternative is a library to save logging information from Serilog to Telegram.
MIT License
36 stars 12 forks source link

Can't log message with _ symbol #8

Closed andryushchenko closed 3 years ago

andryushchenko commented 3 years ago

I have faced error sending log message containing symbol _ After some tests I found it should be escaped

SelfLog:

2021-04-21T13:18:40.4614642Z Trying to send message to chatId '23844465': '‼ _

_: The message occurred on 21.04.2021 16:18:39+03:00_
'.
2021-04-21T13:18:40.7309163Z Message sent to chatId '23844465': 'BadRequest'.
2021-04-21T13:18:40.7313418Z Trying to send message to chatId '23844465': '‼ \_

_: The message occurred on 21.04.2021 16:18:39+03:00_
'.
2021-04-21T13:18:41.4290726Z Message sent to chatId '23844465': 'OK'.

I'm not familiar with Telegram API so I don't know if it is a bug or known limitation, but the workaround with escaping _ before sending message looks correct

SeppPenner commented 3 years ago

I will check this and fix it if possible.

SeppPenner commented 3 years ago

I can confirm that a message with _ should work with the API. I have added a new test that confirms that. The issue must have to do with some other problem, I guess...

You can test with a URL like https://api.telegram.org/bot[yourToken]/sendMessage?chat_id=[yourChatId]&text=_ where [yourToken] and [yourChatId] are placeholders, of course.

I have made a new version 1.0.10 (There only some Nuget-Pakete are updated). Maybe this helps.

andryushchenko commented 3 years ago

I found this code https://github.com/SeppPenner/Serilog.Sinks.Telegram/blob/71a1e425a42d69be0bec353fa54c93f9dc3fbf3f/src/Serilog.Sinks.Telegram/Sinks/Telegram/TelegramClient.cs#L63 As I understand Markdown formatting mode is used, so according to documentation https://core.telegram.org/bots/api#markdown-style underscore and some other characters should be escaped.

Your test https://github.com/SeppPenner/Serilog.Sinks.Telegram/blob/71a1e425a42d69be0bec353fa54c93f9dc3fbf3f/src/Serilog.Sinks.Telegram.Tests/TestSink.cs#L83 is passed because you have two underscores and this is correct italic text fragment. Suggested simple URL request successes because no formatting mode is used.

SeppPenner commented 3 years ago

You're right, my mistake. I will check what needs to be escaped and provide an update soon.

SeppPenner commented 3 years ago

I resolved this issue in the new version (1.0.11) which should be available soon. Now, the sink uses HTML formatting instead of Markdown or MarkdownV2 as Telegram has a strange markdown flavor in some cases (Escaping didn't work as I expected it to do and so on...). Reference is here https://core.telegram.org/bots/api#html-style. The sink now escapes the mentioned entities as described by Telegram:

All <, > and & symbols that are not a part of a tag or an HTML entity must be replaced with the corresponding HTML entities (< with <, > with > and & with &).

In my test case, the underscore worked properly with that formatting. Just tell me if you have any other issues.