Skip to main content

Utility functions to send Slack notifications using slack_sdk.

Project description

py2slack

py2slack provides utility functions to send notifications to Slack using the slack_sdk library.

Installation

Install the package via pip:

pip install py2slack

Configuration

Before using the package, you must provide your Slack credentials and default channel information. To do so, prepare one of the following configuration files in your project root (and add them to your .gitignore to protect sensitive data):

Option 1: Using a .env file

Create a file named .env with the following contents:

SLACK_OAUTH_TOKEN=<Your OAuth token>
SLACK_DEFAULT_CHANNEL=<Slack default channel ID (not the name)>

Note: The .env file requires the python-dotenv library. Install it via pip install python-dotenv if you choose this option.

Option 2: Using a slack_config.json file

Alternatively, create a file named slack_config.json with the following JSON content:

{
    "oauth_token": "<Your OAuth token>",
    "default_channel": "<Slack default channel ID (not the name)>"
}

OAuth Token Setup

To use py2slack, you need to create a Slack App Bot and obtain an OAuth token. Refer to this guide for instructions.
Your Bot must have at least the following token scopes:

  • chat:write – to send messages as your bot.
  • files:write – to upload, edit, and delete files as your bot.
  • groups:read – to view basic information about private channels your bot is added to.

After creating your Bot, make sure to invite it to the Slack channel where you want to send notifications.

Basic Usage

After installing the package and preparing your configuration file, you can send messages as follows:

from py2slack import send_slack

# Send a simple text notification
send_slack("Hello, Slack!")

You can also pass a file or specify a channel by using the additional parameters of the send_slack function. Refer to the function’s documentation for further details.

Automatic Error Handling & Notifications Using the @slack_notify Decorator

py2slack now offers additional functionality to automatically wrap your application's main processing code with error handling and Slack notifications. This can help notify you when your process completes successfully or if an unhandled exception occurs. You can integrate this feature using one of the following approaches:

Wrap your main function with the @slack_notify decorator to automatically send a success notification upon normal completion or an error notification if an exception occurs.

from py2slack import send_slack, slack_notify

@slack_notify
def main():
    # Your main process here
    print("Running main process...")
    # Uncomment the following line to simulate an error
    # raise ValueError("Sample error")

if __name__ == '__main__':
    main()

Requirements

  • slack_sdk: Install with pip install slack_sdk
  • python-dotenv: Required only if you are using a .env file for configuration (pip install python-dotenv)

Notes

  • Configuration: Prepare either the .env file or the slack_config.json file—not both.
  • Security: Ensure that your configuration files (i.e., .env and slack_config.json) are added to your .gitignore to avoid committing sensitive data.
  • Module Import: The package’s __init__.py is set up to allow you to import the send_slack function (and additional utilities) directly:
    from py2slack import send_slack, slack_notify
    

License

This project is licensed under the MIT License. See the LICENSE file 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

py2slack-0.4.0.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

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

py2slack-0.4.0-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file py2slack-0.4.0.tar.gz.

File metadata

  • Download URL: py2slack-0.4.0.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.19

File hashes

Hashes for py2slack-0.4.0.tar.gz
Algorithm Hash digest
SHA256 d0faf475cc4410c9e30f06775b6ff2d723306d49f7896b12c2e67edad6c5a713
MD5 c3a3fbba0b8b1b2de973a8899c00ea38
BLAKE2b-256 aeb95f9091362e9bedd46bf2a51737d87a7f0f0b005f0f01456ec5ef3ad54c55

See more details on using hashes here.

File details

Details for the file py2slack-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: py2slack-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 7.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.19

File hashes

Hashes for py2slack-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f16f439f419f442dc1e3915578c99c249a7e3ba2c9528bbdd5afb06a8684859f
MD5 737fa4612b984abe10051afff6829254
BLAKE2b-256 6d82389daf0f23e5ecea1e59bd117648334421e95e96712693549296d66640d2

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