Encoding string to Telegram

Hello,

I'm using a Telegram BOT to receive my TTN messages. Is working fine but I would like to use the MarkdownV2 option which also offers underline and strikethrough. When using this parse_mode I get errors on several characters such as '-' and '()'. The debugger tells me they need to be escaped. But when I do so, I still get the same error..?

Is there any function available to check/escape characters in a string?

Kind regards,

Peter Hunt
Zoetermeer
Netherlands

An you send me an example string?
I will try to do that inside the sender node when markdown is enablef

Hi Peter,
I ran into a similar issue with another telegram node, where my string contained html entities depicting things like smart quotes. I wrote a node for it, but it mostly handles the special things and is probably not too useful for escaping regular characters, except perhaps with one setting.

~Lena

Hi,

Now I also have some issues with the regular Markdown, this is my coding ;

{
    "chatId": "-4x9x5x4x6",
    "type": "message",
    "content": "{{ payload }}",
    "options": { 
        "parse_mode": "Markdown"
    }
}

The string is "zalkerbos_cms_ketelhuis_1 overdue for 5 days!"

The error is "Error: ETELEGRAM: 400 Bad Request: can't parse entities: Can't find end of the entity starting at byte offset 23"

What is payload? You mean msg.payload?

Yes... I found this example somewhere, but judging by your reply this isn't common? It's working though, except for the Markdown. Could this be my issue?

Well the problem is that your string contains underscores _ which are interpreted as markdown control characters.
Text between two undersores is written in italic style.
But as your string only contains 3 underscores the third underscore confuses the parser as it thinks that you forgot the closing one (the 4th).

The error message indicates that at position 25 some formatting starts without a matching _ before the end of the string.

zalkerbos_cms_ketelhuis_1 overdue for 5 days!

I added an underscore and now the message is sent. Though that is obviously not what you wanted to achieve
image

Well, if it is only the underscore you can escape it as follows:

var str = msg.topic.replace(/_/g, "\\_");
msg.payload = {
    chatId: 123,
    type: "message",
    content: str,
    options: { 
        parse_mode: "Markdown"
    }
}

return msg;

I passed your string using an inject node therefore msg.topic contains the relevant string.

Here is a short flow

[{"id":"ba1931f2.6acf9","type":"telegram sender","z":"c513a2fb.133e3","name":"","bot":"19f02f8b.fa0c5","x":630,"y":80,"wires":[]},{"id":"fef80bb2.c2c278","type":"inject","z":"c513a2fb.133e3","name":"","topic":"zalkerbos_cms_ketelhuis_1 overdue for 5 days!","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":100,"wires":[["f10dd4b3.a198d8"]]},{"id":"f10dd4b3.a198d8","type":"function","z":"c513a2fb.133e3","name":"","func":"var str = msg.topic.replace(//g, "\\");\nmsg.payload = {\n chatId: 138708568,\n type: "message",\n content: str,\n options: { \n parse_mode: "Markdown"\n }\n}\n\nreturn msg;","outputs":1,"noerr":0,"x":330,"y":100,"wires":[["a432daea.b1a618","ba1931f2.6acf9"]]},{"id":"a432daea.b1a618","type":"debug","z":"c513a2fb.133e3","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":470,"y":160,"wires":},{"id":"19f02f8b.fa0c5","type":"telegram bot","z":"c513a2fb.133e3","botname":"Foo","usernames":"","chatids":"","baseapiurl":"","updatemode":"polling","pollinterval":"300","usesocks":false,"sockshost":"","socksport":"","socksusername":"anonymous","sockspassword":"none","bothost":"","localbotport":"","publicbotport":"","privatekey":"","certificate":"","useselfsignedcertificate":false,"sslterminated":false,"verboselogging":true}]

Output will look like
image

1 Like

Ok, I got it working now, exactly the way I want. But ;

msg.payload = msg.payload.replace(/-/g, "\-");
msg.payload = msg.payload.replace(/./g, "\.");
msg.payload = msg.payload.replace(/(/g, "\(");
msg.payload = msg.payload.replace(/)/g, "\)");

Isn't there any better way of escaping characters?

instead of {{ payload }} you could try {{{ payload }}}
as triple braces should stop escaping things inside... - but I'm not sure in this instance.

That's what I'm using already;

"type": "message",
"content": "{{{ payload }}}",
"options": { 
    "parse_mode": "MarkdownV2"
}

Anyhows... This is exactly how I wanted it to display;

image

  • Underlining the device-name
  • Bolding a raised alarm
  • Striking through a ceased alarm

But still; if any other characters enter the flow in future updates which should be escaped, the number of 'escaping lines' will keep increasing. So then again; is there any more convenient way of escaping (a set of) special characters?

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.