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

View Source

Helpers for writing frames back through an open RTM WebSocket.

These functions are imported into any module that does use Slack, so
inside a bot callback you can write send_message("hi", channel, slack)
directly. They serialise messages to JSON and cast them onto the WebSocket
process referenced by the Slack.State you pass in — so they require an
active RTM connection.

For richer messages (attachments, blocks, files, ephemeral posts), use the
generated Web API modules (Slack.Web.Chat, Slack.Web.Files, etc.)
instead — send_message/3 covers only plain text.

Summary

Functions

Notifies Slack that the bot is typing in channel.

Sends text to channel over the RTM WebSocket held by slack.

Same as send_message/3, but threads the message under an existing parent.

Sends a ping frame, optionally merging extra fields into the payload.

Casts an already-encoded JSON string onto the RTM WebSocket.

Subscribes to presence_change events for ids.

Types

channel()

@type channel() :: String.t()

slack()

@type slack() :: Slack.State.t() | %{process: pid(), client: module()}

Functions

indicate_typing(channel, slack)

@spec indicate_typing(channel(), slack()) :: :ok

Notifies Slack that the bot is typing in channel.

Slack clients render the "@bot is typing…" indicator for a few seconds
after this is called. There is no acknowledgement.

send_message(text, channel, slack)

@spec send_message(String.t(), channel(), slack()) :: :ok

Sends text to channel over the RTM WebSocket held by slack.

channel may be:

  • a channel name prefixed with # (e.g. "#general") — resolved via
    Slack.Lookups.lookup_channel_id/2
  • a user ID starting with U or W — the message is sent to the open
    DM channel for that user, opening one via im.open if necessary
  • a user reference starting with @ (deprecated; see
    Slack's changelog)
  • any channel ID that Slack accepts directly ("C…", "G…", "D…")

Raises ArgumentError if #CHANNEL_NAME cannot be resolved against the
current state.

send_message(text, channel, slack, thread)

@spec send_message(String.t(), channel(), slack(), String.t()) :: :ok

Same as send_message/3, but threads the message under an existing parent.

thread is the ts of the message you want to reply to — use the
top-level parent's ts, not a nested reply's. RTM threaded replies are
always posted, never broadcast back to the channel; for reply_broadcast
support, use the Web API (Slack.Web.Chat).

send_ping(data \\ %{}, slack)

@spec send_ping(map() | keyword(), slack()) :: :ok

Sends a ping frame, optionally merging extra fields into the payload.

Slack replies with a matching pong. The default Slack.WebSocketClient
already issues low-level WebSocket keepalive pings; this is the
application-level RTM ping, useful when you want to round-trip a custom
payload such as a request id.

send_raw(json, map)

@spec send_raw(String.t(), slack()) :: :ok

Casts an already-encoded JSON string onto the RTM WebSocket.

Escape hatch for RTM event types Slack.Sends doesn't wrap directly. The
string must be a complete, well-formed JSON message — Slack will close the
connection on malformed frames.

subscribe_presence(ids \\ [], slack)

@spec subscribe_presence([String.t()], slack()) :: :ok

Subscribes to presence_change events for ids.

Slack's RTM no longer broadcasts presence updates by default — callers
must opt-in per user. Pass a list of user IDs to start receiving events;
pass [] to clear the subscription.