Search code examples
pythonpython-2.7python-requestsslackslack-api

Not able to upload a file to slack via web-api


I am trying to upload a file snippet to a channel in slack using Web-API available at https://api.slack.com/methods/files.upload

 payload_channel_caller = {'token': 'xoxpxxxxb1d8529c', 'channel':'C1PJ17FFT', 'file': "/home/nsingh/slack_shift/user_list" ,'title':'Shifters'}

    print "This is a test"
    requests.post('https://slack.com/api/files.upload', data=payload_channel_caller)

But the above code is not able to upload the file, infact it runs right through with no error .

Not sure whats wrong here.

Can someone help me out


Solution

  • Modification points :

    In your script, the file you want to upload is not read. When your script is run, following response is retrieved.

    {"ok":false,"error":"no_file_data"}
    

    At this time, the status code from Slack is 200. So no error occurs. The script which was reflected above points is as follows.

    Modified script :

    import requests
    uploadfile = "/home/nsingh/slack_shift/user_list"  # Please input the filename with path that you want to upload.
    with open(uploadfile, 'rb') as f:
        param = {
            'token': 'xoxpxxxxb1d8529c',
            'channels': 'C1PJ17FFT',
            'title': 'Shifters'
        }
        r = requests.post(
            "https://slack.com/api/files.upload",
            params=param,
            files={'file': f}
        )
        print r.text
    

    If I misunderstand your question, I'm sorry.