Hubot-slack: Unable to send private message to user

Created on 20 Feb 2015  路  16Comments  路  Source: slackapi/hubot-slack

I'm troubleshooting a TYPE ERROR when we try to send a private message to a user. The code looks like this:

  robot.respond ///REGEX///i, (msg) ->
    user = robot.brain.userForId msg.envelope.user.id
    robot.messageRoom msg.envelope.user.id, "SUPER SECRET MESSAGE"

This results in error:

2015-02-20T14:23:29.702795+00:00 app[web.1]: [Fri Feb 20 2015 14:23:29 GMT+0000 (UTC)] ERROR TypeError: Cannot call method 'send' of undefined
2015-02-20T14:23:29.702802+00:00 app[web.1]:   at SlackBot.send (/app/node_modules/hubot-slack/src/slack.coffee:203:17)
2015-02-20T14:23:29.702804+00:00 app[web.1]:   at Robot.messageRoom (/app/node_modules/hubot/src/robot.coffee:415:5)

Any help is appreciated.

Most helpful comment

FWIW, I'm able to accomplish this using the username w/ the preceding @

robot.messageRoom('@caseywebb', 'ping')

All 16 comments

Wow, I just came here to post the exact same bug, but in my case I'm trying to do it the following way (which works locally, but not in slack)

@robot.send {user: {name: reminder.user.name}}, "test DM"

Also @robot.send {user: user}, "message" does not work either. user is the slack user object, and for example the code works fine when just sending to a room like a normal message.

To send a DM you need the DM's ID and pass it as the room property to send() or the first argument to mesageRoom(). I don't know what those look like, but you can dig into the internal Slack client (robot.adapter.client I think) to try to figure it out.

Relevant code here, here, and here.

If you respond to a message from a DM, it will automatically be sent back as a DM.

@evansolomon Thanks for the suggestion. I've injected debug statements, and the logs suggest that msg.envelope.user.id, which is the first argument to messageRoom(), is of the form U********. I believe this is the right form.

If you're trying to send a DM to any arbitrary user, even users that haven't started a DM yet, this will depend on https://github.com/slackhq/node-slack-client/pull/30 from the slack client. Hubot will need to open a DM channel, wait for the callback, then send the DM. I'll poke at this some.

I believe this was fixed in #168? @jtatum, can you confirm?

Doesn't look that way. We deployed hubot-slack from @1156fd60baa63de63e3c44aa1253c85e4b16b2b1, but the issue persists.

2015-04-10T01:14:51.234318+00:00 app[web.1]: [Fri Apr 10 2015 01:14:51 GMT+0000 (UTC)] ERROR TypeError: Cannot call method 'send' of undefined
2015-04-10T01:14:51.234321+00:00 app[web.1]:   at SlackBot.send (/app/node_modules/hubot-slack/src/slack.coffee:214:17)
2015-04-10T01:14:51.234323+00:00 app[web.1]:   at Robot.messageRoom (/app/node_modules/hubot/src/robot.coffee:424:5)
2015-04-10T01:14:51.234324+00:00 app[web.1]:   at /app/node_modules/hubot-deploy/src/scripts/token.coffee:47:17
2015-04-10T01:14:51.234326+00:00 app[web.1]:   at /app/node_modules/hubot-deploy/src/token_verifier.coffee:14:11
2015-04-10T01:14:51.234328+00:00 app[web.1]:   at Client.errorHandle (/app/node_modules/hubot-deploy/node_modules/octonode/lib/octonode/client.js:164:14)
2015-04-10T01:14:51.234329+00:00 app[web.1]:   at Request._callback (/app/node_modules/hubot-deploy/node_modules/octonode/lib/octonode/client.js:178:22)
2015-04-10T01:14:51.234331+00:00 app[web.1]:   at Request.self.callback (/app/node_modules/hubot-deploy/node_modules/octonode/node_modules/request/request.js:129:22)
2015-04-10T01:14:51.234333+00:00 app[web.1]:   at Request.emit (events.js:98:17)
2015-04-10T01:14:51.234334+00:00 app[web.1]:   at Request.<anonymous> (/app/node_modules/hubot-deploy/node_modules/octonode/node_modules/request/request.js:873:14)
2015-04-10T01:14:51.234336+00:00 app[web.1]:   at Request.emit (events.js:117:20)
2015-04-10T01:14:51.234338+00:00 app[web.1]:   at IncomingMessage.<anonymous> (/app/node_modules/hubot-deploy/node_modules/octonode/node_modules/request/request.js:824:12)
2015-04-10T01:14:51.234339+00:00 app[web.1]:   at IncomingMessage.emit (events.js:117:20)
2015-04-10T01:14:51.234341+00:00 app[web.1]:   at _stream_readable.js:944:16
2015-04-10T01:14:51.234342+00:00 app[web.1]:   at process._tickCallback (node.js:448:13)
2015-04-10T01:14:51.234344+00:00 app[web.1]:

Code I got working looked like:

robot.send {room: message.envelope.user.name}, message

I see a couple of folks passing objects with various user attributes, and as far as I know that won't work. The room attribute should be a string with the user's name.

@jtatum Thanks for the hint! I've fixed it. :-D

for those fighting with this in the future (as this is the hit for "hubot slack dm" on google

robot.messageRoom "userid" "message"

Works great.

To clarify: DMs are considered rooms. With Slack, the DM room name is the user's username so either of these will work.

robot.messageRoom res.message.user.name, "Hello!"
robot.send {room: res.message.user.name}, "Hello!"

Those still fighting with this, should check out the workaround here: https://github.com/slackhq/hubot-slack/issues/332#issuecomment-238770906

The only thing that works currently to send DMs in Slack

@contolini It appears that the user.name won't work, due to https://github.com/slackhq/hubot-slack/issues/332. I had to change hubot-slack as in https://github.com/slackhq/hubot-slack/issues/332#issuecomment-238770906 to make it work.

If this is of any help, I was able to use the robot.adapter.client.openDM function to open a DM channel with the user's id, and I did all my stuff in the callback.

This was because I was using robot.emit to send attachments to any user who joins any channel that my bot is in.

robot.adapter.client.openDM res.message.user.id, (room) ->
    DM = room.channel.id

FWIW, I'm able to accomplish this using the username w/ the preceding @

robot.messageRoom('@caseywebb', 'ping')
Was this page helpful?
0 / 5 - 0 ratings