# Encoding string to Telegram

**URL:** <https://discourse.nodered.org/t/encoding-string-to-telegram/24692>\
**Category:** General\
**Created:** [12 April 2020 12:09 UTC](https://discourse.nodered.org/t/encoding-string-to-telegram/24692 "2020-04-12T12:09:33Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hunter](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hunter/32/60045_2.png) [@Hunter](https://discourse.nodered.org/u/Hunter)\
**Post date:** [12 April 2020 12:09 UTC](https://discourse.nodered.org/t/encoding-string-to-telegram/24692/1 "2020-04-12T12:09:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![windkh](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/windkh/32/4713_2.png) [@windkh](https://discourse.nodered.org/u/windkh)\
**Post date:** [14 April 2020 22:12 UTC](https://discourse.nodered.org/t/encoding-string-to-telegram/24692/2 "2020-04-14T22:12:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [15 April 2020 05:37 UTC](https://discourse.nodered.org/t/encoding-string-to-telegram/24692/3 "2020-04-15T05:37:26Z")

</div>

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.

> **[node-red-contrib-html-entities](https://flows.nodered.org/node/node-red-contrib-html-entities)**
>
> Tiny node-red wrapper node around the html entities library called 'he'.

~Lena

---

<div class="post-metadata">

**Author:** ![Hunter](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hunter/32/60045_2.png) [@Hunter](https://discourse.nodered.org/u/Hunter)\
**Post date:** [16 April 2020 16:49 UTC](https://discourse.nodered.org/t/encoding-string-to-telegram/24692/4 "2020-04-16T16:49:00Z")

</div>

Hi,

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

```auto
{
    "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"

---

<div class="post-metadata">

**Author:** ![windkh](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/windkh/32/4713_2.png) [@windkh](https://discourse.nodered.org/u/windkh)\
**Post date:** [16 April 2020 18:38 UTC](https://discourse.nodered.org/t/encoding-string-to-telegram/24692/5 "2020-04-16T18:38:03Z")

</div>

What is payload? You mean msg.payload?

---

<div class="post-metadata">

**Author:** ![Hunter](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hunter/32/60045_2.png) [@Hunter](https://discourse.nodered.org/u/Hunter)\
**Post date:** [16 April 2020 18:50 UTC](https://discourse.nodered.org/t/encoding-string-to-telegram/24692/6 "2020-04-16T18:50:12Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![windkh](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/windkh/32/4713_2.png) [@windkh](https://discourse.nodered.org/u/windkh)\
**Post date:** [16 April 2020 20:30 UTC](https://discourse.nodered.org/t/encoding-string-to-telegram/24692/7 "2020-04-16T20:30:17Z")

</div>

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/d/dd8120acf8e25f61c8d1f0125a09b75d275d71ee.png)

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

```auto
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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/3/a3ea43a4203489142029114997622905fcd69329.png)

---

<div class="post-metadata">

**Author:** ![Hunter](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hunter/32/60045_2.png) [@Hunter](https://discourse.nodered.org/u/Hunter)\
**Post date:** [17 April 2020 21:23 UTC](https://discourse.nodered.org/t/encoding-string-to-telegram/24692/8 "2020-04-17T21:23:55Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [17 April 2020 21:27 UTC](https://discourse.nodered.org/t/encoding-string-to-telegram/24692/9 "2020-04-17T21:27:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Hunter](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hunter/32/60045_2.png) [@Hunter](https://discourse.nodered.org/u/Hunter)\
**Post date:** [17 April 2020 21:36 UTC](https://discourse.nodered.org/t/encoding-string-to-telegram/24692/10 "2020-04-17T21:36:37Z")

</div>

That's what I'm using already;

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

---

<div class="post-metadata">

**Author:** ![Hunter](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hunter/32/60045_2.png) [@Hunter](https://discourse.nodered.org/u/Hunter)\
**Post date:** [17 April 2020 21:45 UTC](https://discourse.nodered.org/t/encoding-string-to-telegram/24692/11 "2020-04-17T21:45:11Z")

</div>

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

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/2/12385855cf71221ffe4530765ca58562783a82c7.png)

- 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?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [16 June 2020 21:45 UTC](https://discourse.nodered.org/t/encoding-string-to-telegram/24692/12 "2020-06-16T21:45:15Z")

</div>

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