Files
Rocket-API-Scripts/getRocketRoomMemberMatrix.sh

73 lines
2.5 KiB
Bash
Raw Normal View History

2025-07-18 06:34:00 +01:00
#!/bin/bash
# Variables
ROCKET_CHAT_URL="https://chat.koozali.org" # Replace with your Rocket.Chat URL
TOKEN="LwSxku5012GVjT5GKukvCiAE3jmxzNREsJ3ZO87oP1D" # Replace with your user token
USER_ID="9RmjfwmQZc9pYrC9G"
# Function to get a list of channels
get_channel_list() {
curl -s -X GET "$ROCKET_CHAT_URL/api/v1/channels.list" \
-H "X-Auth-Token: $TOKEN" \
-H "X-User-Id: $USER_ID" \
-H "Content-Type: application/json"
}
# Function to get members in a given room
get_members_in_channel() {
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"
}
# Main Logic
channels=$(get_channel_list)
# Check if the channel list request was successful
if echo "$channels" | jq -e . >/dev/null; then
# Create an array to store channel information
mapfile -t channel_array < <(echo "$channels" | jq -c '.channels[] | {id: ._id, name: .fname}')
# Print header
echo -e "Channel Name \t\t\t Members"
echo -e "--------------\t\t\t-----------------------------"
# Iterate over each channel from the array
for channel in "${channel_array[@]}"; do
channel_id=$(echo "$channel" | jq -r '.id')
channel_name=$(echo "$channel" | jq -r '.name')
# Check if channel_id and channel_name were extracted correctly
if [[ -z "$channel_id" || -z "$channel_name" ]]; then
echo "Warning: Channel ID or Name is empty. Channel data: $channel"
continue
fi
#echo "Fetching members for channel: $channel_name (ID: $channel_id)" # Debugging line
# Get members for this channel
members_response=$(get_members_in_channel "$channel_id")
# Output the raw response for debugging
#echo "Members Response: $members_response"
# Check if the members response is valid
if echo "$members_response" | jq -e . >/dev/null; then
member_ids=$(echo "$members_response" | jq -r '.members[].username // empty') # Capture usernames
# Create a members list
member_list=$(echo "$member_ids" | tr '\n' ', ' | sed 's/, $//') # Join members with a comma
echo -e "$channel_name \t\t\t $member_list"
else
echo "$channel_name | Unable to fetch members or no members found"
fi
done
else
echo "Failed to retrieve channels or invalid response."
echo "$channels" # Print the actual response for debugging
fi
echo "Done."