Advertisement

Friday, February 14, 2020

DEVOPS : Slack : How to Upload File to Slack using Python

When uploading files to slack using Webhook - it is not possible. You need to use Slack API.
Below is sample code which can be used to put files to slack.

The key thing here is the token. You need to request a new token from Slack which you can do from Below URL - 

https://api.slack.com/legacy/custom-integrations/legacy-tokens

Sample Code - 


import requests
token  = "xxxxxxx"my_file = {
  'file' : ('/tmp/temp_file.out', open('/tmp/temp_file.out', 'rb'), 'txt')
}
payload={
  "filename":"temp_file.out",
  "token":token,
  "channels":['#channel-name'],
}
r = requests.post("https://slack.com/api/files.upload", params=payload, files=my_file)



No comments:
Write comments