Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapt to Slack API changes #235

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions lib/slack/bot.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/slack/rtm.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we change start to connect here, could that cause issues for people who are still relying on the old start behavior?

If that's the case, I wonder if it makes sense to make a new connect method that uses rtm.connect while keeping the start method that keeps using rtm.start?

Copy link
Contributor Author

@binaryseed binaryseed Sep 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What led to this is that I noticed that my bot was fully broken, and rtm.start always returned a 429 rate limit error, even with a single request. I interpreted that as Slack turning off the API.

Perhaps that behavior differs for older Bots though?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes this was fundamentally the issue I ran into with #177 (which lead to #184 and #190)

end
end
2 changes: 1 addition & 1 deletion lib/slack/web/default_client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions test/support/fake_slack/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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