Skip to content

Commit fc21a03

Browse files
authored
Merge pull request #100 from greut/nested
Add support for nested structure, e.g. attachments
2 parents 4fc299f + 5ab1954 commit fc21a03

6 files changed

Lines changed: 35 additions & 5 deletions

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ print sc.api_call("api.test")
4343
print sc.api_call("channels.info", channel="1234567890")
4444
print sc.api_call(
4545
"chat.postMessage", channel="#general", text="Hello from Python! :tada:",
46-
username='pybot', icon_emoji=':robot_face:'
46+
username='pybot', icon_emoji=':robot_face:',
47+
attachments=[{'title': 'This is an attachment', 'color': 'good'}]
4748
)
4849
```
4950

requirements-dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ ipdb==0.9.3
33
ipython==4.1.2
44
pdbpp==0.8.3
55
pytest>=2.8.2
6+
pytest-mock>=1.1
67
pytest-cov==2.2.1
78
pytest-pythonpath>=0.3
89
testfixtures==4.9.1

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
future==0.15.2
2+
six==1.10.0
23
websocket-client==0.35.0

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@
1111
install_requires=[
1212
'websocket-client',
1313
'requests',
14+
'six',
1415
],
1516
zip_safe=False)

slackclient/_slackrequest.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
1+
import json
2+
13
import requests
4+
import six
25

36

47
class SlackRequest(object):
58

69
@staticmethod
710
def do(token, request="?", post_data=None, domain="slack.com"):
811
post_data = post_data or {}
9-
return requests.post(
10-
'https://{0}/api/{1}'.format(domain, request),
11-
data=dict(post_data, token=token),
12-
)
12+
13+
for k, v in six.iteritems(post_data):
14+
if not isinstance(v, six.string_types):
15+
post_data[k] = json.dumps(v)
16+
17+
url = 'https://{0}/api/{1}'.format(domain, request)
18+
post_data['token'] = token
19+
files = {'file': post_data.pop('file')} if 'file' in post_data else None
20+
21+
return requests.post(url, data=post_data, files=files)

tests/test_slackrequest.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from slackclient._slackrequest import SlackRequest
2+
import json
3+
4+
5+
def test_post_attachements(mocker):
6+
requests = mocker.patch('slackclient._slackrequest.requests')
7+
8+
SlackRequest.do('xoxb-123',
9+
'chat.postMessage',
10+
{'attachments': [{'title': 'hello'}]})
11+
12+
assert requests.post.call_count == 1
13+
args, kwargs = requests.post.call_args
14+
assert 'https://slack.com/api/chat.postMessage' == args[0]
15+
assert {'attachments': json.dumps([{'title': 'hello'}]),
16+
'token': 'xoxb-123'} == kwargs['data']
17+
assert None == kwargs['files']

0 commit comments

Comments
 (0)