Node-slack-sdk: sending attachments / images?

Created on 26 Jan 2015  路  12Comments  路  Source: slackapi/node-slack-sdk

the demo examples here just send a text response, vis:

+       response = text.split('').reverse().join('');
+       channel.send(response);
+       console.log('@%s responded with "%s"', slack.self.name, response);

can this node package be used to send other types of messages, or attachments?

I've tried sending JSON like:

 if (reply) {
        // channel.send(reply.string);
        msgpack = {
          type: "message",
          text: reply.string,
          icon_url: "http://laorquesta.mx/wp-content/uploads/2014/12/bikers-300x225.jpg",
          attachment: {
            "color": "#36a64f",
            "title": "drifter bot",
            "title_link": "http://superscriptjs.com/starter/quickstart"
          }
        }
        // channel.send(JSON.stringify(msgpack))
        channel.send(msgpack)

but just get an error back:

{ code: 2, msg: 'message text is missing' }

Most helpful comment

I found out, what the problem was / is. You NEED to JSON encode the 'attachments' argument, so the 'attachments' is actually a string and not an object. Then it should work. At least for me that worked.

All 12 comments

Any argument to channel.send is treated as text.

Technically you could send binary data over the websocket, but I the library doesn't really support it and I imagine it wouldn't show up on the other end in a useful way.

so channel.sendtMessage should send attachments but i haven't been able to get that to work via the web API either. is there a working example/json anywhere? I couldn't find.

this is what i'm sending:

{ type: 'message',
  text: 'TEST MESSAGE',
  icon_url: 'http://lorempixel.com/48/48/',
  attachments: 
   [ { title: '*Title*',
       fallback: 'Title: testing *right now!*',
       text: 'Testing *right now!*',
       mrkdwn_in: [Object] } ],
  token: 'xoxp-2662813184-3515663524-3515664302-b8a82e',
  channel: 'C03F38ZCN' }
Ignoring... { id: 1,
  type: 'message',
  channel: 'C03F38ZCN',
  text: 'TEST MESSAGE' }

postMessage result: { ok: true, channel: 'C03F38ZCN', ts: '1422399323.000004' } 

and what i see:
image

is there a different type: 'message', ?

Sorry, I don't quite understand the example here. Can you post more complete (ideally runnable) code?

here's the method which sends the JSON above.

slack._apiCall  

is a method in your lib.

function testAttachment(channel) {

  var att2 = {
    "title": "*Title*",
    "fallback": "Title: testing *right now!*",
    "text": "Testing *right now!*",

    "mrkdwn_in": ["text", "title", "fallback"]
  }

  msgpack = {
    type: "message",
    text: "TEST MESSAGE",
    icon_url: "http://lorempixel.com/48/48/",
    attachments: [att2]
  }

  msgpack.token = token
  msgpack.channel = channel.id

  console.log("msgpack", msgpack);
  slack._apiCall("chat.postMessage", msgpack, function(err, res){
    console.error("postMessage result:", err, res)
  })

}

I think #15 (along with some changes to the server documented in slackhq/hubot-slack#148) will fix this? Can you confirm?

I think I'm having the same problem.

I was able to send attachments before using Incoming Webhook functionality, but can't do it with Web API.

Should

{
    "fallback": "Required plain-text summary of the attachment.",

    "color": "#36a64f",

    "pretext": "Optional text that appears above the attachment block",

    "author_name": "Bobby Tables",
    "author_link": "http://flickr.com/bobby/",
    "author_icon": "http://flickr.com/icons/bobby.jpg",

    "title": "Slack API Documentation",
    "title_link": "https://api.slack.com/",

    "text": "Optional text that appears within the attachment",

    "fields": [
        {
            "title": "Priority",
            "value": "High",
            "short": false
        }
    ],

    "image_url": "http://my-website.com/path/to/image.jpg"
}

from https://api.slack.com/docs/attachments work with Web API?

Does anyone know when this will be fixed? And https://github.com/slackhq/hubot-slack/pull/148 to be merged.

Any news on this? Seems that it still doesn't work on the Web API.

I ran into this as well. Attachments do not work when using the web api (though I'm not even using the node client, I'm making a raw curl request), switching to an incoming webhook made the attachments appear.

Something is broken at a lower level than the client. Hope it gets fixed.

I found out, what the problem was / is. You NEED to JSON encode the 'attachments' argument, so the 'attachments' is actually a string and not an object. Then it should work. At least for me that worked.

@bobalazek stringifying worked thanks. Pretty peculiar solution :)

The >= 2.0.0 release will now check this for you, so should be a little easier to use, you can see where it's doing that here

Was this page helpful?
0 / 5 - 0 ratings