Last active
June 15, 2018 07:49
-
-
Save onjin/7d8c75a3f11db1beee2e to your computer and use it in GitHub Desktop.
Send msgs to slack using incoming webhook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
Usage: | |
using ~/.slackmerc file: | |
$ 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 | |
overwrite config options | |
$ slackme-alert --channel '#general' some info # using `alert` profile | |
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 | |
[team:support] | |
web_hook = https/hooks.slack.com/services/yourteam2/yourid2/somehash2 | |
[icon:ok] | |
icon_emoji = :ghost: | |
[icon:problem] | |
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 | |
""" | |
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' | |
) | |
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, | |
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, | |
'channel': '#general', | |
} | |
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) | |
read_section(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') | |
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'} | |
) | |
try: | |
f = urllib2.urlopen(req) | |
response = f.read() | |
f.close() | |
if 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") | |
sys.stderr.flush() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment