BlakeWilliams / Elixir-Slack

Slack real time messaging and web API client in Elixir
MIT License
675 stars 182 forks source link

Check where message came from? #179

Closed dideler closed 5 years ago

dideler commented 5 years ago

Hi, I'm trying to make a bot only respond in group channels when @mentioned, while not needing an @mention for responding to direct messages.

Had a quick look through the docs to see where to get this state from. I assume it's somewhere in the Slack.State struct.

According to the Hex docs

Here’s a list of what’s stored:

  • me - The current bot/users information stored as a map of properties.
  • team - The current team’s information stored as a map of properties.
  • bots - Stored as a map with id’s as keys.
  • channels - Stored as a map with id’s as keys.
  • groups - Stored as a map with id’s as keys.
  • users - Stored as a map with id’s as keys.
  • ims (direct message channels) - Stored as a map with id’s as keys.
  • socket - The connection to Slack.
  • client - The client that makes calls to Slack.

For all but socket and client, you can see what types of data to expect each of the types to contain from the Slack API types page.

Trying to dive deeper into the structure is difficult. The Slack API types is not a 1-to-1 mapping with the properties given on the Hex docs.

BlakeWilliams commented 5 years ago

@dideler I think the message event itself should have the information you're looking for assuming you mean the RTM API. https://api.slack.com/events/message

dideler commented 5 years ago

Thanks @BlakeWilliams. Looks like the contents of ims from %Slack.State{} (the second parameter for handle_event/3) can be checked against the channel ID of the message.

I also noticed that the format of the channel ID encodes information about a channel. E.g. "D" prefix for direct channels, "C" prefix for public group channels, "G" prefix for private group channels. So that's another way to determine what type of channel the message belongs to. Though I couldn't find any documentation for it, so it's riskier to use as Slack could change that convention.