How to send message to telegram as ```code````
Asked Answered
E

5

23

Sending the output of Prettytable to Telegram

This question is a followup to an earlier question. The code which i have is this:

import telegram
from prettytable import PrettyTable

def send_msg(text):
    token = "*******:**************"
    chat_id = "***********"
    bot = telegram.Bot(token=token)
    
    bot.sendMessage(chat_id=chat_id, text=text)
        
myTable = PrettyTable(["Student Name", "Class", "Section", "Percentage"])
  
myTable.add_row(["Leanord", "X", "B", "91.2 %"])
myTable.add_row(["Penny", "X", "C", "63.5 %"])
myTable.add_row(["Howard", "X", "A", "90.23 %"])
myTable.add_row(["Bernadette", "X", "D", "92.7 %"])
myTable.add_row(["Sheldon", "X", "A", "98.2 %"])
myTable.add_row(["Raj", "X", "B", "88.1 %"])
myTable.add_row(["Amy", "X", "B", "95.0 %"])
table_txt = myTable.get_string()
with open('output.txt','w') as file:
    file.write(table_txt)
new_list = []
with open("output.txt", 'r', encoding="utf-8") as file:
     send_msg(file.read())

The problem is that the message which is sent looks like this:

+--------------+-------+---------+------------+
| Student Name | Class | Section | Percentage |
+--------------+-------+---------+------------+
|   Leanord    |   X   |    B    |   91.2 %   |
|    Penny     |   X   |    C    |   63.5 %   |
|    Howard    |   X   |    A    |  90.23 %   |
|  Bernadette  |   X   |    D    |   92.7 %   |
|   Sheldon    |   X   |    A    |   98.2 %   |
|     Raj      |   X   |    B    |   88.1 %   |
|     Amy      |   X   |    B    |   95.0 %   |
+--------------+-------+---------+------------+

But when the message received in telegram looks like this:

enter image description here

How can i fix this? Telegram lets you send messages in code whereby i guess it would preserve the format. How can i send this message in format?

Ensepulcher answered 13/8, 2021 at 7:31 Comment(0)
L
40

You have already solved it yourself: you used three backticks in the title of your question. In Markdown (including here on SO), you can put three backticks around a block of code, and that makes it use the code block formatting.

```
this is inside a code block
```

You can just add a line containing the three backticks in front and back of your text:

backticked_text = "```\n" + text + "\n```"
Links answered 13/8, 2021 at 7:40 Comment(3)
your solution worked but i guess prettytable just doesn't hold the form in telegramEnsepulcher
This works, but only on desktop: i.sstatic.net/DVbTo.png -- on mobile, it does this: i.sstatic.net/8LzLP.jpgLinks
make sure to remove formatting of your code text. for example, if your code currently has a non default, font it won't work.Brevity
C
5

Trying to send formatted tables via Telegram is a lost cause. Even when sending the table in monospace, you can't control the line breaks. As already observed by Danya02, the line breaks depend on the client, the device (i.e. the display width), the font, font size etc. If you want to make sure that the user sees a nicely formatted table, send it as PDF or image.

Cleric answered 13/8, 2021 at 8:39 Comment(0)
C
4

You also can specify code language like so:

```typescript
class MathExtension {
  public isBiggerThanZero(int: number): boolean {
    return arg > 0;
  }
}
```

and it will highlight syntax.

Cerebrate answered 27/4, 2024 at 14:20 Comment(0)
T
3

One should paste a code inside ``` block without formatting. If you use a Telegram messenger, paste your code using Cmd + Shift + V to remove a formatting.

Tasia answered 22/1, 2023 at 16:22 Comment(1)
Thanks. The "non-formatted" is very important. I wondered why it sometimes worked and sometimes notShears
E
0

Telegram supports markdown (atleast for code). You can add ``` at the start and end of your text (first and last line), and it should work out

Evolve answered 13/8, 2021 at 7:37 Comment(2)
No, it does not workTrilbi
markdown does work if you use "parse_mode": "MarkdownV2"Her

© 2022 - 2025 — McMap. All rights reserved.