Initial commit for scripts
This commit is contained in:
68
AddAllMemeberstoSpecificChannels.sh
Executable file
68
AddAllMemeberstoSpecificChannels.sh
Executable file
@@ -0,0 +1,68 @@
|
||||
#!/bin/bash
|
||||
|
||||
# --- BEGIN VARIABLES: SET THESE ---
|
||||
ROCKET_CHAT_URL="https://chat.koozali.org"
|
||||
TOKEN="LwSxku5012GVjT5GKukvCiAE3jmxzNREsJ3ZO87oP1D"
|
||||
USER_ID="9RmjfwmQZc9pYrC9G"
|
||||
CHANNELS_FILE="ChannelsToAddTo"
|
||||
# --- END VARIABLES ---
|
||||
|
||||
# Get all users (members) from Rocket.Chat
|
||||
get_all_members() {
|
||||
curl -s -X GET "$ROCKET_CHAT_URL/api/v1/users.list?count=1000" \
|
||||
-H "X-Auth-Token: $TOKEN" \
|
||||
-H "X-User-Id: $USER_ID" \
|
||||
-H "Content-Type: application/json"
|
||||
}
|
||||
|
||||
# Get all members of a channel
|
||||
get_channel_members() {
|
||||
local channel_id="$1"
|
||||
curl -s -X GET "$ROCKET_CHAT_URL/api/v1/channels.members?roomId=$channel_id" \
|
||||
-H "X-Auth-Token: $TOKEN" \
|
||||
-H "X-User-Id: $USER_ID" \
|
||||
-H "Content-Type: application/json"
|
||||
}
|
||||
|
||||
# Add a single user to a channel
|
||||
add_member_to_channel() {
|
||||
local channel_id="$1"
|
||||
local member_id="$2"
|
||||
curl -s -X POST "$ROCKET_CHAT_URL/api/v1/channels.invite" \
|
||||
-H "X-Auth-Token: $TOKEN" \
|
||||
-H "X-User-Id: $USER_ID" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "{\"roomId\":\"$channel_id\", \"userId\":\"$member_id\"}"
|
||||
}
|
||||
|
||||
# MAIN SCRIPT
|
||||
echo "Fetching ALL Rocket.Chat users..."
|
||||
members_response=$(get_all_members)
|
||||
member_ids=$(echo "$members_response" | jq -r '.users[] | select(.active == true) | ._id')
|
||||
total_members=$(echo "$member_ids" | wc -w)
|
||||
echo "Total members to add: $total_members"
|
||||
|
||||
while read -r channel_name channel_id; do
|
||||
echo ""
|
||||
echo "Processing channel '$channel_name' ($channel_id)..."
|
||||
# Get current members of this channel (as a bash array)
|
||||
current_members=$(get_channel_members "$channel_id" | jq -r '.members[] | ._id')
|
||||
# Add each member _only if not already in the channel_
|
||||
added=0
|
||||
already=0
|
||||
for member_id in $member_ids; do
|
||||
if echo "$current_members" | grep -qx "$member_id"; then
|
||||
echo " User $member_id is ALREADY a member of $channel_name"
|
||||
((already++))
|
||||
else
|
||||
resp=$(add_member_to_channel "$channel_id" "$member_id")
|
||||
if echo "$resp" | grep -q '"success":true'; then
|
||||
echo " Added user $member_id to $channel_name"
|
||||
((added++))
|
||||
else
|
||||
echo " Could NOT add $member_id to $channel_name: $(echo "$resp" | jq -c .)"
|
||||
fi
|
||||
fi
|
||||
done
|
||||
echo "$added members added, $already already present in $channel_name"
|
||||
done < "$CHANNELS_FILE"
|
Reference in New Issue
Block a user