Skip to content
Permalink
Browse files

Sending a message by user_id does not perform a username lookup now.

  • Loading branch information...
jameskbride committed Mar 30, 2019
1 parent 7890b9c commit 834fe1f347b0f1e0075e50ac4832a77596600420
Showing with 37 additions and 14 deletions.
  1. +25 −14 lib/slack/sends.ex
  2. +12 −0 test/slack/sends_test.exs
@@ -6,6 +6,9 @@ defmodule Slack.Sends do
Sends `text` to `channel` for the given `slack` connection. `channel` can be
a string in the format of `"#CHANNEL_NAME"`, `"@USER_NAME"`, or any ID that
Slack understands.
NOTE: Referencing `"@USER_NAME"` is deprecated, and should not be used.
For more information see https://api.slack.com/changelog/2017-09-the-one-about-usernames
"""
def send_message(text, channel = "#" <> channel_name, slack) do
channel_id = Lookups.lookup_channel_id(channel, slack)
@@ -18,23 +21,16 @@ defmodule Slack.Sends do
end

def send_message(text, user_id = "U" <> _user_id, slack) do
user_name = Slack.Lookups.lookup_user_name(user_id, slack)
send_message(text, user_name, slack)
send_message_to_user(text, user_id, slack)
end

def send_message(text, user = "@" <> _user_name, slack) do
direct_message_id = Lookups.lookup_direct_message_id(user, slack)
def send_message(text, user_id = "W" <> _user_id, slack) do
send_message_to_user(text, user_id, slack)
end

if direct_message_id do
send_message(text, direct_message_id, slack)
else
open_im_channel(
slack.token,
Lookups.lookup_user_id(user, slack),
fn id -> send_message(text, id, slack) end,
fn reason -> reason end
)
end
def send_message(text, user = "@" <> _user_name, slack) do
user_id = Lookups.lookup_user_id(user, slack)
send_message(text, user_id, slack)
end

def send_message(text, channel, slack) do
@@ -90,6 +86,21 @@ defmodule Slack.Sends do
client.cast(pid, {:text, json})
end

defp send_message_to_user(text, user_id, slack) do
direct_message_id = Lookups.lookup_direct_message_id(user_id, slack)

if direct_message_id do
send_message(text, direct_message_id, slack)
else
open_im_channel(
slack.token,
user_id,
fn id -> send_message(text, id, slack) end,
fn reason -> reason end
)
end
end

defp open_im_channel(token, user_id, on_success, on_error) do
url = Application.get_env(:slack, :url, "https://slack.com")

@@ -57,6 +57,18 @@ defmodule Slack.SendsTest do
assert result == {nil, ~s/{"type":"message","text":"hello","channel":"D789"}/}
end

test "send_message understands user ids (Wxxx)" do
slack = %{
process: nil,
client: FakeWebsocketClient,
users: %{"W123" => %{name: "user", id: "W123"}},
ims: %{"D789" => %{user: "W123", id: "D789"}}
}

result = Sends.send_message("hello", "W123", slack)
assert result == {nil, ~s/{"type":"message","text":"hello","channel":"D789"}/}
end

test "indicate_typing sends typing notification to client" do
result = Sends.indicate_typing("channel", %{process: nil, client: FakeWebsocketClient})
assert result == {nil, ~s/{"type":"typing","channel":"channel"}/}

0 comments on commit 834fe1f

Please sign in to comment.
You can’t perform that action at this time.