pyslack

0.5.0 · deprecated · verified Thu Apr 16

pyslack is a Python API client for interacting with the Slack API. Last updated in September 2019 (version 0.5.0), it provides a synchronous interface for basic Slack operations. It has largely been superseded by the official and actively maintained `slack_sdk` library, which offers a broader feature set, asynchronous support, and better alignment with modern Slack API best practices.

Common errors

Warnings

Install

Imports

Quickstart

Initializes the `pyslack` client with a Slack API token and sends a basic message to a specified channel, then performs an authentication test. This requires a Slack API token (e.g., a legacy token or a bot token) and a channel ID. For new applications, `slack_sdk` is recommended due to its modern authentication and feature set.

import os
from slack import Slack

SLACK_API_TOKEN = os.environ.get('SLACK_API_TOKEN', 'YOUR_SLACK_API_TOKEN')

# Ensure you replace 'YOUR_SLACK_API_TOKEN' with an actual legacy token or bot token
# and 'CHANNEL_ID' with the ID of the channel you want to post to.
# For modern Slack apps, consider using slack_sdk with Bot User OAuth Tokens.

if not SLACK_API_TOKEN or SLACK_API_TOKEN == 'YOUR_SLACK_API_TOKEN':
    print("Error: SLACK_API_TOKEN environment variable not set or is default. Please set your Slack API token.")
else:
    try:
        client = Slack(token=SLACK_API_TOKEN)
        
        # Example: Post a simple message to a channel
        response = client.chat_postMessage(
            channel='CHANNEL_ID',
            text='Hello from pyslack!'
        )
        
        if response.get('ok'):
            print(f"Message sent successfully: {response.get('ts')}")
        else:
            print(f"Error sending message: {response.get('error')}")

        # Example: Get information about the authenticated user
        user_info = client.auth_test()
        if user_info.get('ok'):
            print(f"Authenticated as: {user_info.get('user')}")
        else:
            print(f"Auth test failed: {user_info.get('error')}")

    except Exception as e:
        print(f"An error occurred: {e}")

view raw JSON →