Posting an image with slack API

mr-sk picture mr-sk · Jan 26, 2016 · Viewed 13.1k times · Source

I'm attempting to post an image w/the slack API. I have two APIs open (using python) currently, which is rtm & slackbot.

sc = SlackClient(API_KEY)
sc.api_call('chat.postMessages', channel=, text=) #post as bot

sc.rtm_connect():
sc.rtm_send_message(channel=..., message=...) #post as desired user

These examples work fine to post text to the channel, but I need to post an image to the channel.

I want to post an image using the rtm_* method, but I can't figure out how. The only reference I see for images is https://api.slack.com/docs/attachments but I don't see a way to do it w/the rtm API.

Any help is appreciated. TIA.

UPDATE/EDIT:

So I tried using the method Jon recommended, which is this:

sc.api_call("chat.postMessage", channel='D0K7P9MCJ', text='postMessage test',
            attachments='[{"image_url":"http://i.ytimg.com/vi/tntOCGkgt98/maxresdefault.jpg"}]')

But that doesn't appear to work (no image is posted) and no error is thrown.

SOLUTION

I got this working by including a title in the attachments section. Wish it threw an error, =/

Answer

Shatnerz picture Shatnerz · Nov 21, 2016

As mentioned in an update in the question, in order for this to work one must include a title with the attachment.

image_url = "http://i.ytimg.com/vi/tntOCGkgt98/maxresdefault.jpg"
attachments = [{"title": "Cat", "image_url": image_url}]
sc.api_call("chat.postMessage", channel='D0K7P9MCJ', text='postMessage test',
            attachments=attachments)