Node-slack-sdk: Can't send a thread_ts using RTM sendMessage

Created on 20 Jan 2017  路  9Comments  路  Source: slackapi/node-slack-sdk

  • [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.

Hey with the recent threading feature, the docs say you should send the thread_ts through the RTM however you do not allow sending any options through your sendMessage function.

RTMClient.prototype.sendMessage = function sendMessage(text, channelId, optCb) {
  return this.send({
    text: text,
    channel: channelId,
    type: RTM_API_EVENTS.MESSAGE
  }, optCb);
};

I will use the API in the meantime to respond to thread_ts messages however allowing us to send a thread_ts through this function would be ideal.

Most helpful comment

@bastienrobert I'm glad you asked! I overlooked the fact that the API of RTMClient has changed quite a bit since the messages above, specifically the v3.x to v4.x transition. That guidance is no longer valid, so allow me to provide a working example:

// Before: sending a simple text message
rtm
  .sendMessage('Poke', conversationId)
  .then(res => console.log(`Message sent: ${res}`)
  .catch(console.error);

// After: sending a simple text message in a specific thread
rtm
  .addOutgoingEvent(true, 'message', { text: 'Poke', channel: conversationId, thread_ts: parent_ts })
  .then(res => console.log(`Message sent: ${res}`)
  .catch(console.error);

In the above example, parent_ts is the ts of the root message of the thread.

All 9 comments

Hello @Js41637!

You are correct that sendMessage does not support additional options. sendMessage is intended to be a dead simple wrapper for the most common use case of send.

But there is still hope! You can use send to reply to a thread via the RTM. Porting your current code over to something like this should take care of business:

var RTM_EVENTS = require('@slack/client').RTM_EVENTS;

client.send({
    text:      text,
    channel:   channelId,
    thread_ts: thread_ts,
    type:      RTM_EVENTS.MESSAGE,
});

Give that a try and let us know if you run in to any more issues!

I can't get subtype: 'reply_broadcast' to work 馃槥

@pakastin that's because 'reply_broadcast' isn't a subtype. its meant to be a property itself with a boolean value. but tbh, i'm not sure that works with the RTM API, but it def is an option with the Web API.

Hello @jasonTheNorris can't we send direct message through Client.send api. I tried by giving DM channel Id in the place of channel Id but it did't work. can you please suggest me with suitable api's.

Note: I want to send direct message to user by his ID not by name

Thanks in advance :)

Any update about replying to threads from RTM api?

@bastienrobert: I believe the solution in the comment above works. Have you tried it? Was there an error or some unexpected outcome?

@aoberoi: I was sending messages using @slack/client like:

  .sendMessage(('Poke', conversationId))
  .then(res => {
    console.log('Message sent: ', res)
  })
  .catch(console.error)

But in this example you're editing the sendMessage method in the RTMClient instance, didn't you?

So it should look like:

RTMClient.prototype.sendMessage = function sendMessage(text, channelId, thread_ts, optCb) {
  return this.send({
    text: text,
    channel: channelId,
    thread_ts: thread_ts,
    type: RTM_EVENTS.MESSAGE
  }, optCb)
}

Not sure I'm understanding all of this very well 馃槙

@bastienrobert I'm glad you asked! I overlooked the fact that the API of RTMClient has changed quite a bit since the messages above, specifically the v3.x to v4.x transition. That guidance is no longer valid, so allow me to provide a working example:

// Before: sending a simple text message
rtm
  .sendMessage('Poke', conversationId)
  .then(res => console.log(`Message sent: ${res}`)
  .catch(console.error);

// After: sending a simple text message in a specific thread
rtm
  .addOutgoingEvent(true, 'message', { text: 'Poke', channel: conversationId, thread_ts: parent_ts })
  .then(res => console.log(`Message sent: ${res}`)
  .catch(console.error);

In the above example, parent_ts is the ts of the root message of the thread.

For future reference, you can find a complete discussion about this change in v4 in the migration guide. Its a good reference when thinking about older comments in issues.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bobrik picture bobrik  路  25Comments

kurisubrooks picture kurisubrooks  路  36Comments

thepont picture thepont  路  17Comments

danielravina picture danielravina  路  16Comments

amkoehler picture amkoehler  路  13Comments