Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to send private message to user #159

Closed
BanzaiMan opened this issue Feb 20, 2015 · 16 comments
Closed

Unable to send private message to user #159

BanzaiMan opened this issue Feb 20, 2015 · 16 comments

Comments

@BanzaiMan
Copy link

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.

@estiens
Copy link

estiens commented Feb 20, 2015

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"

@estiens
Copy link

estiens commented Feb 20, 2015

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.

@evansolomon
Copy link
Contributor

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.

@BanzaiMan
Copy link
Author

@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.

@jtatum
Copy link
Contributor

jtatum commented Mar 18, 2015

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 slackapi/node-slack-sdk#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.

@paulhammond
Copy link
Contributor

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

@BanzaiMan
Copy link
Author

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

@BanzaiMan
Copy link
Author

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]:

@jtatum
Copy link
Contributor

jtatum commented Apr 13, 2015

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.

BanzaiMan added a commit to travis-ci/hubot-deploy that referenced this issue Apr 14, 2015
@BanzaiMan
Copy link
Author

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

@sylvesterjakubowski
Copy link

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.

@contolini
Copy link

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!"

@francof
Copy link

francof commented Sep 5, 2016

Those still fighting with this, should check out the workaround here: #332 (comment)

The only thing that works currently to send DMs in Slack

@iancward
Copy link

iancward commented Sep 9, 2016

@contolini It appears that the user.name won't work, due to #332. I had to change hubot-slack as in #332 (comment) to make it work.

@ladisays
Copy link

ladisays commented Jan 10, 2017

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

@caseyWebb
Copy link

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

12 participants