Skip to content

Instantly share code, notes, and snippets.

@onjin
Last active June 15, 2018 07:49
Show Gist options
  • Save onjin/7d8c75a3f11db1beee2e to your computer and use it in GitHub Desktop.
Save onjin/7d8c75a3f11db1beee2e to your computer and use it in GitHub Desktop.

Revisions

  1. onjin revised this gist Feb 11, 2015. 1 changed file with 33 additions and 1 deletion.
    34 changes: 33 additions & 1 deletion slackme
    Original file line number Diff line number Diff line change
    @@ -3,13 +3,17 @@
    """
    Usage:
    using ~/.slackmerc file:
    $ slackme some info # with `default` profile
    $ slackme --profile alert some info # using `alert` profile
    put profile in filename
    put profile in filename:
    $ ln -s slackme slackme-alert
    $ slackme-alert some info # using `alert` profile
    overwrite config options
    $ slackme-alert --channel '#general' some info # using `alert` profile
    Example ~/.slackmerc file
    @@ -37,6 +41,34 @@ Usage:
    icon_emoji = :bell:
    Full help:
    $ slackme -h
    usage: slackme [-h] [-w WEB_HOOK] [-c CHANNEL] [-u USERNAME] [-i ICON_EMOJI]
    [-f CONFIG_FILE] [-p PROFILE] [-d] [--dry-run]
    [message [message ...]]
    positional arguments:
    message Message to send
    optional arguments:
    -h, --help show this help message and exit
    -w WEB_HOOK, --web-hook WEB_HOOK
    Slack Incoming Webhook URL
    -c CHANNEL, --channel CHANNEL
    Channel to send message, default #general
    -u USERNAME, --username USERNAME
    Username to send as, default user@hostname
    -i ICON_EMOJI, --icon_emoji ICON_EMOJI
    Emoji icon
    -f CONFIG_FILE, --config-file CONFIG_FILE
    slackme config file, default ~/.slackmerc
    -p PROFILE, --profile PROFILE
    profile to use - section name in config file; default
    `default`
    -d, --debug Enable debug mode
    --dry-run Do not send data, forces debug=True
    Find newer version at:
    * https://gist.github.com/onjin/7d8c75a3f11db1beee2e
  2. onjin revised this gist Feb 11, 2015. 1 changed file with 39 additions and 10 deletions.
    49 changes: 39 additions & 10 deletions slackme
    Original file line number Diff line number Diff line change
    @@ -3,11 +3,15 @@
    """
    Usage:
    $ slackme some info
    $ slackme --profile alert some info
    $ slackme some info # with `default` profile
    $ slackme --profile alert some info # using `alert` profile
    put profile in filename
    $ ln -s slackme slackme-alert
    $ slackme-alert some info # using `alert` profile
    Example ~/.slackmerc file
    Example ~/.slackmerc file
    [default]
    extends = team:default
    @@ -24,14 +28,18 @@ Usage:
    web_hook = https/hooks.slack.com/services/yourteam/yourid/somehash
    [team:support]
    web_hook = https/hooks.slack.com/services/yourteam2/yourid2/somehash2
    web_hook = https/hooks.slack.com/services/yourteam2/yourid2/somehash2
    [icon:ok]
    icon_emoji = :ghost:
    [icon:problem]
    icon_emoji = :bell:
    Find newer version at:
    * https://gist.github.com/onjin/7d8c75a3f11db1beee2e
    """

    import argparse
    @@ -69,20 +77,35 @@ parser.add_argument(
    help='slackme config file, default ~/.slackmerc'
    )
    parser.add_argument(
    '-p', '--profile', action='store', type=str, default='default',
    '-p', '--profile', action='store', type=str,
    help='profile to use - section name in config file; default `default`'
    )
    parser.add_argument(
    '-d', '--debug', action='store_true', default=False,
    help='Enable debug mode'
    )
    parser.add_argument(
    '--dry-run', action='store_true', default=False,
    help='Do not send data, forces debug=True'
    )
    parser.add_argument(
    'message', action='store', type=str, nargs='*',
    help='Message to send'
    )

    args = parser.parse_args()

    debug = args.debug or args.dry_run

    profile = 'default'

    # detect profile from filename suffix f.i. slackme-prv
    if '-' in sys.argv[0]:
    _, profile = sys.argv[0].split('-', 1)

    if args.profile:
    profile = args.profile

    # read config
    data = {
    'username': USERNAME,
    @@ -105,7 +128,7 @@ if os.path.exists(args.config_file):

    config = ConfigParser.ConfigParser()
    config.read(args.config_file)
    read_section(args.profile, data)
    read_section(profile, data)

    for k, v in args.__dict__.items():
    if k in ['web_hook', 'channel', 'username', 'icon_emoji'] and v:
    @@ -118,12 +141,18 @@ if 'extends' in data:
    if 'web_hook' not in data:
    raise ValueError('Missing -w/--web-hook parameter')

    if args.debug:
    pprint.pprint(data)

    web_hook = data['web_hook']
    del data['web_hook']

    if debug:
    pprint.pprint(web_hook)
    pprint.pprint(data)

    if args.dry_run:
    sys.stdout.write('dry-run mode enabled: exitin\n')
    sys.stdout.flush()
    sys.exit(0)

    req = urllib2.Request(
    web_hook, json.dumps(data), {'Content-Type': 'application/json'}
    )
    @@ -132,7 +161,7 @@ try:
    response = f.read()
    f.close()

    if args.debug:
    if debug:
    sys.stdout.write('response: %s' % response)
    sys.stdout.write('\n')
    sys.stdout.flush()
  3. onjin revised this gist Feb 11, 2015. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion slackme
    Original file line number Diff line number Diff line change
    @@ -21,9 +21,10 @@ Usage:
    channel = #issues
    [team:default]
    web_hook = https/hooks.slack.com/services/yourteam/yourid/somehash some message
    web_hook = https/hooks.slack.com/services/yourteam/yourid/somehash
    [team:support]
    web_hook = https/hooks.slack.com/services/yourteam2/yourid2/somehash2
    [icon:ok]
    icon_emoji = :ghost:
  4. onjin revised this gist Feb 11, 2015. 1 changed file with 42 additions and 17 deletions.
    59 changes: 42 additions & 17 deletions slackme
    Original file line number Diff line number Diff line change
    @@ -3,23 +3,34 @@
    """
    Usage:
    $ slackme -w https/hooks.slack.com/services/yourteam/yourid/somehash -c @myuser some message
    or with ~/.slacmerc configured
    $ slacme some info
    $ slackme --profile other_profile some info
    $ slackme some info
    $ slackme --profile alert some info
    Example ~/.slackmerc file
    [default]
    extends = team:default
    channel = #general
    [alert]
    extends = team:default icon:problem
    [issue]
    extends = team:support icon:problem
    channel = #issues
    [team:default]
    web_hook = https/hooks.slack.com/services/yourteam/yourid/somehash some message
    channel = @myuser
    [other_profile]
    web_hook = https/hooks.slack.com/services/yourteam2/yourid/somehash some message
    channel = @myuser2
    [team:support]
    [icon:ok]
    icon_emoji = :ghost:
    [icon:problem]
    icon_emoji = :bell:
    """

    import argparse
    @@ -50,7 +61,7 @@ parser.add_argument(
    )
    parser.add_argument(
    '-i', '--icon_emoji', action='store', type=str, default='',
    help='Emoji icon, default :white_check_mark:'
    help='Emoji icon'
    )
    parser.add_argument(
    '-f', '--config-file', action='store', type=str, default=CONFIG_FILE,
    @@ -75,20 +86,33 @@ args = parser.parse_args()
    data = {
    'username': USERNAME,
    'channel': '#general',
    'icon_emoji': ':white_check_mark:',
    }


    def read_section(section, data):
    if not data:
    data = {}
    if not config.has_section(section):
    raise ValueError('Missing `%s` section' % section)

    if config.has_option(section, 'extends'):
    for parent in config.get(section, 'extends').strip().split():
    read_section(parent, data)
    data.update(dict(config.items(section)))

    if os.path.exists(args.config_file):

    config = ConfigParser.ConfigParser()
    config.read(args.config_file)
    if config.has_section(args.profile):
    data.update(dict(config.items(args.profile)))
    read_section(args.profile, data)

    for k, v in args.__dict__.items():
    if k in ['web_hook', 'channel', 'username', 'icon_emoji'] and v:
    data[k] = v

    data['text'] = ' '.join(args.message)
    if 'extends' in data:
    del data['extends']

    if 'web_hook' not in data:
    raise ValueError('Missing -w/--web-hook parameter')
    @@ -107,9 +131,10 @@ try:
    response = f.read()
    f.close()

    sys.stdout.write(response)
    sys.stdout.write('\n')
    sys.stdout.flush()
    if args.debug:
    sys.stdout.write('response: %s' % response)
    sys.stdout.write('\n')
    sys.stdout.flush()
    except urllib2.HTTPError as e:
    sys.stderr.write('API Error: %s' % e.read())
    sys.stderr.write("\n")
  5. onjin revised this gist Feb 11, 2015. 1 changed file with 14 additions and 10 deletions.
    24 changes: 14 additions & 10 deletions slackme
    Original file line number Diff line number Diff line change
    @@ -3,23 +3,23 @@
    """
    Usage:
    $ slackme -w https://hooks.slack.com/services/yourteam/yourid/somehash -c @myuser some message
    $ slackme -w https/hooks.slack.com/services/yourteam/yourid/somehash -c @myuser some message
    or with ~/.slacmerc configured
    $ slacme some info
    $ sleep 5 && slackme finished # for long tasks
    $ slackme --profile other_profile some info
    Example ~/.slackmerc file
    [default]
    web_hook = https/hooks.slack.com/services/yourteam/yourid/somehash some message
    channel = @myuser
    More options:
    $ slackme -h
    [default]
    web_hook = https/hooks.slack.com/services/yourteam/yourid/somehash some message
    channel = @myuser
    [other_profile]
    web_hook = https/hooks.slack.com/services/yourteam2/yourid/somehash some message
    channel = @myuser2
    """

    import argparse
    @@ -56,6 +56,10 @@ parser.add_argument(
    '-f', '--config-file', action='store', type=str, default=CONFIG_FILE,
    help='slackme config file, default ~/.slackmerc'
    )
    parser.add_argument(
    '-p', '--profile', action='store', type=str, default='default',
    help='profile to use - section name in config file; default `default`'
    )
    parser.add_argument(
    '-d', '--debug', action='store_true', default=False,
    help='Enable debug mode'
    @@ -77,8 +81,8 @@ data = {
    if os.path.exists(args.config_file):
    config = ConfigParser.ConfigParser()
    config.read(args.config_file)
    if config.has_section('default'):
    data.update(dict(config.items('default')))
    if config.has_section(args.profile):
    data.update(dict(config.items(args.profile)))

    for k, v in args.__dict__.items():
    if k in ['web_hook', 'channel', 'username', 'icon_emoji'] and v:
  6. onjin revised this gist Feb 6, 2015. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions slackme
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    """
    Usage:
    $ slackme -w https/hooks.slack.com/services/yourteam/yourid/somehash -c @myuser some message
    $ slackme -w https://hooks.slack.com/services/yourteam/yourid/somehash -c @myuser some message
    or with ~/.slacmerc configured
    @@ -13,7 +13,6 @@ Usage:
    Example ~/.slackmerc file
    [default]
    web_hook = https/hooks.slack.com/services/yourteam/yourid/somehash some message
    channel = @myuser
  7. onjin revised this gist Feb 6, 2015. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion slackme
    Original file line number Diff line number Diff line change
    @@ -4,11 +4,12 @@
    Usage:
    $ slackme -w https/hooks.slack.com/services/yourteam/yourid/somehash -c @myuser some message
    $ sleep 5 && slackme woken # for long tasks
    or with ~/.slacmerc configured
    $ slacme some info
    $ sleep 5 && slackme finished # for long tasks
    Example ~/.slackmerc file
  8. onjin revised this gist Feb 6, 2015. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions slackme
    Original file line number Diff line number Diff line change
    @@ -4,6 +4,7 @@
    Usage:
    $ slackme -w https/hooks.slack.com/services/yourteam/yourid/somehash -c @myuser some message
    $ sleep 5 && slackme woken # for long tasks
    or with ~/.slacmerc configured
  9. onjin created this gist Feb 6, 2015.
    111 changes: 111 additions & 0 deletions slackme
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,111 @@
    #!/usr/bin/env python

    """
    Usage:
    $ slackme -w https/hooks.slack.com/services/yourteam/yourid/somehash -c @myuser some message
    or with ~/.slacmerc configured
    $ slacme some info
    Example ~/.slackmerc file
    [default]
    web_hook = https/hooks.slack.com/services/yourteam/yourid/somehash some message
    channel = @myuser
    More options:
    $ slackme -h
    """

    import argparse
    import json
    import os
    import socket
    import sys
    import urllib2
    import pprint
    import ConfigParser

    CONFIG_FILE = os.path.join(os.getenv('HOME'), '.slackmerc')
    USERNAME = '%s@%s' % (os.getenv('USER'), socket.gethostname())


    parser = argparse.ArgumentParser(prog='slackme')
    parser.add_argument(
    '-w', '--web-hook', action='store', type=str,
    help='Slack Incoming Webhook URL'
    )
    parser.add_argument(
    '-c', '--channel', action='store', type=str, default='',
    help='Channel to send message, default #general'
    )
    parser.add_argument(
    '-u', '--username', action='store', type=str, default='',
    help='Username to send as, default user@hostname'
    )
    parser.add_argument(
    '-i', '--icon_emoji', action='store', type=str, default='',
    help='Emoji icon, default :white_check_mark:'
    )
    parser.add_argument(
    '-f', '--config-file', action='store', type=str, default=CONFIG_FILE,
    help='slackme config file, default ~/.slackmerc'
    )
    parser.add_argument(
    '-d', '--debug', action='store_true', default=False,
    help='Enable debug mode'
    )
    parser.add_argument(
    'message', action='store', type=str, nargs='*',
    help='Message to send'
    )

    args = parser.parse_args()

    # read config
    data = {
    'username': USERNAME,
    'channel': '#general',
    'icon_emoji': ':white_check_mark:',
    }

    if os.path.exists(args.config_file):
    config = ConfigParser.ConfigParser()
    config.read(args.config_file)
    if config.has_section('default'):
    data.update(dict(config.items('default')))

    for k, v in args.__dict__.items():
    if k in ['web_hook', 'channel', 'username', 'icon_emoji'] and v:
    data[k] = v

    data['text'] = ' '.join(args.message)

    if 'web_hook' not in data:
    raise ValueError('Missing -w/--web-hook parameter')

    if args.debug:
    pprint.pprint(data)

    web_hook = data['web_hook']
    del data['web_hook']

    req = urllib2.Request(
    web_hook, json.dumps(data), {'Content-Type': 'application/json'}
    )
    try:
    f = urllib2.urlopen(req)
    response = f.read()
    f.close()

    sys.stdout.write(response)
    sys.stdout.write('\n')
    sys.stdout.flush()
    except urllib2.HTTPError as e:
    sys.stderr.write('API Error: %s' % e.read())
    sys.stderr.write("\n")
    sys.stderr.flush()