pyhton code to send file to slack code example

Example: pyhton code to send file to slack

def send_file_to_slack(file_path):
	url = "https://slack.com/api/files.upload"

    # this is where you add your query string. Please chage token value
    querystring = {"token": xxxxxxx}

    # this is where you define who do you want to send it to. Change channels to your target one
    payload = {
        "channels": xxxxxxxx,  # slack channel id 
        "initial_comment": comment above the file should be here"
    }

    file_upload = {
        "file": (file_path, open(file_path, 'rb'), 'csv')
    }

    response = requests.post(url, data=payload, params=querystring, files=file_upload)

    if response.ok:
        return {
            "message": "Successfully uploaded the file to slack"
        }

Tags:

Misc Example