Skip to content

A simple script for sending decorated notifications using Slack Incoming Webhook from Python3

License

Notifications You must be signed in to change notification settings

kiyou/slack_notipy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

39 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

slack-notipy

A simple script for sending decorated notifications using Slack Incoming Webhook from Python3.

Overview

  • Works only with python standard libraries
  • Use the hostname and the process id as the sender name as default
  • Default color scheme for each priority level
  • Context Manager for notification:
    • fields which can notify various outputs by passing a dictionary
    • traceback information of an Exception if raised
    • a flag for notifying only when an Exception is raised
    • elapsed time to finish the with statement
    • a hash of the with statement as a footer as identification
  • Decorator for notification
  • CLI command

Requirements

  • Python3 and its standard libraries
  • python-dotenv (required only when loading .env, not to be installed with this package as dependency)

Install

Clone this repository and run pip install .:

git clone https://github.com/kiyou/slack_notipy.git
cd slack_notipy
pip install .

or one-liner:

pip install git+https://github.com/kiyou/slack_notipy.git

To uninstall, use pip uninstall:

pip uninstall slack_notipy

Usage

  1. Get Slack Webhook URL

    https://api.slack.com/messaging/webhooks

  2. Set environment variable SLACK_WEBHOOK_URL

    • Linux

      # Run following line or append it in your profile file (e.g. ~/.bash_profile)
      export SLACK_WEBHOOK_URL=https://hooks.slack.com/services/*****/*****
    • Command Prompt on Windows

      set SLACK_WEBHOOK_URL=https://hooks.slack.com/services/*****/*****
    • Power Shell on Windows

      $env:SLACK_WEBHOOK_URL = https://hooks.slack.com/services/*****/*****
    • Or install python-dotenv and prepare .env in a runtime directory

      pip install python-dotenv
      echo "SLACK_WEBHOOK_URL=https://hooks.slack.com/services/*****/*****" > .env
  3. Use

    • Context Manager

      # load
      from slack_notipy import Notify
      
      # using context manager
      # notifying a value by fields
      with Notify("context 1") as f:
          a = sum([i for i in range(1, 101)])
          f.fields = a
      
      # notifying multiple values by specifying dictionary to fields
      with Notify("context 2") as f:
          formula = "1 + 1"
          f.fields = {"formula": formula, "results": eval(formula)}
      
      # notifying Exception and stop
      try:
          with Notify("exception in context"):
              print(1 / 0)
      except ZeroDivisionError:
          print("Exception called")
      
      # notifying Exception and continue by catch_exception
      with Notify("catch exception", catch_exception=(ZeroDivisionError,)) as f:
          print(1 / 0)
    • Decorator

      from slack_notipy import context_wrapper
      
      # using decorator to notify return value and duration
      @context_wrapper(name="calc with context wrapper")
      def calc(a, b):
          """
          example calculation
          """
          return a + b
      
      c = calc(1, 1)
      
      try:
          d = calc(1, 0)
      except ZeroDivisionError:
          print("Exception called")
    • CLI

      slack_notipy -h
      # usage: slack_notipy [-h] [--name NAME] [--title TITLE] [--message_type MESSAGE_TYPE] [--color COLOR] [--footer FOOTER] message
      # 
      # Sending decorated notifications using Slack Incoming Webhook from Python3
      # 
      # positional arguments:
      #   message               message to send
      # 
      # options:
      #   -h, --help            show this help message and exit
      #   --name NAME           name of sender, default: slack_notipy:cli
      #   --title TITLE         title, default: default name corresponding to message type
      #   --message_type MESSAGE_TYPE
      #                         message type, default: info
      #   --color COLOR         color, default: default color scheme corresponding to message type
      #   --footer FOOTER       footer, default: slack_notipy:cli on [HOSTNAME]
      slack_notipy "test notification"

Licence

MIT

Author

kiyou

About

A simple script for sending decorated notifications using Slack Incoming Webhook from Python3

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published