Skip to content

Slack automation: messaging, channel monitoring, and notifications #27

@brando90

Description

@brando90

Summary

Build a uutils/slack_uu.py module for Slack automation — send messages, monitor channels, and receive notifications programmatically.

Motivation

Automate research group communications, experiment notifications, and channel monitoring on Slack (e.g., Stanford research group Slack workspaces).

Proposed Implementation

API

  • Slack Web API (official, well-supported)
    • Full-featured: send messages, read channels, upload files, react, etc.
    • Auth: Bot Token or User Token via Slack App
    • Python SDK: pip install slack-sdk
    • Docs: https://api.slack.com/methods

Module structure

class SlackClient:
    @classmethod
    def from_token(cls, token_file="~/keys/slack_bot_token.txt") -> "SlackClient"
    
    def send_message(self, channel, text, thread_ts=None) -> dict
    def upload_file(self, channel, file_path, title="") -> dict
    def list_channels(self) -> list[dict]
    def get_unread_messages(self, channel) -> list[dict]
    def get_channel_history(self, channel, limit=50) -> list[dict]

Use cases

  1. Experiment notifications: "Training run X finished with accuracy Y"
  2. Channel monitoring: Check for unread messages in key channels
  3. File sharing: Upload plots/results to a channel
  4. Scheduled messages: Daily summaries, reminders

🔐 Secrets & Security

  • Bot token stored in ~/keys/slack_bot_token.txt — NEVER in the repo
  • Alternatively: ~/keys/slack_credentials.json with workspace + token
  • .gitignore already excludes credential patterns
  • Use bot tokens (xoxb-) with minimal scopes, not user tokens

🧑 Human Interaction Required

  • One-time: Create a Slack App at https://api.slack.com/apps (~10 min)
  • One-time: Install app to workspace (requires workspace admin approval if restricted)
  • One-time: Copy Bot Token to ~/keys/slack_bot_token.txt
  • One-time: Invite the bot to relevant channels (/invite @botname)

💻 Machine Uptime

  • Sending messages: does NOT require machine to stay on (one-shot)
  • Monitoring channels on a schedule: machine must be on at scheduled time
  • Real-time monitoring (websocket): machine must stay on continuously
  • Recommendation: use cron for periodic checks, not real-time socket

Tasks

  • Create Slack App and configure bot scopes (chat:write, channels:history, files:write)
  • Implement slack_uu.py with token-based auth
  • Add send_message, upload_file, get_channel_history
  • Add unread message checking
  • Add slack-sdk to pyproject.toml
  • Write usage docs in module docstring
  • Test with a real workspace

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions