skip to Main Content

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?

4

Answers


  1. 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

    Login or Signup to reply.
  2. 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```"
    
    Login or Signup to reply.
  3. 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.

    Login or Signup to reply.
  4. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search