Slackweb Incoming Webhook Client

1.0.5 · active · verified Thu Apr 16

Slackweb is a minimalistic Python library for sending messages to Slack via incoming webhooks. It provides a simple interface to construct and send webhook payloads, supporting basic text, attachments, and Block Kit components. The current version is 1.0.5, with releases occurring infrequently, primarily for bug fixes or minor enhancements.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `Slack` client with your webhook URL and send various types of messages, including simple text, messages with custom sender information, and messages with attachments. Ensure `SLACK_WEBHOOK_URL` is set in your environment.

import os
import slackweb

# Get your Slack Incoming Webhook URL from environment variables
# e.g., SLACK_WEBHOOK_URL='https://hooks.slack.com/services/T.../B.../...'
WEBHOOK_URL = os.environ.get('SLACK_WEBHOOK_URL', 'https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX')

if WEBHOOK_URL == 'https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX':
    print("WARNING: Please set the SLACK_WEBHOOK_URL environment variable to a valid Slack Incoming Webhook URL.")
    print("Skipping quickstart execution due to missing URL.")
else:
    try:
        slack = slackweb.Slack(url=WEBHOOK_URL)
        
        # Send a simple text message
        slack.notify(text="Hello from slackweb! This is a test message.")
        print("Successfully sent a simple Slack message.")

        # Send a message to a specific channel with a username and emoji icon
        slack.notify(
            channel="#general", 
            username="My Python Bot", 
            icon_emoji=":robot_face:", 
            text="Another message with custom sender info."
        )
        print("Successfully sent a Slack message with custom sender info.")

        # Send a message with an attachment
        slack.notify(
            attachments=[
                {
                    "title": "Example Attachment",
                    "text": "This is an attachment with some extra info.",
                    "color": "#36a64f"
                }
            ]
        )
        print("Successfully sent a Slack message with an attachment.")

    except Exception as e:
        print(f"An error occurred: {e}")
        print("Please ensure your SLACK_WEBHOOK_URL is correct and accessible.")

view raw JSON →