Send a pandas dataframe to slack

Viewed 2155

I am trying to send a pandas dataframe on a channel on slack using the following small script:

import requests
URL = 'XXXXXX'

response = requests.post(
        URL, data={dataframe}
    )

But when I do that I have the following error message: 'DataFrame' objects are mutable, thus they cannot be hashed! I also tried to send it as a json but then it is unreadable for the users. Any idea on how to do this properly? thanks!

My dataframe looks at follow:

    Bot instance    Current Potential profit    Potential Profit 24h ago    Change last 24h Volume 24h  Volume Market
    0   Biki - TECUSDT  21.69386074 USDT    21.60458081 USDT    0.08927993 USDT 0.84554375 USDT 28577.2565559176490500
    1   Binance - XEMBTC    -3.55974813 BTC -3.55514961 BTC -0.00459852 BTC 11.31867593 BTC 924.7716585800000000
    2   Binance - XEM/ETH   -15.38320177 ETH    -15.32533185 ETH    -0.05786992 ETH 35.44241506 ETH 1273.2456803600000000
    3   bitfinex ant/btc    1.64774952 BTC  1.65615563 BTC  -0.00840611 BTC 0.01056552 BTC  5.2507294117379079
    4   Bitfinex ANT/ETH    -15.93635190 ETH    -15.60342681 ETH    -0.33292509 ETH 0.00000000 ETH  99.3832923859608600

And when I send it to slack as a json it looks like this:

{"Bot instance":{"0":"Biki - TECUSDT","1":"Binance - XEMBTC","2":"Binance - XEM/ETH","3":"bitfinex ant/btc","4":"Bitfinex ANT/ETH","5":"Bitfinex ANT/USD","6":"Bitfinex ODE/BTC","7":"Bitfinex ODE/USD","8":"Bitfinex - RIFBTC","9":"Bitfinex - RIFUSD","10":"Bitfinex - VLD/USD","11":"Bittrex ANT/BTC","12":"Bittrex ANT/ETH","13":"Bittrex - GBYTEBTC","14":"Bitvavo - ANT/EUR","15":"Coinall - RIFBTC","16":"Coindeal - PHT/ETH","17":"Coinmetro - PRQEUR","18":"Hitbtc ANTBTC","19":"Hitbtc - PLBTBTC","20":"Hitbtc - PLBTETH","21":"Hitbtc - PLR/BTC","22":"Hitbtc - PLR/ETH","23":"Hitbtc - PLR/USDT","24":"Kucoin - RIFBTC","25":"LGO - LGOUSD","26":"Liquid - RIFUSD","27":"Livecoin - PLBTBTC","28":"Livecoin - PLBTETH","29":"Livecoin - PLBTUSD","30":"Nash - ANTBTC","31":"Poloniex - PRQUSDT","32":"P2PB2B - ANTBTC"},"Current Potential profit":{"0":"21.69386074 USDT","1":"-3.55974813 BTC","2":"-15.38320177 ETH","3":"1.64774952 BTC","4":"-15.93635190 ETH","5":"-22533.4891602 USD","6":null,"7":"-163.0524

Which is not readable for users

3 Answers

You could use pandas.DataFrame.to_markdown to encode your data as string. That should be somewhat readable in Slack.

response = requests.post(
    URL,
    data={'<payload_key>': dataframe.to_markdown()}
)

to_markdown gives a string like this:

|    | index             | Bot instance     | Current Potential profit   | Potential Profit 24h ago   | Change last 24h Volume   |   24h  Volume Market |
|---:|:------------------|:-----------------|:---------------------------|:---------------------------|:-------------------------|---------------------:|
|  0 | Biki - TECUSDT    | 21.69386074 USDT | 21.60458081 USDT           | 0.08927993 USDT            | 0.84554375 USDT          |          28577.3     |
|  1 | Binance - XEMBTC  | -3.55974813 BTC  | -3.55514961 BTC            | -0.00459852 BTC            | 11.31867593 BTC          |            924.772   |
|  2 | Binance - XEM/ETH | -15.38320177 ETH | -15.32533185 ETH           | -0.05786992 ETH            | 35.44241506 ETH          |           1273.25    |
|  3 | bitfinex ant/btc  | 1.64774952 BTC   | 1.65615563 BTC             | -0.00840611 BTC            | 0.01056552 BTC           |              5.25073 |
|  4 | Bitfinex ANT/ETH  | -15.93635190 ETH | -15.60342681 ETH           | -0.33292509 ETH             | 0.00000000 ETH           |             99.3833  |

Your 'data' should be an encoded string, e.g.

message = 'string'
data = message.encode('utf-8')

After testing this should work:

for private channel: make sure your bot has access to your channel you are trying to send the message to. If they are not in it, add the bot to the channel.

import pandas
import requests
from pandas import DataFrame

from io import StringIO

csv_string = StringIO("""Bot instance,Current,Potential profit,Potential Profit 24h ago,Change last 24h Volume, 24h Volume Market
Biki - TECUSDT,21.69386074 USDT,21.60458081 USDT,0.08927993 USDT,0.84554375 USDT,28577.2565559176490500
Binance - XEMBTC, -3.55974813 BTC,-3.55514961 BTC,-0.00459852 BTC,11.31867593 BTC,924.7716585800000000
Binance - XEM/ETH,-15.38320177 ETH, -15.32533185 ETH,-0.05786992 ETH,35.44241506 ETH,1273.2456803600000000
bitfinex ant/btc, 1.64774952 BTC,1.65615563 BTC,-0.00840611 BTC,0.01056552 BTC,5.2507294117379079
Bitfinex ANT/ETH, -15.93635190 ETH,-15.60342681 ETH,-0.33292509 ETH,0.00000000 ETH,99.3832923859608600""")

if __name__ == '__main__':
    data: DataFrame = pandas.read_csv(csv_string)
    response = requests.post(
        'https://slack.com/api/chat.postMessage',
        data={
            'token': 'your-token',
            'channel': '#the-channel',
            'text': data.to_markdown() # can be what ever you want, ie: data.to_csv()
        }
    )
    print(response.json())

Edit results on slack

Related