Notification icon indicating copy to clipboard operation
Notification copied to clipboard

Can't Use Line Break on Telegram API

Open rulim34 opened this issue 5 years ago • 15 comments

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

  • PHP: 7.4
  • WordPress: 5.4
  • Notification plugin: 7.1.1

Screenshots

The line break doesn't works: image

Expected message: image

rulim34 avatar Jul 27 '20 06:07 rulim34

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

jakubmikita avatar Dec 31 '20 17:12 jakubmikita

@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.

rulim34 avatar Dec 31 '20 17:12 rulim34

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

jakubmikita avatar Dec 31 '20 17:12 jakubmikita

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.

rulim34 avatar Jan 01 '21 09:01 rulim34

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

jakubmikita avatar Jan 01 '21 09:01 jakubmikita

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

khaliziq99 avatar Sep 28 '22 02:09 khaliziq99

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

jazzjaymel avatar Oct 16 '22 21:10 jazzjaymel

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 avatar Nov 12 '22 21:11 n0099

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.

n0099 avatar Nov 12 '22 21:11 n0099

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

jakubmikita avatar Nov 13 '22 08:11 jakubmikita

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

ephi2github avatar Nov 22 '22 16:11 ephi2github