Skip to content

Commit 7d1266f

Browse files
committed
Replace slacker with slack-sdk
- remove deprecated `as_user` method
1 parent 0123c8e commit 7d1266f

File tree

3 files changed

+12
-30
lines changed

3 files changed

+12
-30
lines changed

HANDBOOK.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2291,8 +2291,7 @@ joined the channel.
22912291
22922292
![Slack](assets/slack.png)
22932293
2294-
This plugin requires [Python slacker](https://github.com/os/slacker).
2295-
For image support (added November 2018), slacker 0.10.0 is required.
2294+
This plugin requires [Python slack-sdk](https://github.com/slackapi/python-slack-sdk).
22962295
22972296
The slack service will accept a payload with either a simple text message, or a json payload which contains
22982297
a `message` and either an `imageurl` or `imagebase64` encoded image.

mqttwarn/services/slack.py

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,8 @@
55
__copyright__ = 'Copyright 2014 Jan-Piet Mens'
66
__license__ = 'Eclipse Public License - v 1.0 (http://www.eclipse.org/legal/epl-v10.html)'
77

8-
9-
# 2018-11-13 - Update by psyciknz to add image upload function. See Readme
10-
# Needs slacker 0.10.0 at a minimum
11-
12-
from slacker import Slacker
13-
8+
from slack_sdk import WebClient
9+
from slack_sdk.errors import SlackApiError
1410

1511
from builtins import str
1612
import base64
@@ -28,12 +24,6 @@ def plugin(srv, item):
2824

2925
# get the target tokens
3026
addrs = list(item.addrs)
31-
as_user = False
32-
33-
# check if we have the optional as_user token (extract and remove if so)
34-
if isinstance(addrs[-1], (bool)):
35-
as_user = addrs[-1]
36-
addrs = addrs[:len(addrs) - 1]
3727

3828
# check for target level tokens (which have preference)
3929
try:
@@ -82,33 +72,26 @@ def plugin(srv, item):
8272
elif 'imagebase64' in item.data:
8373
imagebase64 = item.data['imagebase64']
8474
srv.logging.debug("Image (base64 encoded) detected")
85-
#image = base64.decodestring(imagebase64)
8675
image = base64.b64decode(str(imagebase64))
87-
filename = 'some_image.jpg'
88-
#base 64 doesn't seem to decode properly. So have to write it to a temp image, and reload it.
89-
#maybe this method might be dropped?
90-
with open(filename, 'wb') as f:
91-
f.write(image)
9276

9377
except Exception as e:
9478
srv.logging.warning("Cannot download image: %s", e)
9579

9680
try:
97-
slack = Slacker(token)
81+
slack = WebClient(token=token)
9882
if image is None:
99-
slack.chat.post_message(channel, text, as_user=as_user, username=username, icon_emoji=icon, unfurl_links=True)
83+
slack.chat_postMessage(channel=channel, text=text, username=username, icon_emoji=icon, unfurl_links=True)
10084
else:
10185

10286
srv.logging.debug("Channel id: %s" % channel);
103-
channelname = channel.replace('#','')
10487

105-
# weird check and re-read I had to do for base64 decoded images.
106-
if 'imagebase64' in item.data:
107-
with open(filename,'rb') as f2:
108-
slack.files.upload(file_=f2,title=text,channels=slack.channels.get_channel_id(channelname))
109-
else:
110-
slack.files.upload(file_=image,title=text,channels=slack.channels.get_channel_id(channelname))
88+
slack.files_upload(file=image,title=text,channels=channel)
11189
srv.logging.debug("image posted")
90+
except SlackApiError as e:
91+
assert e.response["ok"] is False
92+
assert e.response["error"]
93+
srv.logging.warning("Cannot post to slack %s: %s" % (channel, e.response['error']))
94+
return False
11295
except Exception as e:
11396
srv.logging.warning("Cannot post to slack %s: %s" % (channel, e))
11497
return False

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
'pyserial>=3.4',
8282
],
8383
'slack': [
84-
'slacker>=0.9.65',
84+
'slack-sdk>=3.1.0',
8585
],
8686
'ssh': [
8787
'paramiko>=2.4.1',

0 commit comments

Comments
 (0)