From d4e0e04655fd23d1dc32ebfaa6125e40257dc406 Mon Sep 17 00:00:00 2001 From: Nathan Merritt Date: Tue, 26 Feb 2019 18:00:44 -0500 Subject: [PATCH 1/2] allow user lookup via email --- src/slack | 8 ++++---- test/integration/chat.bats | 4 ++++ 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/slack b/src/slack index e309d95..ca4c8f0 100755 --- a/src/slack +++ b/src/slack @@ -252,10 +252,10 @@ function jqify() { function lchannel() { case "${channel}" in - @*) + *@*) local _user=$(\ curl -s -X POST https://slack.com/api/users.list --data-urlencode "token=${token}" 2>&1 | \ - jq -r ".members | map(select(.name == \"${channel/@/}\" or .profile.display_name == \"${channel/@/}\")) | .[0].id") + jq -r ".members | map(select(.name == \"${channel/@/}\" or .profile.display_name == \"${channel/@/}\" or .profile.email == \"${channel}\")) | .[0].id") local _channel=$(\ curl -s -X POST https://slack.com/api/im.list --data-urlencode "token=${token}" 2>&1 | \ jq -r ".ims | map(select(.user == \"${_user}\")) | .[].id") @@ -268,10 +268,10 @@ function lchannel() { function luser() { case "${user}" in - @*) + *@*) local _user=$(\ curl -s -X POST https://slack.com/api/users.list --data-urlencode "token=${token}" 2>&1 | \ - jq -r ".members | map(select(.name == \"${user/@/}\" or .profile.display_name == \"${user/@/}\")) | .[0].id") + jq -r ".members | map(select(.name == \"${user/@/}\" or .profile.display_name == \"${user/@/}\" or .profile.email == \"${user}\")) | .[0].id") echo ${_user} ;; diff --git a/test/integration/chat.bats b/test/integration/chat.bats index 6d4e341..495ec13 100755 --- a/test/integration/chat.bats +++ b/test/integration/chat.bats @@ -14,6 +14,10 @@ load suite build/bin/slack chat send 'chat send to direct channel should succeed' @rockymadden } +@test 'chat send to direct channel via email should succeed' { + build/bin/slack chat send 'chat send to direct channel via email should succeed' github@rockymadden.com +} + @test 'chat send to invalid channel should fail' { run build/bin/slack chat send 'chat send to invalid channel should fail' '#invalid' [ ${status} -eq 1 ] From c88143c60820da886a3355c16dd815ffd2844da3 Mon Sep 17 00:00:00 2001 From: Nathan Merritt Date: Wed, 6 Mar 2019 17:40:19 -0500 Subject: [PATCH 2/2] use dedicated users.lookupByEmail api method instead --- src/slack | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/src/slack b/src/slack index ca4c8f0..65d318c 100755 --- a/src/slack +++ b/src/slack @@ -253,12 +253,22 @@ function jqify() { function lchannel() { case "${channel}" in *@*) + local _user=$(\ + curl -s -X POST https://slack.com/api/users.lookupByEmail --data-urlencode "token=${token}" --data-urlencode "email=${channel}" 2>&1 | \ + jq -r ".user.id") + local _channel=$(\ + curl -s -X POST https://slack.com/api/im.open --data-urlencode "token=${token}" --data-urlencode "user=${_user}" 2>&1 | \ + jq -r ".channel.id") + + echo ${_channel} + ;; + @*) local _user=$(\ curl -s -X POST https://slack.com/api/users.list --data-urlencode "token=${token}" 2>&1 | \ - jq -r ".members | map(select(.name == \"${channel/@/}\" or .profile.display_name == \"${channel/@/}\" or .profile.email == \"${channel}\")) | .[0].id") + jq -r ".members | map(select(.name == \"${channel/@/}\" or .profile.display_name == \"${channel/@/}\")) | .[0].id") local _channel=$(\ - curl -s -X POST https://slack.com/api/im.list --data-urlencode "token=${token}" 2>&1 | \ - jq -r ".ims | map(select(.user == \"${_user}\")) | .[].id") + curl -s -X POST https://slack.com/api/im.open --data-urlencode "token=${token}" --data-urlencode "user=${_user}" 2>&1 | \ + jq -r ".channel.id") echo ${_channel} ;; @@ -269,9 +279,16 @@ function lchannel() { function luser() { case "${user}" in *@*) + local _user=$(\ + curl -s -X POST https://slack.com/api/users.lookupByEmail --data-urlencode "token=${token}" --data-urlencode "email=${user}" 2>&1 | \ + jq -r ".user.id") + + echo ${_user} + ;; + @*) local _user=$(\ curl -s -X POST https://slack.com/api/users.list --data-urlencode "token=${token}" 2>&1 | \ - jq -r ".members | map(select(.name == \"${user/@/}\" or .profile.display_name == \"${user/@/}\" or .profile.email == \"${user}\")) | .[0].id") + jq -r ".members | map(select(.name == \"${user/@/}\" or .profile.display_name == \"${user/@/}\")) | .[0].id") echo ${_user} ;;