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
1511from builtins import str
1612import 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
0 commit comments