Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can a bot start a thread in Slack

I am trying to have my bot framework bot reply to a user by starting a thread. This way I can keep who the bot is talking to when in a channel with many people straight.

According to the slack documentation what I need to do is set the thread_ts property to the ts property sent to my bot. I have tried a few things and have been unable to accomplish this. This is the most concise example I have:

var reply = (Activity)activity;
reply = reply.CreateReply("reply");

reply.ChannelData = JObject.Parse($"{{thread_ts:'{ts}'}}");
await context.PostAsync(reply);

This is not working for me.

like image 510
botsbotsbotsbots Avatar asked Jun 28 '18 00:06

botsbotsbotsbots


People also ask

How do I start a thread chat in Slack?

Start or reply to a threadClick the Reply in thread icon. Type your reply. If you'd like to send your reply back to the channel or DM's main view, check the box below your message. Send your message.

How do I automate messages in Slack?

Schedule a messageClick the compose button or open the conversation where you'd like to send your message. Type your message in the message field. Click the arrow icon to the right of the paper plane icon. Choose a date and time from the list or select Custom time.

What can Slack bots do?

Bots can do a lot of the same things in Slack that regular members can: They have names, profiles, profile photos, and exist in the directory. They can be @mentioned and sent direct messages. They can post messages and upload files.

Why dont people use Slack threads?

Threads in Slack are useful but the implementation is pretty bad. For example you don't see new responses in a visible thread until you click the button for more messages. It makes them really frustrating to use. Slack does a lot of things really well which makes the pain of using threads even worse.


1 Answers

You will need to set the text in the ChannelData in order for your bot to reply in the thread. Right now you are setting it in your activity reply = reply.CreateReply("reply"); All you need to do is this:

reply.ChannelData = JObject.Parse($"{{text:'reply', thread_ts:'{ts}'}}");

here is a full working method from a dialog:

public async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> argument)
{
    var activity = await argument;
    var ts = activity.ChannelData?.SlackMessage?.thread_ts
             ?? activity.ChannelData?.SlackMessage?.ts
             ?? activity.ChannelData?.SlackMessage["event"].thread_ts
             ?? activity.ChannelData?.SlackMessage["event"].ts;

    var reply = (Activity)activity;
    reply = reply.CreateReply();

    reply.ChannelData = JObject.Parse($"{{text:'reply', thread_ts:'{ts}'}}");
    await context.PostAsync(reply);
}
like image 145
JJ_Wailes Avatar answered Sep 22 '22 08:09

JJ_Wailes