简体   繁体   中英

Send perfectly aligned tabular text message in Google Chat Space using webhook

I have created a Google chat space. I have a script which does some calculation and the final data is in pandas Dataframe. I want to send the data in this Dataframe as Daily alerts in tabular form in the chat space. What have I tried

# sample pandas Dataframe to be sent as alert
df = 
    SYMBOL      LAST        TIMESTAMP
0   20MICRONS   102.15      19-AUG-2022
1   21STCENMGM  27.50       19-AUG-2022
2   3IINFOLTD   45.90       19-AUG-2022
3   3MINDIA     22859.80    19-AUG-2022
4   3PLAND      17.45       19-AUG-2022
5   5PAISA      281.25      19-AUG-2022
6   63MOONS     188.50      19-AUG-2022
7   A2ZINFRA    12.80       19-AUG-2022
8   AAKASH      12.80       19-AUG-2022
9   AAREYDRUGS  35.20       19-AUG-2022

Using tabulate python package (which is used to print tabular data in nicely formatted tables) to create a table string

records = df.to_dict(orient="list")
tabular_string = tabulate(records, headers="keys", tablefmt="github")
print(tabular_string)

#Output
| SYMBOL     |     LAST | TIMESTAMP   |
|------------|----------|-------------|
| 20MICRONS  |   102.15 | 19-AUG-2022 |
| 21STCENMGM |    27.5  | 19-AUG-2022 |
| 3IINFOLTD  |    45.9  | 19-AUG-2022 |
| 3MINDIA    | 22859.8  | 19-AUG-2022 |
| 3PLAND     |    17.45 | 19-AUG-2022 |
| 5PAISA     |   281.25 | 19-AUG-2022 |
| 63MOONS    |   188.5  | 19-AUG-2022 |
| A2ZINFRA   |    12.8  | 19-AUG-2022 |
| AAKASH     |    12.8  | 19-AUG-2022 |
| AAREYDRUGS |    35.2  | 19-AUG-2022 |

Creating message and posting it on webhook url

webhook_url = "https://chat.googleapis.com/..."

message = {"text": tabular_string}
headers = {'Content-Type': "application/json"}
response = requests.post(webhook_url, data=json.dumps(message), headers=headers)

In Chat Space the message is completely misaligned

在此处输入图像描述

Whereas the same message if sent to Microsoft Teams channel the table is perfectly aligned.

The issue is that Google Chat uses a proportional font by default, and for this type of line drawing you need a monospaced font .

You can't control the entire chat's font or whatever the user has selected in their browser, but Google Chat supports some markdown formatting so you can wrap your table between three backquotes (```) like we do here in StackOverflow to create a code block. This is mentioned in their documentation .

The result looks like this:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM