I'd like to be able to remove a batch of people from a "private" Slack channel.
Use case: whoops, I just added 137 people to the wrong channel!
The Slack Member ID list is obtained from an Airtable spreadsheet column.
- copy the range of cells from Airtable
- format the values into a single-space separated string
- prepend the Channel ID to the MemberID list
Structure the input arguments like so:
$ sh script.sh ChannelID MemberID1[ MemberID2 MemberID3 ...]
#!/bin/bash
args=("$@")
channel=${args[0]}
for arg in "${@:2}"; do
curl -X POST \
-H 'Authorization: Bearer foobar' \
-H "Content-Type":"application/json; charset=utf-8" \
--data '{"channel":"'"$channel"'", "user":"'"$arg"'"}' \
https://slack.com/api/conversations.kick;
done
The Slack API method I am cURLing is limited to acting upon one MemberID at a time, and limited to only accepting 50 cURL POSTs per minute (Tier 3). I'd like to take a 50+ list of MemberIDs, and make sure they only get cURL'd in 50-member batches that are a minute apart.
I started looking into xargs and made it as far as:
if [ ${#args[@]} -gt 2 ]; then
echo "channel ID: $channel"
echo "${#args[@]} items in the argument array"
echo "${@:2}"
xargs -n 2 <<<${@:2} | xargs -I {} echo {} | sed -e 's/ /,/g'
fi
$ sh test.sh FO1O2B3A4R 1 2 3 4 5 6 7 8 9 10 11
channel ID: FO1O2B3A4R
12 items in the argument array
1 2 3 4 5 6 7 8 9 10 11
1,2
3,4
5,6
7,8
9,10
11
Can I use xargs to make the 50 member batch, then for each one of those members, fire off the cURL command, then wait one minute (sleep 60s
) in between firing off the batches?
sleep 1.25
between each iteration. 50 per minute is a simple one per 1.2 seconds; with 0.05 seconds added for some wiggle room if the API has an interesting sense of time. – DopeGhoti Mar 19 '19 at 16:10