BracketSpace / Notification

WordPress Notification plugin
https://docs.bracketspace.com/notification/
GNU General Public License v3.0
170 stars 30 forks source link

Can't Use Line Break on Telegram API #286

Open rulim34 opened 4 years ago

rulim34 commented 4 years ago

Bug description

I can't create line break when using webhook on Telegram Bot API

To reproduce

Steps to reproduce the behavior:

  1. Setup the webhook
  2. Use HTTP POST to https://api.telegram.org/bot_token/sendMessage
  3. Add JSON payload with line break (\n or %0A) on the text parameter
  4. The line break (\n or %0A) will be printed as character on the Telegram, it doesn't create new line.

Expected behavior

New line created when i used the line break character.

Environment

Screenshots

The line break doesn't works: image

Expected message: image

jakubmikita commented 3 years ago

Hey @rulim34 why have you closed this issue? Have you managed to get it working?

rulim34 commented 3 years ago

@Kubitomakita I changed the implementation. Now I send the raw data to my own coded webhook handler, then the webhook handler process it and send a notification to Telegram.

jakubmikita commented 3 years ago

I see, thanks for the update!

Let me reopen this ticket anyway as the underlying issue is still not resolved. I'd like to check and fix the newline characters

rulim34 commented 3 years ago

Sure. I think I know why it happened. To create a line break in Telegram, is to use \n, but when I write \n and save it, it will turn into \\n. So in the previous webhok handler implementation, I write %0A on the payload, and then the webhook handler replace it with \n and send it to Telegram.

jakubmikita commented 3 years ago

Makes sense as we have an issue with escaping \ due to JSON storage.

khaliziq99 commented 2 years ago

Thanks. It works for me. I'm using the %0A. I've searching everywhere for this solution. Thank you so much.

jazzjaymel commented 2 years ago

Try urlencode($text). It works for me.

n0099 commented 2 years ago

I've figured out that only using the json unicode code point escape form: \u000a(neither \n \\n \\\\n %0A %25%0A) will preserve the real line break char, and you have to manually insert or replace all \\\\n (the double escaped \n after you write some \n in notification content) with the raw \u000a into the notification post content in your wp_posts table, for replacing it would be like: UPDATE wp_posts SET post_content = REPLACE(post_content, "\\\\\\\\n", "\\u000a") WHERE post_type = "notification" note that \\\\\\\\n is a quadruple escaped \n for represeting the double escaped \n, what a leaning toothpick it is!

n0099 commented 2 years ago

https://github.com/BracketSpace/Notification/issues/286#issuecomment-753294425

when I write \n and save it, it will turn into \\n

It's caused by some escape operations done by the post content filter when the notification is being updated, also any chars within a <> pair will be removed by strip_tags(), so writing <{user_email}> will result in an empty string, we have to use its corresponding html entity like &lt;&gt; to avoid this.

jakubmikita commented 2 years ago

Excellent find @n0099! I plan to move the notifications to a separate database table, which would resolve many issues.

ephi2github commented 1 year ago

God Bless you peeple. %0A is working for me

alexey-sh commented 5 months ago

I spent about two days trying to figure out why the following was not working

curl -v -F parse_mode='Markdownv2' --form-string "text=bundle 123 \\n\\nabc" "https://api.telegram.org/bot${BOT_TOKEN}/sendMessage?chat_id=${CHAT_ID}

I tried "text=bundle 123 \n\nabc" "text=bundle 123 \n\nabc" "text=bundle 123 \\n\nabc" "text=bundle 123 \\\\n\nabc" without any luck. %0A does nothing too.

Finally found a working solution

curl -v -F "document=@./bundle.zip"  -F parse_mode='HTML' "https://api.telegram.org/bot${BOT_TOKEN}/sendDocument?chat_id=${CHAT_ID}&caption=1%0A22%0A23"
quangnguyen3499 commented 5 months ago

Sure. I think I know why it happened. To create a line break in Telegram, is to use \n, but when I write \n and save it, it will turn into \\n. So in the previous webhok handler implementation, I write %0A on the payload, and then the webhook handler replace it with \n and send it to Telegram.

Cool, it works!

mxmrykov commented 2 months ago

I have the same problem, but nothing works for me. So I used parsed_mode: MarkdownV2 (tap documentation) and double quotes. \r\n works for me.

P.s. nah 4 years passed :/

Joaquimborges commented 1 month ago

Thanks. It works for me. I'm using the %0A. I've searching everywhere for this solution. Thank you so much.

%0A works perfectly!