Hubot-slack: Ability to respond by creating a new thread

Created on 22 Nov 2017  路  14Comments  路  Source: slackapi/hubot-slack

Description

There doesn't seem to be a way to create a new thread with the response. #398 added threaded responses, but it seems thread_ts is only populated when the message is already in a thread.

I'm not sure if this is a documentation issue or actually feature request.

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

  • [ ] bug
  • [x] enhancement (feature request)
  • [ ] question
  • [x] 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.
docs

Most helpful comment

have we tried the following?

res.message.thread_ts = res.message.id;
res.message.reply_broadcast = true;
res.send(/*...*/);

it might just work.

All 14 comments

First of all, thank you for opening this ticket @CashWilliams!

I'm no longer working with the team that needed this but I can try to clarify the context.
As I am not working with that team I won't need the assistance anymore but for the sake of potentially helping some future person who is attempting the same thing, I will explain the context below:

TLDR:
I was trying to set him up so that he would respond in a thread to a user's commands

Long version:
We have a slack channel where dozens of people message our hubot to perform an action that some amount of time. As hubot waits for the steps of the command to be completed it provides updates via replies in the same channel.

His responses get jumbled up such that it's not clear who he's responding to anymore when he says something like, "request still in progress".

I was trying to set him up so that he would respond in a thread to specific commands

I couldn't wrap my head around it as it was my first attempt at extending any hubot.

Thanks again for opening this ticket. Hope the clarification above help to determine whether this ticket should be closed or resolved :-)

@CashWilliams Your incoming message will have a ts. You simply need to set the outgoing message's thread_ts to that value. It's a one-liner:

response.message.thread_ts = response.message.item.ts;

EDIT
Here are the docs: https://api.slack.com/docs/message-threading#threads_party
Relative line under the Threading the Needle header:

Attach your messages to a thread by specifying another message's ts value as your reply's thread_ts.

thanks for reporting the issue. i do think we could be more clear about this capability in the docs, so i've marked the issue with that label.

hm i always thought response.message was the incoming message...so feels hacky to set it this way. but yeah docs should add info about replying with threads

hm..not sure what is posted works.

first response.message.item seems to be undefined. i seem to see a response.message.id and a response.message.rawMessage.id that work the way you suggested.

it's a bit inconvenient to set the value the given way because it makes all response.send like things be in a thread then, be better if i could do it at the send level.

Also is there a way to set the reply_broadcast? response.message.reply_broadcast = true did not seem to work, that'd make it not so bad

Using :

res.message.thread_ts = res.message.id;
res.send(/*...*/);

worked for me 馃憤

Unfortunately I still can't get this to work.

It might be due to the way we are using Hubot as this is a _really_ old code base -

  robot.respond /[REDACTED]/i, (msg) ->
    output = []

    ...

    msg.message.thread_ts = mes.message.id;
    msg.send message.join("\n")

I'm assuming msg is the same as res in examples above, just a different variable name?

erm... social coding FTW?
Posting above I saw the typo... mes != msg. It's all working now.

I think this can be closed from my perspective, unless there is still documentation updates needed?

That's a good idea. We should definitely add threads to the docs.

@drdamour regarding reply_broadcast I've managed to get it working by doing the following:

module.exports = (robot) ->

  robot.respond /handover$/i, (msg) ->
    msg.message.thread_ts = msg.message.rawMessage.ts
    msg.send "*handover time*"
    msg.send "https://dashboard"

  robot.respond /handover ack$/i, (msg) ->
    if msg.message.thread_ts?
      # the message is inside a thread, so continue
      room = msg.message.room
      message = "handover acked by #{msg.message.user.name}"
      robot.adapter.client.web.chat.postMessage(room, message, { as_user: true, reply_broadcast: true })

This script makes hubot create a thread response to the initial hubot handover command, then if somebody says hubot handover ack within the thread (any thread in fact), it posts back to the main channel.

hm that's cool, but how does it know what thread to send to since you aren't using msg.message.thread_ts. guessing the room indicates a thread?

Ah in fact my handover ack command only posts a message back in the main channel, nothing in the thread.. (and it doesn't appear with the original message quoted like when a person checks the 'send message to channel' box). So not really a proper broadcasted reply at all. Just a command that only works when used inside a thread is what I really have...

have we tried the following?

res.message.thread_ts = res.message.id;
res.message.reply_broadcast = true;
res.send(/*...*/);

it might just work.

@aoberoi works for me! Thank for this.

Was this page helpful?
0 / 5 - 0 ratings