Giter VIP home page Giter VIP logo

elixir-slack's Introduction

Build Status

Elixir-Slack

This is a Slack Real Time Messaging API client for Elixir. You'll need a Slack API token which can be retrieved from the Web API page or by creating a new bot integration.

Installing

Add Slack to your mix.exs dependencies function.

If you do not already have a JSON library, pick :poison or :jason by adding either to your mix.exs

def application do
  [extra_applications: [:logger]]
end

def deps do
  [
    {:slack, "~> 0.14.0"},
    ## Poison or Jason
    {:poison, "~> 3.0"},
    {:jason, "~> 1.1"}
  ]
end

Then make sure to add this to your appropriate configuration file config/<env>.exs:

For Poison:

config :slack,
  json_library: Poison

For Jason:

config :slack,
  json_library: Jason

RTM (Bot) Usage

Define a module that uses the Slack behaviour and defines the appropriate callback methods.

defmodule SlackRtm do
  use Slack

  def handle_connect(slack, state) do
    IO.puts "Connected as #{slack.me.name}"
    {:ok, state}
  end

  def handle_event(message = %{type: "message"}, slack, state) do
    send_message("I got a message!", message.channel, slack)
    {:ok, state}
  end
  def handle_event(_, _, state), do: {:ok, state}

  def handle_info({:message, text, channel}, slack, state) do
    IO.puts "Sending your message, captain!"

    send_message(text, channel, slack)

    {:ok, state}
  end
  def handle_info(_, _, state), do: {:ok, state}
end

To run this example, you'll want to call Slack.Bot.start_link(SlackRtm, [], "TOKEN_HERE") and run the project with mix run --no-halt.

You can send messages to channels using send_message/3 which takes the message as the first argument, channel/user as the second, and the passed in slack state as the third.

The passed in slack state holds the current user properties as me, team properties as team, the current websocket connection as socket, and a list of bots, channels, groups, users, and ims (direct message channels).

If you want to do things like trigger the sending of messages outside of your Slack handlers, you can leverage the handle_info/3 callback to implement an external API.

This allows you to both respond to Slack RTM events and programmatically control your bot from external events.

{:ok, rtm} = Slack.Bot.start_link(SlackRtm, [], "token")
send rtm, {:message, "External message", "#general"}
#=> {:message, "External message", "#general"}
#==> Sending your message, captain!

Slack has a lot of message types so it's a good idea to define a callback like above where unhandled message types don't crash your application. You can find a list of message types and examples on the RTM API page.

You can find more detailed documentation on the Slack hexdocs page.

Web API Usage

The complete Slack Web API is implemented by generating modules/functions from the JSON documentation. You can view this project's documentation for more details.

There are two ways to authenticate your API calls. You can configure api_token on slack that will authenticate all calls to the API automatically.

config :slack, api_token: "VALUE"

Alternatively you can pass in %{token: "VALUE"} to any API call in optional_params. This also allows you to override the configured api_token value if desired.

Quick example, getting the names of everyone on your team:

names = Slack.Web.Users.list(%{token: "TOKEN_HERE"})
|> Map.get("members")
|> Enum.map(fn(member) ->
  member["real_name"]
end)

Web Client Configuration

A custom client callback module can be configured for cases in which you need extra control over how calls to the web API are performed. This can be used to control timeouts, or to add additional custom error handling as needed.

config :slack, :web_http_client, YourApp.CustomClient

All Web API calls from documentation-generated modules/functions will call post!/2 with the generated url and body passed as arguments.

In the case where you only need to control the options passed to HTTPoison/hackney, the default client accepts a keyword list as an additional configuration parameter. Note that this is ignored if configuring a custom client.

See HTTPoison docs for a list of avilable options.

config :slack, :web_http_client_opts, [timeout: 10_000, recv_timeout: 10_000]

Testing

For integration tests, you can change the default Slack URL to your fake Slack server:

config :slack, url: "http://localhost:8000"

elixir-slack's People

Contributors

acconrad avatar afaur avatar blakewilliams avatar christophermaier avatar davydog187 avatar devl avatar dylangriffith avatar iamjarvo avatar jeg2 avatar josephan avatar kazucocoa avatar kcurtin avatar lugghawk avatar manuraj17 avatar mazubieta avatar mgwidmann avatar mikeastock avatar mmartinson avatar ne1ro avatar nrw505 avatar octosteve avatar pavellishin avatar povilas avatar remi avatar reneklacan avatar roehst avatar thbar avatar vanstee avatar waynehoover avatar wgottschalk-rms avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.