Hubot-slack: Can Hubot respond with DM when entering a channel

Created on 22 Jan 2021  路  2Comments  路  Source: slackapi/hubot-slack

Description

Is it possible to have hubot to send a DM or reply in a thread to a person joining a channel?

I know i can do the below actions but non of them fulfils my requirements:

  • send response to the entire channel
  • send DM to the person if Hubot have had a DM session with the user prior to entering the channel.

i have tried e.g.:

  • Hubot to reply to the entire channel
    module.exports = (robot) ->
    robot.enter (res) ->
    str = "Hello and welcome"
    res.send str
  • reply to person which Hubot already have had a DM conversation with:
    module.exports = (robot) ->
    robot.enter (res) ->
    room = robot.adapter.client.rtm.dataStore.getDMByName res.message.user.name
    robot.messageRoom room.id, "Hello and welcome"

also tried e.g. res.message.thread_ts but that just throws an error.

thanks

What type of issue is this? (place an x in one of the [ ])

  • [ ] bug
  • [ ] enhancement (feature request)
  • [ x ] question
  • [ ] documentation related
  • [ ] testing related
  • [ ] discussion

Requirements (place an x in each of the [ ])

  • [x ] I've read and understood the Contributing guidelines and have done my best effort to follow them.
  • [x ] I've read and agree to the Code of Conduct.
  • [x ] I've searched for any related issues and avoided creating a duplicate issue.

Reproducible in:

hubot-slack version:

node version:

OS version(s):
CentOS Linux release 7.9.2009 (Core)

question

Most helpful comment

Hey @mwbrooks
That works like a charm.馃憢馃憢馃憢馃憢
Tnx a lot

All 2 comments

Hey @magander3 馃憢

You can send a DM to the user who joined the channel using postMessage and the user ID as the channel. The message will arrive in the Slackbot DM and come from your bot:

# Send a dm when a user enters a channel
robot.enter (res) ->
  robot.adapter.client.web.chat.postMessage(
    res.message.user.id,
    'Howdy'
  );

You can send a message as a thread reply using the follow code, but I'm not sure that it'll work for your situation. What would the main message be that's replied to? Unfortunately, you cannot thread reply to a user has joined #channel system message. :(

# Reply in a thread when a user enters a channel
# NOTE: does not work because thread replies not supported for system messages
robot.enter (res) ->
  robot.adapter.client.web.chat.postMessage(
    res.message.user.room,
    'Howdy',
    { thread_ts: res.message.ts }
  );

Hope that helps and let me know how it goes!

Hey @mwbrooks
That works like a charm.馃憢馃憢馃憢馃憢
Tnx a lot

Was this page helpful?
0 / 5 - 0 ratings