Pydantic Models for working with the Slack API
Project description
slack-models
Comprehensive Pydantic models for working with the Slack API
slack-models provides type-safe, validated data structures for Slack API integration, making it easier to build robust Slack applications with proper error handling and IDE support.
🚀 Features
- Complete Coverage: Models for all major Slack events, objects, and webhook payloads
- Type Safety: Full type annotations with Python 3.12+ modern type hints
- Validation: Automatic data validation using Pydantic 2.x
- IDE Support: Excellent autocomplete and type checking
- Event Parsing: Convenient utilities for parsing webhook payloads
- Standards Compliant: Strict adherence to official Slack API specifications
📦 Installation
pip install slack-models
Requirements:
- Python 3.12+
- pydantic >=2.11.3,<3
🔧 Quick Start
Basic Usage
from slack_models import parse_event, SlackEventCallback, MessageEvent
# Parse a webhook payload
webhook_data = {
"type": "event_callback",
"event": {
"type": "message",
"channel": "C1234567890",
"user": "U1234567890",
"text": "Hello, world!",
"ts": "1234567890.123456"
},
"team_id": "T1234567890",
"api_app_id": "A1234567890"
}
# Parse and validate
event = parse_event(webhook_data)
if isinstance(event, SlackEventCallback):
if isinstance(event.event, MessageEvent):
print(f"Message: {event.event.text}")
print(f"Channel: {event.event.channel}")
print(f"User: {event.event.user}")
Event Handling
from slack_models import (
parse_event, SlackEventCallback, MessageEvent,
ReactionAddedEvent, ChannelCreatedEvent
)
def handle_slack_event(payload: dict):
"""Handle incoming Slack events with type safety."""
event = parse_event(payload)
if isinstance(event, SlackEventCallback):
if isinstance(event.event, MessageEvent):
print(f"New message: {event.event.text}")
elif isinstance(event.event, ReactionAddedEvent):
print(f"Reaction added: {event.event.reaction}")
elif isinstance(event.event, ChannelCreatedEvent):
print(f"Channel created: {event.event.channel.name}")
Working with Models
from slack_models import User, Channel, MessageEvent
# Create and validate user data
user = User(
id="U1234567890",
name="john.doe",
real_name="John Doe",
profile={
"email": "john.doe@example.com",
"display_name": "John",
"first_name": "John",
"last_name": "Doe"
}
)
# Access user information with full type safety
print(f"User: {user.name} ({user.real_name})")
print(f"Email: {user.profile.email}")
📚 Documentation
- Documentation: Comprehensive guides and API reference
- Quick Start: Get up and running quickly
- API Reference: Detailed model documentation
- Examples: Practical usage examples
🎯 Supported Events
slack-models supports all major Slack event types:
Message Events
MessageEvent: Standard messages, bot messages, threaded messagesAppMentionEvent: App mentions in channelsMessageEdited: Message edit events
Reaction Events
ReactionAddedEvent: Reaction additionsReactionRemovedEvent: Reaction removals
Channel Events
ChannelCreatedEvent: New channel creationChannelDeletedEvent: Channel deletionChannelRenameEvent: Channel name changes
Team Events
TeamJoinEvent: New team member joins
File Events
FileCreatedEvent: File uploadsFileDeletedEvent: File deletions
Webhook Types
SlackEventCallback: Standard event callbacksSlackUrlVerification: URL verification challengesSlackAppRateLimited: Rate limiting notifications
Error Handling
from pydantic import ValidationError
from slack_models import parse_event
try:
event = parse_event(webhook_data)
# Process event
except ValidationError as e:
print(f"Invalid payload: {e}")
# Handle validation errors
except Exception as e:
print(f"Processing error: {e}")
# Handle other errors
🧪 Development
Setup
# Clone the repository
git clone https://github.com/gmr/slack-models.git
cd slack-models
# Create virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install development dependencies
pip install -e '.[dev]'
# Install pre-commit hooks
pre-commit install
Testing
# Run tests
pytest
# Run tests with coverage
pytest --cov=slack_models --cov-report=html
# Run linting
ruff check .
# Run type checking
mypy src/slack_models
Documentation
# Build documentation
mkdocs build
# Serve documentation locally
mkdocs serve
🤝 Contributing
Contributions are welcome! Please see our Contributing Guide for details.
Development Workflow
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Run the test suite and linting
- Submit a pull request
📄 License
This project is licensed under the BSD-3-Clause License. See the LICENSE file for details.
🙏 Acknowledgments
- Built for Slack bot projects at AWeber
- Powered by Pydantic for data validation
- Inspired by the Slack API documentation
📞 Support
- Documentation: https://gmr.github.io/slack-models/
- Issues: https://github.com/gmr/slack-models/issues
- Source Code: https://github.com/gmr/slack-models
Made with ❤️ by Gavin M. Roy at AWeber
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file slack_models-1.0.0.tar.gz.
File metadata
- Download URL: slack_models-1.0.0.tar.gz
- Upload date:
- Size: 44.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fba2242e7a31cc88d79c641aaaaf55a84a84396715878a72985793a58ad5e657
|
|
| MD5 |
5a596fe3a18385ca347c417abd28771c
|
|
| BLAKE2b-256 |
3030bb179aa9ab03e54db55e35a265e31c3b41c99d961ac3920a8e481d00ebc1
|
File details
Details for the file slack_models-1.0.0-py3-none-any.whl.
File metadata
- Download URL: slack_models-1.0.0-py3-none-any.whl
- Upload date:
- Size: 10.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
22f9bbddb1822f2b215d85ccc422cc1e35fb5889788269a1ab70221dfea3ff59
|
|
| MD5 |
ebaf19644468fce1dfb09f9c2f70db1f
|
|
| BLAKE2b-256 |
dad775194027826ad2565a3b6d3d2e5e92fd1007b62956021b2a842d15b938d8
|