Skip to main content

Slack API client library for kiarina namespace

Project description

kiarina-lib-slack

A Python library for Slack API integration with configuration management using pydantic-settings-manager.

Features

  • Configuration Management: Use pydantic-settings-manager for flexible configuration
  • Type Safety: Full type hints and Pydantic validation
  • Secure Credential Handling: Secrets are protected using SecretStr
  • Multiple Configurations: Support for multiple named configurations (e.g., different workspaces/apps)
  • Environment Variable Support: Configure via environment variables with KIARINA_LIB_SLACK_ prefix
  • Comprehensive Settings: Support for OAuth, bot tokens, app tokens, and installation store configuration

Installation

pip install kiarina-lib-slack

Quick Start

Basic Usage

from kiarina.lib.slack import SlackSettings, settings_manager

# Configure Slack App
settings_manager.user_config = {
    "default": {
        "app_id": "A01234567",
        "client_id": "1234567890.1234567890",
        "client_secret": "your-client-secret",
        "signing_secret": "your-signing-secret",
        "scopes": ["chat:write", "channels:read"],
        "bot_token": "xoxb-your-bot-token"
    }
}

# Get settings
settings = settings_manager.settings
print(f"App ID: {settings.app_id}")
print(f"Scopes: {', '.join(settings.scopes)}")

Using with Slack SDK

from slack_sdk import WebClient
from kiarina.lib.slack import settings_manager

# Configure settings
settings_manager.user_config = {
    "default": {
        "app_id": "A01234567",
        "client_id": "1234567890.1234567890",
        "client_secret": "your-client-secret",
        "signing_secret": "your-signing-secret",
        "bot_token": "xoxb-your-bot-token"
    }
}

# Get settings and initialize Slack client
settings = settings_manager.settings
client = WebClient(token=settings.bot_token.get_secret_value())

# Use the client
response = client.chat_postMessage(
    channel="#general",
    text="Hello from kiarina-lib-slack!"
)

Environment Variable Configuration

Configure Slack App using environment variables:

export KIARINA_LIB_SLACK_APP_ID="A01234567"
export KIARINA_LIB_SLACK_CLIENT_ID="1234567890.1234567890"
export KIARINA_LIB_SLACK_CLIENT_SECRET="your-client-secret"
export KIARINA_LIB_SLACK_SIGNING_SECRET="your-signing-secret"
export KIARINA_LIB_SLACK_BOT_TOKEN="xoxb-your-bot-token"
export KIARINA_LIB_SLACK_SCOPES='["chat:write", "channels:read"]'
from kiarina.lib.slack import settings_manager

# Settings are automatically loaded from environment variables
settings = settings_manager.settings
print(f"App ID: {settings.app_id}")

Multiple Configurations

Manage multiple Slack apps:

from kiarina.lib.slack import settings_manager

# Configure multiple apps
settings_manager.user_config = {
    "app_a": {
        "app_id": "A01234567",
        "client_id": "1234567890.1234567890",
        "client_secret": "secret-a",
        "signing_secret": "signing-a",
        "team_id": "T01234567",
        "bot_token": "xoxb-token-a"
    },
    "app_b": {
        "app_id": "A98765432",
        "client_id": "9876543210.9876543210",
        "client_secret": "secret-b",
        "signing_secret": "signing-b",
        "team_id": "T98765432",
        "bot_token": "xoxb-token-b"
    }
}

# Switch between configurations
settings_manager.active_key = "app_a"
app_a_settings = settings_manager.settings
print(f"App A Team: {app_a_settings.team_id}")

settings_manager.active_key = "app_b"
app_b_settings = settings_manager.settings
print(f"App B Team: {app_b_settings.team_id}")

Socket Mode with App Token

Configure for Socket Mode applications:

from kiarina.lib.slack import settings_manager

settings_manager.user_config = {
    "socket_mode": {
        "app_id": "A01234567",
        "client_id": "1234567890.1234567890",
        "client_secret": "your-client-secret",
        "signing_secret": "your-signing-secret",
        "app_token": "xapp-your-app-token",  # Required for Socket Mode
        "bot_token": "xoxb-your-bot-token"
    }
}

settings = settings_manager.settings
print(f"App Token configured: {settings.app_token is not None}")

Configuration

This library uses pydantic-settings-manager for flexible configuration management.

SlackSettings

The SlackSettings class provides the following configuration fields:

Field Type Required Description
app_id str Yes Slack App ID
client_id str Yes Slack Client ID
client_secret SecretStr Yes Slack Client Secret (masked in logs)
signing_secret SecretStr Yes Slack Signing Secret (masked in logs)
scopes list[str] No OAuth Scopes for the Slack App
app_token SecretStr | None No Slack App-Level Token (xapp-...) for Socket Mode
team_id str | None No Slack Team ID
enterprise_id str | None No Slack Enterprise ID
bot_token SecretStr | None No Slack Bot User OAuth Token (xoxb-...)

Environment Variables

All settings can be configured via environment variables with the KIARINA_LIB_SLACK_ prefix:

