This is a Slack Real Time Messaging API client for Elixir. You'll need aSlack API token which can be retrieved by following the Token GenerationInstructions or by creating anew bot integration.
Add Slack to your mix.exs
dependencies
function.
def application do
[extra_applications: [:logger]]
end
def deps do
[{:slack, "~> 0.23.6"}]
end
The newest version of the Slack client introduces breaking changes with regards to starting and connecting to the Real Time Messaging API. rtm.start
is now deprecated and has since been replaced with rtm.connect
. This has removed the list of bots
, channels
, groups
, users
, and ims
that are normally returned from rtm.start
. Additionally, these lists are now rate-limited. In order to achieve relative parity to the old way of doing things, you'll need to make one change in your code:
Wherever you grab the passed in slack
state, add in additional calls to populate these lists:
slack
|> Map.put(:bots, Slack.Web.Bots.info(%{token: token}) |> Map.get("bot"))
|> Map.put(:channels, Slack.Web.Channels.list(%{token: token}) |> Map.get("channels"))
|> Map.put(:groups, Slack.Web.Groups.list(%{token: token}) |> Map.get("groups"))
|> Map.put(:ims, Slack.Web.Im.list(%{token: token}) |> Map.get("ims"))
|> Map.put(:users, Slack.Web.Users.list(%{token: token}) |> Map.get("members"))
Define a module that uses the Slack behaviour and defines the appropriatecallback 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 messageas 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
, teamproperties as team
, and the current websocket connection as socket
.
If you want to do things like trigger the sending of messages outside of yourSlack handlers, you can leverage the handle_info/3
callback to implement anexternal API.
This allows you to both respond to Slack RTM events and programmatically controlyour 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 likeabove where unhandled message types don't crash your application. You can find alist of message types and examples on the RTM API page.
You can find more detailed documentation on the Slack hexdocspage.
The complete Slack Web API is implemented by generating modules/functions fromthe JSON documentation. You can view this project's documentation for moredetails.
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 inoptional_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)
A custom client callback module can be configured for cases in which you need extra controlover how calls to the web API are performed. This can be used to control timeouts, or to add additionalcustom 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 urland body passed as arguments.
In the case where you only need to control the options passed to HTTPoison/hackney, the default client acceptsa keyword list as an additional configuration parameter. Note that this is ignored if configuring a custom client.
See HTTPoison docs for a list of available options.
config :slack, :web_http_client_opts, [timeout: 10_000, recv_timeout: 10_000]
For integration tests, you can change the default Slack URL to your fake Slackserver:
config :slack, url: "http://localhost:8000"
Let’ s Build |> 使用 Elixir,Phoenix 和 React 打造克隆版的 Slack (part 3 — Frontend Authentication) Live Demo---GitHub Repo 上一篇博文中我们已经实现用户认证相关的API接口,接下来我们添加前端的登录注册界面并实现用户认证。 关于样式用法的备注:在React项目中,我喜欢作用域在组件内的样式,也就
Elixir是基于python界有名的ORM库SQLAlchemy做的封装。而且是轻量级的封装,它提供了更简单的方式来创建Python类并直接映射到关系数据库表(即通常所说的Active Record设计模式),类似于Django中的ORM。 示例: class Person(Entity): name = Field(String(128)) addresses = OneToMan
Table of Contents Elixir plugin IDEs Features Project From Existing Sources Import project from external model Create project from existing sources New Project Structure Project Settings Module Settin
�� Timber - Great Elixir Logging Made Easy Timber.io is a hosted service for aggregating logs across your entire stack -any language,any platform,any data source. Unlike traditional logging tools, Tim
This repository is the stable base upon which we build our Elixir projects at Mirego. We want to share it with the world so you can build awesome Elixir applications too. Introduction To learn more ab
CURRENTLY NOT COMPATIBLE TO THE AETERNITY NETWORK This implementation aims to be a aims to be a full node that complies with the aeternity specification, in the current state this is not ready yet and
Elixir Runtime for Google Cloud Platform This repository contains the source for the Elixir Runtime for theGoogle App Engine Flexible Environment.It can also be used to run Elixir applications inGoogl