Slackweb Incoming Webhook Client
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
-
AttributeError: 'Slack' object has no attribute 'chat_postMessage'
cause Attempting to use full Slack Web API methods (like `chat_postMessage`) with the `slackweb` library, which is designed only for incoming webhooks.fixUse the `slack_sdk` library for full Slack Web API access. If you only need webhooks, use `slack.notify()`. -
requests.exceptions.MissingSchema: Invalid URL 'YOUR_WEBHOOK_URL': No schema supplied. Perhaps you meant https://YOUR_WEBHOOK_URL?
cause The webhook URL provided to `slackweb.Slack` is missing the `http://` or `https://` scheme.fixEnsure your Slack webhook URL starts with `https://hooks.slack.com/...`. -
TypeError: __init__() missing 1 required positional argument: 'url'
cause You tried to instantiate `slackweb.Slack()` without providing the `url` parameter in versions 1.0.3 and later.fixPass the webhook URL directly to the constructor: `slack = slackweb.Slack(url='YOUR_WEBHOOK_URL')`.
Warnings
- gotcha This library is *only* for Slack Incoming Webhooks. It does not support the full Slack Web API (e.g., chat.postMessage, users.list, RTM API) or Bot Token authentication. For full API access, use `slack_sdk` (the official Python Slack SDK).
- deprecated Prior to v1.0.3, the `Slack` object could potentially be instantiated without a `url` parameter and set later. As of v1.0.3, it is strongly recommended (and effectively required for immediate use) to pass the webhook URL directly to the constructor.
- gotcha The library might not immediately support the newest Slack Block Kit features or advanced authentication methods like OAuth token exchange. Always refer to the official Slack API documentation for the latest webhook payload structure.
Install
-
pip install slackweb
Imports
- Slack
from slackweb import Client
from slackweb import Slack
Quickstart
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.")