# Required fields
export KIARINA_LIB_SLACK_APP_ID="A01234567"
export KIARINA_LIB_SLACK_CLIENT_ID="1234567890.1234567890"
export KIARINA_LIB_SLACK_CLIENT_SECRET="your-client-secret"
export KIARINA_LIB_SLACK_SIGNING_SECRET="your-signing-secret"

# Optional fields
export KIARINA_LIB_SLACK_SCOPES='["chat:write", "channels:read"]'
export KIARINA_LIB_SLACK_APP_TOKEN="xapp-your-app-token"
export KIARINA_LIB_SLACK_TEAM_ID="T01234567"
export KIARINA_LIB_SLACK_ENTERPRISE_ID="E01234567"
export KIARINA_LIB_SLACK_BOT_TOKEN="xoxb-your-bot-token"

Programmatic Configuration

from pydantic import SecretStr
from kiarina.lib.slack import SlackSettings, settings_manager

# Direct settings object
settings = SlackSettings(
    app_id="A01234567",
    client_id="1234567890.1234567890",
    client_secret=SecretStr("your-client-secret"),
    signing_secret=SecretStr("your-signing-secret"),
    scopes=["chat:write", "channels:read"],
    bot_token=SecretStr("xoxb-your-bot-token")
)

# Via settings manager
settings_manager.user_config = {
    "default": {
        "app_id": "A01234567",
        "client_id": "1234567890.1234567890",
        "client_secret": "your-client-secret",  # Automatically converted to SecretStr
        "signing_secret": "your-signing-secret",
        "bot_token": "xoxb-your-bot-token"
    }
}

Runtime Overrides

from kiarina.lib.slack import settings_manager

# Override specific settings at runtime
settings_manager.cli_args = {
    "team_id": "T99999999"
}

settings = settings_manager.settings
print(f"Team ID: {settings.team_id}")  # Uses overridden value

Security

Secret Protection

Sensitive fields are stored using Pydantic's SecretStr type, which provides the following security benefits:

  • Masked in logs: Secrets are displayed as ********** in string representations
  • Prevents accidental exposure: Secrets won't appear in debug output or error messages
  • Explicit access required: Must use .get_secret_value() to access the actual secret

Protected fields:

  • client_secret
  • signing_secret
  • app_token
  • bot_token
from kiarina.lib.slack import settings_manager

settings = settings_manager.settings

# Secrets are masked in string representation
print(settings)  # client_secret=SecretStr('**********')

# Explicit access to get the actual secret
client_secret = settings.client_secret.get_secret_value()

API Reference

SlackSettings

class SlackSettings(BaseSettings):
    app_id: str
    client_id: str
    client_secret: SecretStr
    signing_secret: SecretStr
    scopes: list[str] = Field(default_factory=list)
    app_token: SecretStr | None = None
    team_id: str | None = None
    enterprise_id: str | None = None
    bot_token: SecretStr | None = None

Pydantic settings model for Slack API configuration.

Required Fields:

  • app_id (str): Slack App ID
  • client_id (str): Slack Client ID
  • client_secret (SecretStr): Slack Client Secret (protected)
  • signing_secret (SecretStr): Slack Signing Secret (protected)

Optional Fields:

  • scopes (list[str]): OAuth Scopes for the Slack App
  • app_token (SecretStr | None): Slack App-Level Token for Socket Mode (protected)
  • team_id (str | None): Slack Team ID
  • enterprise_id (str | None): Slack Enterprise ID
  • bot_token (SecretStr | None): Slack Bot User OAuth Token (protected)

settings_manager

settings_manager: SettingsManager[SlackSettings]

Global settings manager instance for Slack configuration with multi-configuration support. See: pydantic-settings-manager

Development

Prerequisites

  • Python 3.12+

Setup

# Clone the repository
git clone https://github.com/kiarina/kiarina-python.git
cd kiarina-python

# Setup development environment
mise run setup

Running Tests

# Run format, lint, type checks and tests
mise run package kiarina-lib-slack

# Coverage report
mise run package:test kiarina-lib-slack --coverage

Dependencies

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contributing

This is a personal project, but contributions are welcome! Please feel free to submit issues or pull requests.

Related Projects

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kiarina_lib_slack-2.0.0.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

kiarina_lib_slack-2.0.0-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file kiarina_lib_slack-2.0.0.tar.gz.

File metadata

  • Download URL: kiarina_lib_slack-2.0.0.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for kiarina_lib_slack-2.0.0.tar.gz
Algorithm Hash digest
SHA256 d33e0d86fc410273874490c3065f9add067c8a804fd12c00afa951d898f7cdcd
MD5 ed45458b5091423f80885626cd5d1f78
BLAKE2b-256 0fd6a38b4d7bb8d418868f5ff54b9beb93547142764809d36a7d28934af80958

See more details on using hashes here.

File details

Details for the file kiarina_lib_slack-2.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for kiarina_lib_slack-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6ceca10c201a25b63d2e6a0bed547abe4ff31fef720d62fc225d744aa050f017
MD5 a823e358974053ed3e32d92e1578f2d4
BLAKE2b-256 a26284e8533c9883f5dcb362c522a5ca47c1b8fb25661998ac7db82895c6ecd0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page