Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format bot 'send_message' output so it aligns like a table?

I'm trying to create a simple Telegram Bot using the python-telegram-bot library, but i can't align a dictionary with the default "{0:<20} {1}".format(key, value) idea.

Let me give u an example:

Mapy = {
    "one": "1",
    "two": "2",
    "three": "3",
    "four": "4",
    "five": "5",
    "six": "6",
    "seven": "7",
    "eight": "8"
}

tmpstring = ""

for key, value in Mapy.items():
    tmpstring = tmpstring + "{0:<20} {1}".format(key, value) + "\n"

print(tmpstring)
context.bot.send_message(chat_id=update.message.chat_id, text=tmpstring)

the printed finish looks like this:

one                  1
two                  2
three                3
four                 4
five                 5
six                  6
seven                7
eight                8

as expected nicely aligned, but the message in Telegram looks like this:

enter image description here

So my question is: How can i align the chat message so it looks like the printed output?

like image 943
Lyux Avatar asked Dec 06 '22 09:12

Lyux


1 Answers

Use Markdowns (V2) 'pre-formatted fixed-width code block' to preserve the alignment. [Docs]

Wrap the wonderfully aligned table in a code-block using 3 back-ticks (```)

```
one          1
two          2
three        3
```

Send your message with the parse_mode option set to: MarkDown (V2)

like image 174
0stone0 Avatar answered Dec 28 '22 12:12

0stone0