pythonpandasslackdataformat

Python Pandas and Slack Webhooks Requests.post


What im looking to achieve is pulling a csv file from a workorder app that we use. Then convert it using pandas and remove unnecessary columns. Then post this info into slack using a webhook. I dont have access to the slack API. So far this is what i came up with but am finding it hard to get the data into a format that i can send.

from time import sleep
from requests.models import Response
from selenium import webdriver
import os
import pandas as pd

import json
import requests


def WOPULLER_CSV():
  options = webdriver.ChromeOptions() 
  prefs = {'download.default_directory' : 'C:\\WO_CSV'}
  options.add_experimental_option('prefs', prefs)
  driver = webdriver.Chrome(options=options)
  driver.get('https://portal.ez.na.rme.logistics.a2z.com/work-orders?preset=allCSS&customPreset=allCSS&scheduledStartDate=nextOneWeek&status=RS,S')
  SignInASButton = driver.execute_script("return document.querySelector('ez-rme-app').shadowRoot.querySelector('ez-login-page').shadowRoot.querySelector('ez-login').shadowRoot.querySelector('mwc-button:nth-child(4)').shadowRoot.querySelector('#button')")
  SignInASButton.click()
  sleep(1)
  SingleSignOnButton = driver.execute_script("return document.querySelector('ez-rme-app').shadowRoot.querySelector('ez-login-page').shadowRoot.querySelector('ez-login').shadowRoot.querySelector('#sso-login').shadowRoot.querySelector('#button')")
  SingleSignOnButton.click()
  sleep(8)
  if os.path.exists("C:\\WO_CSV\\WorkOrderExport.csv"):
    os.remove("C:\\WO_CSV\\WorkOrderExport.csv")
  CSVButton = driver.execute_script("return document.querySelector('body > ez-rme-app').shadowRoot.querySelector('#content > main > ez-work-order-list-page').shadowRoot.querySelector('div > mwc-button:nth-child(1)').shadowRoot.querySelector('#button')")
  CSVButton.click()
  sleep(1)

def WEBHOOK():

  #value = pd.read_json('C:\WO_CSV\WorkOrderExport.json')

  value = wo
  web_hook_link = 'WEBHOOK LINK'
  headers = {
    'Content-Type': 'application/json',
          }
  data = json.dumps({"name": value})
  response = requests.post(web_hook_link, headers=headers, data=data)
  print (response)

#WOPULLER_CSV() 

df = pd.read_csv (r'C:\WO_CSV\WorkOrderExport.csv')
df = df.sort_values(by='Scheduled Start Date', ascending=False)
df = df.drop(columns=['Organization', 'Original PM due date', 'PM Compliance Max Date', 'PM Compliance Min Date', 'Scheduled End Date', 'Priority', 'Equipment Criticality', 'Equipment Alias', 'Type', 'Index', 'Equipment Description' ])

if os.path.exists("C:\WO_CSV\WorkOrderExport.json"):
    os.remove("C:\WO_CSV\WorkOrderExport.json")

df.to_json('C:\WO_CSV\WorkOrderExport.json')
wo = pd.read_json (r'C:\WO_CSV\WorkOrderExport.json')

WEBHOOK()
print (wo)

Information that is in the CSV file : CSV After Colum Drop

What im trying to achieve in slack is similar to if i just copy and pasted from excel xlsx file. There is no MD formatting in slack so this is the best option. Format in slack


Solution

  • OK i found my answer . In order to post to slack you need to tabulate the csv file then use the json.dump. Referenced these other stack posts :

    what-are-some-ways-to-post-python-pandas-dataframes-to-slack

    pretty-printing-a-pandas-dataframe

    def WEBHOOK():
        
          #value = pd.read_json('C:\WO_CSV\WorkOrderExport.json')
        
          value = tab
          web_hook_link = 'webhook link'
          headers = {
            'Content-Type': 'application/json',
                  }
          data = json.dumps({"name": value})
          response = requests.post(web_hook_link, headers=headers, data=data)
          print (response)
        
        #WOPULLER_CSV() 
        
        df = pd.read_csv (r'C:\WO_CSV\WorkOrderExport.csv')
        df = df.sort_values(by='Scheduled Start Date', ascending=False)
        df = df.drop(columns=['Organization', 'Original PM due date', 'PM Compliance Max Date', 'PM Compliance Min Date', 'Scheduled End Date', 'Priority', 'Equipment Criticality', 'Equipment Alias', 'Type', 'Index', 'Equipment Description' ])
        
        **tab = (tabulate(df, tablefmt="grid"))**
        
        WEBHOOK()
        print (tab)