Sending Direct Message to a user

419 Views Asked by At

Right now, my bot listens to a channel in slack called 'teams' and replies in that channel. I want it to reply to the user (say 'User1') via direct message instead. How can I construct a message to do that?

Thanks!

1

There are 1 best solutions below

1
On

You can send a Direct Message as follows:

    var response = await activityContext.ConnectorAPI.Conversations.CreateDirectConversationAsync(activity.Recipient, activity.From);

    var reply = activity.CreateReply($"This is a direct message to {activity.From.Name ?? activity.From.Id} : {activity.Text}");
    reply.Conversation = new ConversationAccount(id: response.Id);
    reply.ReplyToId = null;

    await activityContext.ConnectorAPI.Conversations.SendToConversationAsync(reply);