Skip to content

Commit 62a2fc1

Browse files
authored
Merge pull request #85 from skiedude/master
Migrate files_upload.py to use slack_sdk
2 parents 329803e + 2aef8a7 commit 62a2fc1

4 files changed

Lines changed: 29 additions & 12 deletions

File tree

CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Change Log
22

3+
# 2.3.0
4+
5+
* migrate files_upload.py to use the slack_sdk helper method files_upload_v2. API endpoint files.upload is deprecated and will stop functioning on March 11, 2025.
6+
37
# 2.2.1
48

59
* fix "__all__" reference for `post_attachment`

actions/files_upload.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
from run import SlackAction
1+
from st2common.runners.base_action import Action
2+
from slack_sdk import WebClient
3+
from slack_sdk.errors import SlackApiError
24

35

4-
class FilesUploadAction(SlackAction):
6+
class FilesUploadAction(Action):
57

68
def run(self, **kwargs):
7-
# https://requests.readthedocs.io/en/master/user/quickstart/#post-a-multipart-encoded-file
8-
files = None
9+
token = kwargs.get('token') if kwargs.get('token') else self.config['action_token']
10+
client = WebClient(token=token)
911
if 'file_path' in kwargs and kwargs['file_path']:
1012
if 'file' in kwargs and kwargs['file']:
1113
raise RuntimeError('Passing in "file" and "file_path" at the same time is'
@@ -14,10 +16,20 @@ def run(self, **kwargs):
1416
' then use the "file_path" parameter. Otherwise, if you'
1517
' would like to read the file yourself and pass in the data'
1618
' then use the "file" parameter set to the content.')
17-
# the name 'file' is hard coded because that's the name of the parameter
18-
# that the Slack API is expecting
19-
files = {'file': open(kwargs['file_path'], 'rb')}
20-
kwargs.pop('file', None)
21-
kwargs.pop('file_path')
22-
23-
super(FilesUploadAction, self).run(files=files, **kwargs)
19+
try:
20+
file = kwargs['file'] if kwargs['file'] else kwargs['file_path']
21+
response = client.files_upload_v2(
22+
filename=kwargs['filename'],
23+
file=file,
24+
content=kwargs['content'],
25+
title=kwargs['title'],
26+
channel=kwargs['channels'],
27+
initial_comment=kwargs['initial_comment'],
28+
thread_ts=kwargs['thread_ts'],
29+
)
30+
assert response['file']
31+
return response.get('file', {}).get('name')
32+
except SlackApiError as e:
33+
assert e.response["ok"] is False
34+
assert e.response["error"]
35+
return False, e.response['error']

pack.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ keywords:
77
- chat
88
- messaging
99
- instant messaging
10-
version: 2.2.1
10+
version: 2.3.0
1111
python_versions:
1212
- "3"
1313
author : StackStorm, Inc.

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ beautifulsoup4==4.6.0
44
lxml==4.6.5
55
jinja2>=2.10.1
66
slackclient==1.3.1
7+
slack_sdk==3.34.0

0 commit comments

Comments
 (0)