Skip to main content

notification-py

notification-py is a Python package that provides a simple and convenient way to send notifications to Discord, Slack, and Email. You can send notification to either of these using a single command.

Motivation

Lately I noticed several critical errors poping up in my python hosted backend, I thought of a simple way to get notified about these failues (be it Stripe webhook failure or anything else). So I came up with this idea to create a simple notification package which can be imported and used when unexpected errors are thrown by code.

Using this package, developers can receive notifications through various combinations of Discord, Slack, and Email. The package supports sending notifications to all three platforms simultaneously, any two of them, or just one platform, depending on the provided credentials and configuration. This flexibility allows developers to customize their notification setup based on their specific requirements and preferences.

How to install?

To install notification-py using pip:

pip install notification-py

Usage

1. Importing types

To use notification-py in your Python project, you can import the send_notification function from the package. :

from notification_py.custom_types import (
    DiscordCreds,
    Message,
    Creds,
    EmailCreds,
    MessageDetails,
    SlackCreds,
)       

2. Creating Message Object:

Message object is core to the functionality of the notification-py package.

message = Message(
    message_details=MessageDetails(
        title="Test Title",
        text="Test Text",
        severity=2,
        source="Test Source",
        filename="Test Filename",
        line_number=0,
        time=datetime.now(),
    ),
    creds=Creds(
        discord=DiscordCreds(
            token="your_discord_bot_token",
            channel_id=your_discord_channel_id,
            team_id=your_discord_team_id,
        ),
        slack=SlackCreds(webhook_url="your_slack_webhook_url"),
        email=EmailCreds(
            email="your_email",
            password="your_email_password",
            smtp_server="your_smtp_server",
            smtp_port=your_smtp_port,
            target_email="target_email",
        ),
    ),
)

The Message object consists of two main parts:

  1. message_details: An instance of MessageDetails that contains the details of the notification message, such as the title, text, severity, source, filename, line number, and timestamp.
  2. creds: An instance of Creds that holds the credentials for Discord, Slack, and email notifications.

These creds are independent of each other and one can just send Discord notifications using this message object (same applies for other combinations of these):

message = Message(
    message_details=MessageDetails(
        title="Test Title",
        text="Test Text",
        severity=2,
        source="Test Source",
        filename="Test Filename",
        line_number=0,
        time=datetime.now(),
    ),
    creds=Creds(
        discord=DiscordCreds(
            token="your_discord_bot_token",
            channel_id=your_discord_channel_id,
            team_id=your_discord_team_id,
        ),
        slack=None,
        email=None,
    ),
)

3. Sending notification:

You can just call this async function notify to send notification to all the services at a go.

from notification_py.main import send_notification

async def notify(message):
    await send_notification(message)

Please note that only those services which have valid creds will be notified.

Creating Credentials

To send notifications to Discord, Slack, and email, you need to provide the necessary credentials. Here's how you can create the credential objects:

1. Discord:

  1. You can obtain your Discord bot token by simply following this tutorial.
  2. Once you have the token and the bot is installed on your Discord server, just get your channel id and id of the role that you want to notify for the notification.

2. Slack:

  1. Just go to apps and create a new app for your workspace.
  2. Once app is created, select the channel to enable the webhooks on this url
  3. Copy that webhooks url and it's gtg.

3. Email (for gmail):

  1. Make sure you have 2FA enabled.
  2. Just follow along this discussion to get your app password
  3. Once that is done, save the generated password and create email creds like this:
email = "YOUR_ID@gmail.com"
password = "GENERATED_APP_PASSWORD"
smtp_server = "smtp.gmail.com"
smtp_port = "587"
target_email = "RECIPIENT_ID@gmail.com"

Sample notifications

1. Discord

Sample Discord Notifications

2. Slack

Sample Slack Notifications

3. Email

Sample Email Notifications

Contact me

To raise any issues/requests you may refer the issue page here.

You may mail me here on my mail id.

Feel free to connect with me on my LinkedIn.

Please do check out my other projects on my GitHub here.

Also I have made many cool Firefox Add ons. They are pretty useful, you may want to check them out here.

If you like my work, you may want to buy me a book here.

Metadata

Release files for notification_py 1.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for notification_py 1.0.2
File Size Uploaded
notification_py-1.0.2.tar.gz 12.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for notification_py 1.0.2
File Interpreter ABI Platform
notification_py-1.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 25.0 kB

Release files / notification_py-1.0.2.tar.gz

Download URL notification_py-1.0.2.tar.gz
Size 12.3 kB
Tags Source
SHA-256 checksum
How to use checksums
34efa0bfcd73d15feb9dfdb10830e6f4e56684fa819931764bbbd2ff5b8f7ec4
BLAKE2b-256 checksum
How to use checksums
09a4a88a85eca9318080d1c3d3bac4b1d617cb38e903ec76895a9423cc530bca
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.11.8 Linux/6.8.2-zen1-1-zen

Release files / notification_py-1.0.2-py3-none-any.whl

Download URL notification_py-1.0.2-py3-none-any.whl
Size 12.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1d4d0d7659cb4641c4e1dbce4e1c41d698f76c98bb50697eada1a8508c35eab9
BLAKE2b-256 checksum
How to use checksums
69fb9283db25ddb0abe8fa47aa9d7f2e33a295b70c61d491db4da3f161f6c5e5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.11.8 Linux/6.8.2-zen1-1-zen

Release history Release notifications | RSS feed

1.1.0

2 release files

1.0.5

2 release files

1.0.4

2 release files

1.0.3

2 release files

This release

1.0.2 This release

2 release files

1.0.1

2 release files

0.0.11

2 release files

0.0.10

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page