Search code examples
c#azurebotframeworkslackslack-api

How can my Slack-using BotFramework bot ignore messages on all channels except direct?


I know this seems like it's been asked before - but I understand that you can figure out that someone's talking on a Slack channel by inspecting activity.Conversation.IsGroup - I'm using this successfully.

This is fine as long as the channel has more than two members! But there's an annoying edge case where newly-created channels, say, or channels just containing two members, have to experience the bot trying to respond to all messages there, until at least 3 members are added to the channel.

Has anyone figured out a handy way to detect this? I inspected the Activity JSON for both a message to the app/bot user directly and a message to a channel, but there's no obvious way of being able to distinguish a channel message from a direct-to-bot-user message.


Solution

  • If I remember well, there is a difference in Slack in the naming between a channel and a direct messaging: this difference can be seen in the Conversation.Id field:

    Examples:

    • for a channel: il will look like Bxxxxxxxx:Tyyyyyyyy:Czzzzzzzz
    • for a direct messaging between you and people A: Bxxxxxxxx:Tyyyyyyyy:Daaaaaaaa
    • for a direct messaging with several people: Bxxxxxxxx:Tyyyyyyyy:Gbbbbbbbb

    Where:

    • Bxxxxxxxx is your bot Slack's ID
    • Tyyyyyyyy is your Slack's workspace ID
    • Czzzzzzzz is the current channel Slack's ID (visible in the url when using web version of Slack)

    You will be able to detect your case by checking the conversation ID