25

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?

5 Answers 5

41

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```"
3
  • your solution worked but i guess prettytable just doesn't hold the form in telegram Commented Aug 13, 2021 at 8:23
  • 3
    This works, but only on desktop: i.sstatic.net/DVbTo.png -- on mobile, it does this: i.sstatic.net/8LzLP.jpg
    – Danya02
    Commented Aug 13, 2021 at 8:31
  • make sure to remove formatting of your code text. for example, if your code currently has a non default, font it won't work. Commented Feb 15 at 16:18
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.

5

You also can specify code language like so:

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

and it will highlight syntax.

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.

1
  • Thanks. The "non-formatted" is very important. I wondered why it sometimes worked and sometimes not Commented Jul 23, 2023 at 22:39
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

2
  • 1
    No, it does not work Commented Jul 30, 2022 at 22:04
  • markdown does work if you use "parse_mode": "MarkdownV2" Commented Mar 12, 2023 at 16:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.