GithubHelp home page GithubHelp logo

kiyou / slack_notipy Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 65 KB

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

License: MIT License

Python 89.80% Dockerfile 10.20%
slack python3 cli decorator context-manager

slack_notipy's Introduction

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

slack_notipy's People

Contributors

kiyou avatar

Watchers

 avatar

slack_notipy's Issues

Update setup configuration

Problem

  • cannot get package version at runtime
  • users have to install python-dotenv manually when using .env

Solution

  • use setup.cfg and simplify setup.py
  • add package version as slack_notify.__version__
  • link package version with setup configuration
  • use extras_require to optional requirements for python-dotenv

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.