Last active
June 15, 2023 21:18
-
-
Save etigui/b047b77abecbc798f384b1259c45aa8e to your computer and use it in GitHub Desktop.
Python - send file with Slack API using post request
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Send file with Slack API using post request | |
# Under "Add an OAuth scope" you need to add "files:write:user" permission | |
# https://api.slack.com/methods/files.upload#arg_title | |
# https://api.slack.com/types/file#file_types | |
def get_file_content(file_name): | |
content = '' | |
with open(file_name, 'rb') as f: | |
content = f.read() | |
return content | |
# Post file with slack | |
def send_file(): | |
content = get_file_content('text.txt') | |
url = "https://slack.com/api/files.upload" | |
headers = {"Content-Type": "application/x-www-form-urlencoded"} | |
data = { | |
'token': 'xoxb-xxxx-xxxx-xxxx', | |
'channels': 'xxxxxx', | |
'content': content, | |
'filename': 'text.txt', | |
'filetype': 'text', | |
'title': 'text.txt', | |
} | |
res = requests.post(url=url, data=data, headers=headers) | |
if res.status_code == 200: | |
print(f'Response: {res.json()}') | |
else: | |
print(f'Fail to send: {res.json()}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment