Skip to main content

No project description provided

Project description

Snap Notify

A Python tool for sending notifications to Slack using a modular DSL template system. This tool allows you to define message templates in YAML format and send them to Slack channels with variable interpolation support.

Features

  • YAML-based message templates
  • Variable interpolation using Jinja2
  • Support for threaded messages
  • Simple CLI interface
  • Secure token management
  • Programmatic usage with Python API

Installation

pip install snap-notify

Prerequisites

  • Python 3.13 or higher
  • A Slack Bot Token (set as SLACK_BOT_TOKEN environment variable)

Usage

1. Create a Message Template

Create a YAML file with your message template. Example:

channel: "#your-channel"
blocks:
  - type: "section"
    text: "Hello {{ name }}! Here's your daily update:"
  - type: "context"
    elements:
      - type: "mrkdwn"
        text: "Report generated at {{ timestamp }}"
interpolate:
  name: "John"
  timestamp: "2024-03-20 10:00:00"

2. Send the Message

Using the CLI

snap-notify --file path/to/template.yaml

You can specify the template format using the --format or -t option:

snap-notify --file path/to/template.yaml --format yaml
# or using the short form
snap-notify -f path/to/template.yaml -t yaml

Using the Client API

from snap_notify import SnapNotifyClient
from snap_notify.parser import parse_template

# Initialize the client
client = SnapNotifyClient()  # Uses SLACK_BOT_TOKEN from environment
# Or provide token directly
# client = SnapNotifyClient(token="your-slack-token")

# Parse your template
template = parse_template("path/to/template.yaml")

# Send the message
response = client.send(template)

You can also create message payloads programmatically:

from snap_notify import SnapNotifyClient

client = SnapNotifyClient()

# Create a message payload
payload = {
    "channel": "#your-channel",
    "blocks": [
        {
            "type": "section",
            "text": {
                "type": "mrkdwn",
                "text": "Hello from Python!"
            }
        }
    ]
}

# Send the message
response = client.send(payload)

Working with Threads

You can create threaded conversations by using the thread_ts parameter:

from snap_notify import SnapNotifyClient

client = SnapNotifyClient()

# Create a parent message
parent_message = {
    "channel": "#your-channel",
    "blocks": [
        {
            "type": "section",
            "text": {
                "type": "mrkdwn",
                "text": "This is the parent message"
            }
        }
    ]
}

# Send the parent message and get its timestamp
parent_response = client.send(parent_message)
thread_ts = parent_response["ts"]

# Create a child message in the thread
child_message = {
    "channel": "#your-channel",
    "thread_ts": thread_ts,  # Link to parent message
    "blocks": [
        {
            "type": "section",
            "text": {
                "type": "mrkdwn",
                "text": "This is a reply in the thread"
            }
        }
    ]
}

# Send the child message
client.send(child_message)

You can also use templates with threads:

# parent.yaml
channel: "#your-channel"
blocks:
  - type: "section"
    text: "Parent message with {{ variable }}"
interpolate:
  variable: "interpolated value"

# child.yaml
channel: "#your-channel"
thread_ts: "{{ parent_ts }}"  # Will be replaced with actual timestamp
blocks:
  - type: "section"
    text: "Child message in thread"
interpolate:
  parent_ts: "1234567890"  # Replace with actual parent message timestamp
  variable: "test message" # Repalce with actual message text

Currently supported formats:

  • yaml (default): YAML template format

Template Structure

The template supports the following fields:

  • channel: The Slack channel to send the message to (required)
  • thread_ts: Thread timestamp for threaded messages (optional)
  • blocks: Array of Slack block elements
  • interpolate: Dictionary of variables for template interpolation

Environment Variables

  • SLACK_BOT_TOKEN: Your Slack Bot User OAuth Token (required)

License

MIT License - see LICENSE for details.

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

snap_notify-0.2.1.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

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

snap_notify-0.2.1-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file snap_notify-0.2.1.tar.gz.

File metadata

  • Download URL: snap_notify-0.2.1.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.2 CPython/3.13.3 Darwin/24.4.0

File hashes

Hashes for snap_notify-0.2.1.tar.gz
Algorithm Hash digest
SHA256 1418f2dbb42906a7a578d4feb854ccdf481e9dbe26e7a4afd83d36d8aca963cc
MD5 9ec3046e6464d4d026c4380d99257e25
BLAKE2b-256 0b8e970372e0f0c6ae4575c0d292ab11915d412306084ef9cec70c1893409f45

See more details on using hashes here.

File details

Details for the file snap_notify-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: snap_notify-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 6.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.2 CPython/3.13.3 Darwin/24.4.0

File hashes

Hashes for snap_notify-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2edbfb787f9e3bc247e4664b2d6d12557bb08d68ea729cccf969563a5d05fdcb
MD5 0c1fb5e48c86db6473cb749ba2697f2b
BLAKE2b-256 e2872c2e2992b16eee4a580eda44555bf957e109ea54318d97a99e90de51e905

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