This Python code defines a `SlackUserMessageMetrics` class designed to interact...
This Python code defines a SlackUserMessageMetrics
class designed to interact with the Slack API and compute message metrics for Slack users. Here is what the code does step-by-step:
Purpose:
The script collects and processes metrics related to user messages in a Slack workspace over the last 7 days. It fetches message counts across different conversation types (e.g., public channels, direct messages, and group direct messages) for each user.
Code Breakdown:
-
Imports Required Libraries:
Libraries such asos
,time
,json
,datetime
, andslack_sdk
are imported for accessing environment variables, time delays, JSON manipulation, date handling, and Slack API functionality. -
Class Initialization:
TheSlackUserMessageMetrics
class is initialized with a Slacktoken
to authorize API requests. It also calculates the Unix timestamp for 7 days prior (oldest_timestamp
) to filter messages from that point onward. -
Fetch Licensed Users:
- The
get_licensed_users
method retrieves all users in the Slack workspace, excluding bots, deleted users, and the system-providedUSLACKBOT
. - Results are paginated using
cursor
.
- The
-
Fetch All Conversations:
- The
get_all_conversations
method retrieves all workspace conversations (public channels, private channels, direct messages, and group direct messages) using theconversations_list
API. - Conversations are paginated using
cursor
.
- The
-
Get User Messages for a Conversation:
- The
get_user_messages_in_conversation
method computes the number of public messages and direct messages a specific user sent in a specific conversation within the last 7 days. - It determines whether the conversation is a public channel, direct message, or group DM to categorize the message type.
- The
-
Categorize Conversations:
- The
categorize_conversations
method organizes all retrieved Slack conversations into three categories:public_channels
: Public channels viewable by everyone.direct_messages
: 1-on-1 direct messages.group_direct_messages
: Multi-person direct messages.
- The
-
Compute a User's Message Metrics:
- The
get_user_metrics
method calculates the total messages a user has sent across all public channels, direct messages, and group direct messages. - For each conversation type, it calls the Slack API to fetch message history and filters messages authored by the given user.
- The
-
Compute Metrics for All Users:
- The
gather_all_user_metrics
method combines the above processes to gather message statistics for all licensed users:- Fetches all users.
- Fetches and categorizes all conversations.
- Computes metrics for each user (total messages, public channel messages, and direct messages).
- User data such as
real_name
,email
, and metrics is consolidated into a dictionary.
- The
-
Main Function:
- The
main
function checks for theSLACK_BOT_TOKEN
environment variable to authenticate the script with the Slack workspace. - If valid, it creates an instance of
SlackUserMessageMetrics
, gathers user metrics (gather_all_user_metrics
), and packages the results in a final dictionary:- Period: Last 7 days.
- Current timestamp.
- Total user count.
- Metrics for each user.
- The result is printed as a formatted JSON string.
- The
Output:
The script outputs a JSON object containing:
- The analysis time range (
Last 7 days
). - Total number of licensed (non-bot, active) users analyzed.
- Detailed message metrics (public, direct, total) for each user, along with their name and email.
Key Functionality:
- Uses Slack API methods:
users_list
: Fetch workspace users.conversations_list
: Fetch workspace conversations.conversations_info
: Retrieve metadata about a conversation.conversations_history
: Fetch message history for a conversation.
- Filters Messages:
- Filters by user, Slack message type, and time period (7 days).
- Aggregates User Metrics:
- Categorizes and aggregates messages sent across conversation types.
Practical Use:
The code is useful for Slack workspace admins or analysts looking to monitor user activity, message trends, or engagement within their organization.