Slink.Event (Slink v0.1.0)

Copy Markdown View Source

A Slack event, normalised into one shape regardless of transport.

Socket Mode wraps events in an envelope (events_api, slash_commands, interactive) and expects an ACK; the Events API delivers the same inner payload over HTTP. Both are reduced here to the same struct so your Slink.handle_event/2 never has to care which transport delivered it.

Fields

  • :type — the routable type. For event callbacks this is the inner Slack event type (e.g. "app_mention", "message"); otherwise the envelope kind ("slash_commands", "interactive", "url_verification", ...).
  • :subtype — the event subtype, when present (e.g. "bot_message").
  • :payload — the inner event/command/interaction map you'll usually read.
  • :raw — the full original map, if you need something not surfaced above.
  • :transport:socket_mode or :http.
  • :kind:event_callback, :slash_commands, :interactive, or :other.
  • :envelope_id — Socket Mode ACK id (nil over HTTP).

Summary

Functions

The channel the event happened in, or nil.

The text addressed to the bot: the event's text with a leading <@…> mention stripped and trimmed.

Whether this event was produced by a bot (including this app itself).

Normalise a decoded Events API HTTP body.

Normalise a decoded Socket Mode envelope.

Whether this event happened inside a thread.

Whether the app itself was mentioned — i.e. an app_mention event.

User IDs mentioned in the event's text, in order (e.g. ["U0123", "U0456"]).

Whether user_id is mentioned in the event's text.

The atom for a known Slack type string, or the string itself if unknown.

The thread_ts to reply into so a reply lands in this event's thread.

The event's text, or an empty string.

The thread this event belongs to, or nil if it's not in a thread.

The event's own message timestamp (ts), or nil.

The user who produced the event (author of the message), or nil.

Types

t()

@type t() :: %Slink.Event{
  envelope_id: String.t() | nil,
  kind: :event_callback | :slash_commands | :interactive | :other,
  payload: map(),
  raw: map(),
  subtype: String.t() | nil,
  transport: :socket_mode | :http,
  type: atom() | String.t() | nil
}

Functions

channel(event)

@spec channel(t()) :: String.t() | nil

The channel the event happened in, or nil.

command(event)

@spec command(t()) :: String.t()

The text addressed to the bot: the event's text with a leading <@…> mention stripped and trimmed.

For app_mention events the text starts with the bot mention, so this returns just the instruction ("@bot deploy now" → "deploy now").

from_bot?(event)

@spec from_bot?(t()) :: boolean()

Whether this event was produced by a bot (including this app itself).

Slack tags bot-authored messages with a bot_id. Handlers use this to skip the bot's own posts so an auto-reply never triggers itself in a loop.

from_http(body)

@spec from_http(map()) :: t()

Normalise a decoded Events API HTTP body.

from_socket_mode(env)

@spec from_socket_mode(map()) :: t()

Normalise a decoded Socket Mode envelope.

in_thread?(event)

@spec in_thread?(t()) :: boolean()

Whether this event happened inside a thread.

mention?(event)

@spec mention?(t()) :: boolean()

Whether the app itself was mentioned — i.e. an app_mention event.

This is the "someone @-mentioned the bot" signal. To see who else is mentioned in the text, use mentions/1.

mentions(event)

@spec mentions(t()) :: [String.t()]

User IDs mentioned in the event's text, in order (e.g. ["U0123", "U0456"]).

Empty when nobody is mentioned.

mentions?(event, user_id)

@spec mentions?(t(), String.t()) :: boolean()

Whether user_id is mentioned in the event's text.

normalize_type(type)

@spec normalize_type(String.t() | nil) :: atom() | String.t() | nil

The atom for a known Slack type string, or the string itself if unknown.

reply_thread(event)

@spec reply_thread(t()) :: String.t() | nil

The thread_ts to reply into so a reply lands in this event's thread.

If the event is already in a thread, that thread; otherwise the event's own ts, so replying starts a thread on it. nil if there's no timestamp.

text(event)

@spec text(t()) :: String.t()

The event's text, or an empty string.

thread_ts(event)

@spec thread_ts(t()) :: String.t() | nil

The thread this event belongs to, or nil if it's not in a thread.

This is Slack's thread_ts — the ts of the thread's root message.

ts(event)

@spec ts(t()) :: String.t() | nil

The event's own message timestamp (ts), or nil.

user(event)

@spec user(t()) :: String.t() | nil

The user who produced the event (author of the message), or nil.