This code is a Python script that interacts with the...
September 2, 2025 at 03:52 PM
This code is a Python script that interacts with the Slack API to analyze and generate a report of user messages in public Slack channels and direct messages (DM/IM), over a configurable lookback period.
Key Steps in the Code:
-
Environment Configuration:
- Fetches configuration values from environment variables:
SLACK_TOKEN
orSLACK_BOT_TOKEN
: The API token used to authenticate against the Slack API.SLACK_API_BASE
: The base Slack API URL (defaults tohttps://slack.com/api
).LOOKBACK_DAYS
: Defines how many days back to look for message histories (defaults to 7).
- Fetches configuration values from environment variables:
-
Slack API Request Helpers:
api_call()
: A function to send API requests to Slack endpoints. It handles retry logic for rate-limited errors (HTTP 429).paginate()
: Handles pagination for Slack API calls where responses return partial data. It iteratively retrieves all pages of a result set using Slack'snext_cursor
value.
-
User and Channel Data Collection:
get_licensed_users()
: Retrieves a list of all active, non-bot, non-guest Slack users from theusers.list
API endpoint.list_conversation_ids()
: Retrieves a list of channel IDs for either public channels or direct/multi-person messaging channels.
-
Message Analysis:
iter_history_with_replies()
: Iterates through the message history of a channel (both parent messages and replies) usingconversations.history
andconversations.replies
API endpoints.is_countable_user_message()
: Determines whether a message is considered a "countable" user message, filtering out certain system/bot messages and irrelevant subtypes.
-
Processing Message Data:
- Loops through public channels, direct messages (
im
), and multi-person DMs (mpim
), counting messages for each user that meet the criteria for "countable" messages. - Counts are separated into public (
public_counts
) and direct message (dm_counts
) categories.
- Loops through public channels, direct messages (
-
Results and Reporting:
- Compiles the message counts for each user into a list of result dictionaries, including:
- User ID, display name, real name.
- Counts of public and DM messages, and the total message count.
- The time range covered by the analysis, in UTC ISO format.
- The configured lookback period in days.
- Outputs the final results as a formatted JSON object, printed to standard output.
- Compiles the message counts for each user into a list of result dictionaries, including:
Example Output:
The output is a JSON object, for example:
{
"results": [
{
"user_id": "U12345",
"display_name": "JohnDoe",
"real_name": "John Doe",
"dm_messages": 10,
"public_messages": 25,
"total_messages": 35,
"start_utc": "2023-10-01T00:00:00+00:00",
"end_utc": "2023-10-08T00:00:00+00:00",
"lookback_days": 7
},
...
]
}
Purpose and Use Case:
This script is likely intended for analyzing Slack user activity, such as:
- Monitoring employee engagement by counting messages in public channels and private messages.
- Generating activity summaries for reporting or audits.
- Providing insights into Slack usage over a defined timeframe.
Preconditions:
- A valid Slack API token with sufficient permissions is required.
- The environment variables (
SLACK_TOKEN
,LOOKBACK_DAYS
, etc.) need to be set before running the script.
Notes:
- The code filters out certain message "subtypes" (e.g., system-generated messages) and bot messages to focus only on relevant user-generated content.
- Rate-limited API calls (HTTP 429) are handled by retrying after the
Retry-After
duration specified by Slack.
Generate your own explanations
Download our vscode extension
Read other generated explanations
Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node