15

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, =/

mr-sk
  • 13,174
  • 11
  • 66
  • 101
  • 1
    Can you please post an example call with attachments that actually works? I'm trying to do the same thing and including a title isn't helping – Bicubic May 18 '16 at 13:12
  • @Bicubic It's been awhile but I added a solution that should work. – Shatnerz Nov 21 '16 at 03:09

2 Answers2

9

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)
Logovskii Dmitrii
  • 2,629
  • 4
  • 27
  • 44
Shatnerz
  • 2,353
  • 3
  • 27
  • 43
3

You will have to use the postMessage option -- you can't do it using RTM (from https://api.slack.com/rtm):

The RTM API only supports posting simple messages formatted using our default message formatting mode. It does not support attachments or other message formatting modes. To post a more complex message as a user clients can call the chat.postMessage Web API method with as_user set to true.

  • I updated my OP to show example of using `postMessage`, which doesn't appear to work for me. – mr-sk Jan 26 '16 at 19:30
  • Are you including `token` argument? It's required https://api.slack.com/methods/chat.postMessage –  Jan 26 '16 at 19:41
  • I'm using a client library, it doesn't seem to require that I supply the value. It's working fine w/out supplying token, though I supply API key on client constructor. - Also, note, I posted what appears to be the solution in my OP. Missing image title. – mr-sk Jan 26 '16 at 19:43
  • Thanks! And thanks for your comments. Helped me to keep digging. – mr-sk Jan 26 '16 at 19:49