top of page
Writer's pictureIGEL Community

How to Perform an Emergency Reboot with IGEL UMS

Written by Milan Potrok (IGEL COMMUNITY MEMBER OF THE YEAR) and Jeff, IGEL COMMUNITY MEMBER & Insider


Hello, fellow tech enthusiasts!


I've been working on a fun and geeky project that combines the power of Slack, shell scripting, and the awesomeness of the AWTRIX clock. I wanted a way to keep track of the active users in our Slack workspace and display this count live on my AWTRIX clock using MQTT. Why? Because every member of the IGEL Community is so important that I want to see every new one right on my clock! Here’s a step-by-step breakdown of how I did it:


The Script: Counting Active Slack Users and Updating AWTRIX

First, let’s take a look at the script that makes this magic happen:



#!/bin/sh
# Slack API Script with Comprehensive Features
# This script handles counting active Slack users and sending the data to an AWTRIX clock via MQTT.
# Variables
SLACK_TOKEN="xoxb-xxxxxxxxxxx-xxxxxxxxxxxxx-xxxxxxxxxxxxxxxxx" # Your Slack API token
LOG_FILE="/var/log/slack-api.log"
CCU_USER="api_user" # Your CCU user
CCU_PASSWORD="your_password" # Your CCU password
CCU_ENDPOINT="http://your_ccu_endpoint" # Your CCU endpoint
SLACKUSERCOUNT_VARIABLE="S99-Work-SlackAPIUserCount"
# Log file with date
LOG_DATE=$(date +%Y-%m-%d)
LOG_FILE="/var/log/slack-api-$LOG_DATE.log"
# Function to log messages
log_message() {
    echo "$(date): $1" >> "$LOG_FILE"
}
# Function to count active Slack users and update CCU
count_and_update_slack_users() {
    local cursor=""
    local total_active_users=0
    while :
    do
        response=$(curl -s -H "Authorization: Bearer $SLACK_TOKEN" "https://slack.com/api/users.list?limit=1000&cursor=$cursor")
        [ $? -ne 0 ] && return 1
        active_user_count=$(echo $response | jq '[.members[] | select(.deleted == false)] | length')
        total_active_users=$((total_active_users + active_user_count))
        cursor=$(echo $response | jq -r '.response_metadata.next_cursor')
        if [ -z "$cursor" ]; then
            break
        fi
    done
    log_message "Total active Slack users: $total_active_users"
    echo "$total_active_users"
    
    local url="$CCU_ENDPOINT?Antwort=dom.GetObject(\"$SLACKUSERCOUNT_VARIABLE\").State(\"$total_active_users\")"
    response=$(curl -k --user "$CCU_USER:$CCU_PASSWORD" "$url")
    if [ -z "$response" ]; then
        log_message "Error updating CCU with Slack user count"
        return 1
    fi
    log_message "Updated CCU with Slack user count: $total_active_users"
}
# Main execution
main() {
    log_message "Starting API script."
    read_last_run_date
    TODAY=$(date +%Y-%m-%d)
    if [ "$LAST_RUN_DATE" != "$TODAY" ]; then
        write_counters
    fi
    local auth_code=$(get_auth_code)
    [ $? -ne 0 ] && return 1
    local access_token=$(get_access_token "$auth_code")
    [ $? -ne 0 ] && return 1
    update_last_run_date
    log_message "End of run - Counter values: Daily Requests: $DAILY_REQUESTS, Token Refreshes: $TOKEN_REFRESHES"
    
    # Process script arguments for further actions
    case "$1" in
        count_active_slack_users)
            count_and_update_slack_users
            ;;
        *)
            echo "Unknown command: $1"
            return 1
            ;;
    esac
    log_message "API script execution completed."
}
# Start the script with provided arguments
main "$@"

Disclaimer


The content of this post is provided without any warranty or support by IGEL Technology. This information is derived from the IGEL Community, and not IGEL Technology. IGEL Technology will not provide any packages, instructions, or support for processes, scripts, or other content contained in this post. Use at your own risk!


Hope it works well, please let me know if you have some questions or comments!


Comments


bottom of page