Slack. Bot
(SlackKit v1.0.0-alpha.0)
View Source
Process that owns a bot's RTM WebSocket connection.
Slack.Bot is the runtime counterpart to the Slack behaviour: it opens the
WebSocket, decodes inbound frames, folds them into a Slack.State, and
dispatches each event to the user-supplied bot_handler module's callbacks.
Lifecycle
start_link/4 performs three steps:
- Hits Slack's
rtm.startendpoint to fetch a one-shot WebSocket URL plus
the bot's initial team/identity payload. The module that performs this
can be overridden viaconfig :slack, :rtm_module, …for tests. - Spawns a
Slack.WebSocketClientpointed at that URL withSlack.Bot
itself as the callback module. - Each inbound text frame is JSON-decoded, atomised, run through
Slack.State.update/2, and forwarded to the user module'shandle_event/3.
You generally won't call this module's callbacks directly — they implement theSlack.WebSocketClient behaviour and exist to glue the socket to your bot.
Summary
Functions
Connects to Slack and delegates events to bot_handler.
Functions
Connects to Slack and delegates events to bot_handler.
Arguments
bot_handler— module implementing theSlackbehaviour (typically a
module that doesuse Slack).initial_state— opaque value handed to the first callback invocation
as thestateargument. Threaded through callbacks from then on.token— the Slack API token for this bot.options— map of optional settings (see below).
Options
:keepalive— milliseconds between WebSocket keepalive pings. Defaults
to10_000.:name— registers the spawned process under the given atom viaProcess.register/2. Defaults tonil(unregistered).:client— module that backs the WebSocket transport. Must implementstart_link/4andcast/2. Defaults toSlack.WebSocketClient;
tests swap in a stub.
Return values
Returns {:ok, pid} on a successful RTM handshake, or {:error, reason} on
failure. Known reasons include:
"Timed out while connecting to the Slack RTM API""Could not connect to the Slack RTM API"— DNS resolution failed"Sent too many connection requests at once to the Slack RTM API."—
Slack has rate-limited thertm.startendpoint- any other error returned by the underlying HTTP client
Examples
{:ok, pid} = Slack.Bot.start_link(MyBot, [], "xoxb-…", %{name: :my_bot})
:sys.get_state(:my_bot)