Haacked
Haacked

Reputation: 59001

Get the name of the sender of the message in slack using Microsoft Bot Framewok and C#

I've set up a Microsoft Bot Framework Instance running in asp.net core. I've connected to Slack using Microsoft's Bot Channels Registration.

In my bot code, I have something like:

protected override async Task OnMessageActivityAsync(
            ITurnContext<IMessageActivity> turnContext,
            CancellationToken cancellationToken)
{
  var message = $"From: {turnContext.Activity.From.Name})";
  await turnContext.SendActivityAsync(message, cancellationToken: cancellationToken);
}

That always returns the "From: BotName". I expected that From.Name would be the name of the person sending the message that the bot is responding to.

Is there a way to get the sender's username? I can see the correct slack user id in turnContext.Activity.ChannelData, but I thought I'd be able to get at it through the normal bot framework API.

Upvotes: 1

Views: 726

Answers (2)

Haacked
Haacked

Reputation: 59001

It turned out that my code was correct, but I had Slack permissions incorrectly configured. It's been a while but I believe the necessary permissions were users.profile:read and user:read (maybe just one of these is required, but my bot uses both).

If you do not have these permissions granted to the bot in Slack, then Bot Framework appears to fail to get the sender's name and just uses the bot name for turnContext.Activity.From.Name.

This behavior is really confusing. I'd rather it be an empty string or say "(unknown)".

Upvotes: 0

Dana V
Dana V

Reputation: 1347

Please see this question/answer here. Essentially, one option is to use the slack API token that comes through with channeldata to make additional calls back into slack to retrieve what you need. OP also found that they could use the session object in node, but I am unable to accomplish that route.

Upvotes: 0

Related Questions