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
.envfile for configuration (pip install python-dotenv)
Notes
- Configuration: Prepare either the
.envfile or theslack_config.jsonfile—not both. - Security: Ensure that your configuration files (i.e.,
.envandslack_config.json) are added to your.gitignoreto avoid committing sensitive data. - Module Import: The package’s
__init__.pyis set up to allow you to import thesend_slackfunction (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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file py2slack-0.3.1.tar.gz.
File metadata
- Download URL: py2slack-0.3.1.tar.gz
- Upload date:
- Size: 6.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3ed0753f42cbbbe3826aed699b9fc82d538a309c135c8a2ed31fb576bc9e99af
|
|
| MD5 |
0891b64cfc50e4cee3d9d4d59057cbc0
|
|
| BLAKE2b-256 |
d9b83cd6b6ea308b7277097a9b936e717c6de67209f24c693ad5dee8e4c511c6
|
File details
Details for the file py2slack-0.3.1-py3-none-any.whl.
File metadata
- Download URL: py2slack-0.3.1-py3-none-any.whl
- Upload date:
- Size: 7.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4a98bc93462cc1b0d5759171aef0cb743023888627df389d093df34a0ef5fd60
|
|
| MD5 |
dcd1020745dee1f60fd5bd2772bd2821
|
|
| BLAKE2b-256 |
6b6b20df95a8a774a26cd4c397de02bc0c4f836df19279eb0658fe769a1eb794
|