Skip to content

Commit

Permalink
Make multiple calls to get all users
Browse files Browse the repository at this point in the history
  • Loading branch information
bewuethr committed Jul 30, 2022
1 parent 63bf0f3 commit 99ffabb
Showing 1 changed file with 38 additions and 9 deletions.
47 changes: 38 additions & 9 deletions scripts/slacktenure
Original file line number Diff line number Diff line change
@@ -1,19 +1,48 @@
#!/usr/bin/env bash

# Fetch complete users list from Slack API
getusers() {
curl https://slack.com/api/users.list \
--silent \
--get \
--data 'pretty=1' \
# Fetch one page of users list from Slack API
getuserspage() {
local cursor=$1
local args=(
--silent
--get
--data 'pretty=1'
--header "Authorization: Bearer $BOT_TOKEN"
)

if [[ -n $cursor ]]; then
args+=(--data "cursor=$cursor")
fi

curl "${args[@]}" https://slack.com/api/users.list
}

# Get next_cursor from payload
extractcursor() {
local -n payload=$1
jq --raw-output '.response_metadata.next_cursor' <<< "$payload"
}

# Make calls to get users until there is no more next cursor
getusers() {
local page cursor
page=$(getuserspage)
printf '%s\n' "$page"
cursor=$(extractcursor page)

while [[ -n $cursor ]]; do
page=$(getuserspage "$cursor")
printf '%s\n' "$page"
cursor=$(extractcursor page)
done
}

# Remove bot and guest users
extractfullusers() {
jq '
.members |
map(select(
jq --slurp '
map(.members)
| add
| map(select(
(
.is_bot or .is_restricted
| not
Expand Down

0 comments on commit 99ffabb

Please sign in to comment.