ex_slack v0.2.2 Slack.RtmApi View Source

Connect to the Slack Real Time Messaging API.

Example

iex()> {:ok, rtm} = Slack.RtmApi.start_link()
{:ok, #PID<0.433.0>}
iex()> flush()
{:slack_rtm, "presence_change", %{"presence" => "active", "user" => "U6D1GJDHA"}}
{:slack_rtm, "user_typing", %{"channel" => "C58DCU4A3", "user" => "U211GPD17"}}
:ok

A GenServer example

defmodule MySlackClient do
  @moduledoc "Logs info from slack"
  require Logger
  use GenServer

  def start_link do
    GenServer.start_link(__MODULE__, [], [])
  end

  def stop(client, reason \ :shutdown) do
    GenServer.stop(client, reason)
  end

  def init([]) do
    {:ok, pid} = Slack.RtmApi.start_link()
    {:ok, %{rtm: pid}}
  end

  def handle_info({:slack_rtm, type, data}, state) do
    Logger.debug "Got slack #{type} message: #{inspect data}"
    {:noreply, state}
  end

  def terminate(reason, state) do
    if state.rtm do
      Logger.debug "Disconnecting from Slack."
      Slack.RtmApi.stop(state.rtm, reason)
    end
  end
end

iex()> {:ok, client} = MySlackClient.start_link()
{:ok, #PID<0.354.0>}
15:20:07.329 [debug] "Got slack "user_typing" message: %{"channel" => "C58DCU4A3", "user" => "U211GPD17"}
iex()> MySlackClient.stop(client)
15:20:10.503 [debug] "Disconnecting from Slack."

Messages will always be in the shape of: {:slack_rtm, message_type, message_data} where:

  • message_type - the type of event. see this for details.
  • message_data - the data of the event. see this for details.

Link to this section Summary

Functions

Send a message to the socket

Start a connection to the Real Time Messaging API

Stop and disconnect from the Slack Real Time Messaging API

Link to this section Functions

Send a message to the socket.

Start a connection to the Real Time Messaging API.

Link to this function stop(rtm, reason \\ :shutdown) View Source

Stop and disconnect from the Slack Real Time Messaging API.