Hubot-slack: Support for as_user=true in msg.send

Created on 21 Jul 2016  路  19Comments  路  Source: slackapi/hubot-slack

Reporting from here, it seems that

msg.send({
    attachments: [{
        title: 'a title',
        fallback: 'a fallback',
        title_link: 'http://example.com',
    }],
    username: process.env.HUBOT_SLACK_BOTNAME,
    as_user: true,
});

ignores as_user, and appears at Slack just like if it was:

msg.send({
    attachments: [{
        title: 'a title',
        fallback: 'a fallback',
        title_link: 'http://example.com',
    }],
    username: process.env.HUBOT_SLACK_BOTNAME,
});

BTW, is this a correct way to send attachments these days, via v3?

bug

Most helpful comment

Been digging all morning. This is a toughie, y'all.

All 19 comments

@DEGoodmanWilson, do you probably have any idea of where to look for this (node-slack-sdk perhaps?), or how else to ensure as_user is passed as true? Thanks in advance!

Will be investigating! And yes, that is the way to do attachments ;)

Do you know a quickfix, so that I could solve my itch right away? Like calling slack adapter directly, etc. Thanks in advance!

That's all we really do, actually. I suspect the problem is in the Slack SDK layer just below the Hubot adapter.

as_user: true should be the default, according to the slack docs. if your wanting to override the name defined in slack, it should be as_user: false. Should it not?

Here I see that as_user "Defaults to false" (not to be confused with example true value in the second column of the table).

@spacediver - Your correct... that tables is a little confusing..

msg.send <simple text> does correctly use the right user, so slack must infer the right as_user

If as_user is not provided at all, then the value is inferred, based on the scopes granted to the caller: If the caller could post with as_user passed as false, then that is how the method behaves; otherwise, the method behaves as if as_user were passed as true.

Prior to 4.0, the code explicitly added as_user=true.

if data.username && data.username != @robot.name
  msg.as_user = false
  msg.username = data.username
  if data.icon_url?
    msg.icon_url = data.icon_url
  else if data.icon_emoji?
    msg.icon_emoji = data.icon_emoji
else
  msg.as_user = true

@ArcticSnowman yes, simple text messages are no problem, these are displayed correctly. It is rich messages (with attachments) that are seemingly processed as if as_user=false or unset.

Also prior to 4.0, the hubot-slack JSON'ifed the message prior to passing to the slack-client. Not sure that matter here or not.

Thanks for the additional data y'all. The new version of node-slack-sdk doesn't require JSONifying the message, which is why that change. I'll look into the other difference that was brought up. I have to admit that I am snowed under this week, so it might be a little time before I can properly dig on this. Just wanted to let you know you're not being ignored.

@DEGoodmanWilson I going to try building the message object explicitly rather than pass it in implicitly to msg.send to see if that works.

Just wanted to reference that #329 is a duplicate that has some nice clarifying information on this issue.

@DEGoodmanWilson I tried constructing the message as a object and add the as_user, similar to the way 3.x added it in the customMessage. But that made no difference.

Hey folks, can we come up with a quick fix for that? Any working solution will suffice =) Thanks!

@spacediver The only 'quick fix' that I know of is to revert to pre-4.0.... It looks like there is something in @slack|client that is at fault.. need some way to capture the HTTP/JSON request it sends to slack to see why explicitly setting as_user=true does not work.

Been digging all morning. This is a toughie, y'all.

The fIx works, thanks a lot!

Was this page helpful?
0 / 5 - 0 ratings