0

I'm having trouble formatting a message in Telegram. I need to output each word on a new line.

Example 1:

const text = "word1\nword2\nword3"

Result #1

This works, but when I try to output in wildcard quotes (``), it stops working.

Example 2:

const a = "amobus"
const b = "amogus"
const c = "autobus"

const text2 = `
    word1: ${a}
    word2: ${b}
    word3: ${c}
`

or

const a = "amobus"
const b = "amogus"
const c = "autobus"

const text2 = `word1: ${a}\nword2: ${b}\nword3: ${c}`

or

const a = "amobus"
const b = "amogus"
const c = "autobus"

const text2 = "word1: " + a + "\n" + "word2: " + b + "\n" + "word3: " + c

result 2

This is what it looks like in a real example:

result 3

But the static text with line breaks is output fine:

result 4

P.S. In the query I use parse_mode=HTML

const url =`https://api.telegram.org/bot${TOKEN}/sendMessagechat_id=${testID}&text=${message}&parse_mode=HTML`
  • That your second example gets additional whitespace before the words on the 2nd and 3rd line is clear - you introduced that, with the spacing you have in your code. – CBroe Apr 12 '23 at 10:03
  • Btw., you should really properly URL-encode those values you are inserting into the API URL. – CBroe Apr 12 '23 at 10:05

1 Answers1

0

The backticks (`) are part of the parse_mode markdown, so it won't work with HTML.


Your first example:

const text2 = `
    word1: ${a}
    word2: ${b}
    word3: ${c}
`

Will include those whitespaces from the beginning, since thats the idea behind the code blocks.

You could append it to your string to prevent that:

let text2 = '';
text2 += `word1: ${a}`
...

Or start the literal on col 0:

const text2 = `
word1: ${a}
word2: ${b}
word3: ${c}
`

I'd recommend using 3 backticks with parse-Mode markdown for better styling
How to format bot 'send_message' output so it aligns like a table?

let text = '```';
text += `word1: ${a}`
text += `word2: ${b}`
text += '```';
0stone0
  • 34,288
  • 4
  • 39
  • 64