Sending the output of Prettytable to Telegram
Asked Answered
V

2

0

I have the following code:

from prettytable import PrettyTable

myTable = PrettyTable(["Student Name", "Class", "Section", "Percentage"])
  
# Add rows
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 %"])

This produces a table which 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 %   |
+--------------+-------+---------+------------+

I want to send this table exactly how it appears with no changes to the format to the telegram messages. So i write this to a text file:

table_txt = table.get_string()
with open('output.txt','w') as file:
    file.write(table_txt)

Next i use this code:

import telegram

def send_msg(text):
    token = "******************:***********"
    chat_id = "********"
    bot = telegram.Bot(token=token)
    for i in text:
        bot.sendMessage(chat_id=chat_id, text=i)
new_list = []
with open("output.txt", 'r', encoding="utf-8") as file:
     new_list = file.read()
for i in new_list:
     send_msg()

What it does is send the content of the file 1 character at a time until you get the telegram error:

RetryAfter: Flood control exceeded. Retry in 43.0 seconds

Please advise what can i do to resolve this?

Vaginismus answered 12/8, 2021 at 1:57 Comment(2)
Why do you send one character at a time? Why don't you send line by line or the entire table in one message?Greg
Thats what i am hoping to get some help onVaginismus
G
1

I'm not a python developer, But I think this will work.

import telegram

def send_msg(text):
    token = "******************:***********"
    chat_id = "********"
    bot = telegram.Bot(token=token)
        bot.sendMessage(chat_id=chat_id, text=text)

with open("output.txt", 'r', encoding="utf-8") as file:
     send_msg(file.read())
Greg answered 13/8, 2021 at 5:35 Comment(0)
B
1

The issue is this bit of code:

for i in text:
    bot.sendMessage(chat_id=chat_id, text=i)

You aren't sending the text, you are iterating over the text. By doing this, you get substrings of the entire string, each containing only one character:

text = "hello"
outputs = []
for i in text:
    outputs.append(i)
assert outputs == ['h', 'e', 'l', 'l', 'o']

for i in text:
    print(i)
# h
# e
# l
# l
# o

To fix this, you can just send the entire string instead:

bot.sendMessage(chat_id=chat_id, text=text)
Burkes answered 13/8, 2021 at 6:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.