From e9a9395f79e0d58e3ba2dc7811f8dea33bfc9a50 Mon Sep 17 00:00:00 2001 From: Vince Foley Date: Mon, 10 Aug 2020 13:09:55 -0700 Subject: [PATCH 1/3] fixes related to deprecated start --- lib/slack/bot.ex | 12 ++++++++++-- lib/slack/rtm.ex | 2 +- lib/slack/web/default_client.ex | 2 +- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/slack/bot.ex b/lib/slack/bot.ex index dcc4bd8..2cffb6f 100644 --- a/lib/slack/bot.ex +++ b/lib/slack/bot.ex @@ -99,8 +99,16 @@ defmodule Slack.Bot do _websocket_request, %{slack: slack, process_state: process_state, bot_handler: bot_handler} = state ) do - {:ok, new_process_state} = bot_handler.handle_connect(slack, process_state) - {:ok, %{state | process_state: new_process_state}} + state = + case bot_handler.handle_connect(slack, process_state) do + {:ok, {slack, new_process_state}} -> + %{state | slack: slack, process_state: new_process_state} + + {:ok, new_process_state} -> + %{state | process_state: new_process_state} + end + + {:ok, state} end @doc false diff --git a/lib/slack/rtm.ex b/lib/slack/rtm.ex index 139c84e..0366846 100644 --- a/lib/slack/rtm.ex +++ b/lib/slack/rtm.ex @@ -42,6 +42,6 @@ defmodule Slack.Rtm do defp slack_url(token) do Application.get_env(:slack, :url, "https://slack.com") <> - "/api/rtm.start?token=#{token}&batch_presence_aware=true&presence_sub=true" + "/api/rtm.connect?token=#{token}&batch_presence_aware=true&presence_sub=true" end end diff --git a/lib/slack/web/default_client.ex b/lib/slack/web/default_client.ex index 57484c2..9cb81d6 100644 --- a/lib/slack/web/default_client.ex +++ b/lib/slack/web/default_client.ex @@ -22,7 +22,7 @@ defmodule Slack.Web.DefaultClient do url |> HTTPoison.post!(body, [], opts()) |> Map.fetch!(:body) - |> Jason.decode!(%{}) + |> Jason.decode!(keys: :atoms) end defp opts do From 50c23ea9203efd242a62089fbaef81d0f744d461 Mon Sep 17 00:00:00 2001 From: Vince Foley Date: Wed, 2 Sep 2020 13:49:36 -0700 Subject: [PATCH 2/3] Update upgrade instructions --- README.md | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 857a13e..c1b424e 100644 --- a/README.md +++ b/README.md @@ -34,15 +34,23 @@ The newest version of the Slack client introduces breaking changes with regards ### Make additional calls to the Slack API to fetch bots, channels, groups, users, and IMs -Wherever you grab the passed in `slack` state, add in additional calls to populate these lists: +Wherever you grab the passed in `slack` state, add in additional calls to populate these lists. + +For example, to initialize the bot with the list of channels, fetch them in your `handle_connect` callback: ```elixir -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")) +def handle_connect(slack, state) do + channels = + Slack.Web.Channels.list(%{token: slack.token}) + |> Map.get(:channels) + |> Map.new(&{&1.id, &1}) + + slack = + slack + |> Map.put(:channels, channels) + + {:ok, {slack, state}} +end ``` ## RTM (Bot) Usage From a61312c555afbc6184b7be54f1b4e2804b5e74af Mon Sep 17 00:00:00 2001 From: Vince Foley Date: Wed, 2 Sep 2020 13:51:24 -0700 Subject: [PATCH 3/3] Update test stub api --- test/support/fake_slack/router.ex | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/test/support/fake_slack/router.ex b/test/support/fake_slack/router.ex index 2d6c50b..e9eb703 100644 --- a/test/support/fake_slack/router.ex +++ b/test/support/fake_slack/router.ex @@ -27,4 +27,27 @@ defmodule Slack.FakeSlack.Router do send_resp(conn, 200, response) end + + get "/api/rtm.connect" do + conn = fetch_query_params(conn) + + pid = Application.get_env(:slack, :test_pid) + send(pid, {:token, conn.query_params["token"]}) + + response = ~S( + { + "ok": true, + "url": "ws://localhost:51345/ws", + "self": { "id": "U0123abcd", "name": "bot" }, + "team": { "id": "T4567abcd", "name": "Example Team" }, + "bots": [{ "id": "U0123abcd", "name": "bot" }], + "channels": [], + "groups": [], + "users": [], + "ims": [] + } + ) + + send_resp(conn, 200, response) + end end