Slack.State (SlackKit v1.0.0-alpha.0)

View Source

Workspace snapshot maintained for a running Slack.Bot.

Each callback in a module that does use Slack receives a %Slack.State{}
as its slack argument. The struct starts from the payload Slack returns on
rtm.start and is rolled forward by update/2 as each RTM event arrives,
so the maps you read inside a callback always reflect the latest known
state of the workspace.

Fields

FieldDescription
:meThe bot's own identity (id, name, profile).
:teamThe workspace's identity (id, name, domain).
:botsMap of bot_id => bot for bots known to the workspace.
:channelsMap of channel_id => channel for public channels.
:groupsMap of channel_id => channel for private channels.
:usersMap of user_id => user. Includes :presence as it's reported.
:imsMap of channel_id => im for open direct-message channels.
:processPID of the Slack.Bot GenServer driving the WebSocket.
:clientModule implementing the WebSocket transport — used by Slack.Sends.
:tokenThe bot's Slack API token.

All entity maps use Slack's string IDs as keys. The values are atom-keyed
maps mirroring the Slack API types — for
example slack.users["U123"].profile.display_name.

Access

Slack.State implements the Access behaviour and is keyed like a map, so
the standard get_in/put_in/update_in helpers work directly against it:

get_in(slack, [:channels, "C123", :name])
put_in(slack, [:users, "U123", :presence], "away")

Updating from RTM events

update/2 pattern-matches on event.type and folds the event into the
state. Supporting a new RTM event type means adding another update/2
clause here. Unrecognised events fall through to a catch-all and leave the
state untouched.

Summary

Functions

Callback implementation for Access.fetch/2.

Callback implementation for Access.pop/2.

Folds a single RTM event into slack, returning the updated state.

Types

t()

@type t() :: %Slack.State{
  bots: map(),
  channels: map(),
  client: module() | nil,
  groups: map(),
  ims: map(),
  me: map() | nil,
  process: pid() | nil,
  team: map() | nil,
  token: String.t() | nil,
  users: map()
}

Functions

fetch(client, key)

Callback implementation for Access.fetch/2.

get(client, key, default)

See Map.get/3.

get_and_update(client, key, function)

Callback implementation for Access.get_and_update/3.

pop(client, key)

Callback implementation for Access.pop/2.

update(map, slack)

@spec update(map(), t()) :: t()

Folds a single RTM event into slack, returning the updated state.

Recognised event types include channel_created, channel_joined,
channel_left, channel_rename, channel_archive, channel_unarchive,
their group_* equivalents, team_rename, team_join, user_change,
presence_change, bot_added, bot_changed, im_created, and the
message subtypes that signal topic/join/leave changes. Any other event is
returned unchanged.