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 hidden or 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: | |
| $ 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 | |
| [team:support] | |
| web_hook = https/hooks.slack.com/services/yourteam2/yourid2/somehash2 | |
| [icon:ok] | |
| icon_emoji = :ghost: | |
| [icon:problem] | |
| icon_emoji = :bell: | |
| """ | |
| 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, 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' | |
| ) | |
| parser.add_argument( | |
| 'message', action='store', type=str, nargs='*', | |
| help='Message to send' | |
| ) | |
| args = parser.parse_args() | |
| # 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(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') | |
| 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() | |
| 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") | |
| sys.stderr.flush() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment