Created
September 1, 2018 05:55
-
-
Save munky69rock/8a97b094cb10403c6947d2908b3a92eb to your computer and use it in GitHub Desktop.
Send notification to slack
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 python2 | |
import argparse | |
import json | |
import sys | |
import urllib | |
import urllib2 | |
''' | |
Example Usage: | |
$ python some_script.py | slack-notifier.py -c random -t team1 | |
$ slack-notifier.py -c specific-channel -t team2 -m 'hello world' | |
''' | |
WEBHOOK_URL = {'team1': '__WEBHOOK_URL_1__', 'team2': '__WEBHOOK_URL_2__'} | |
def parse_args(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument( | |
'--channel', '-c', type=str, action='store', default='#general') | |
parser.add_argument( | |
'--icon', '-i', type=str, action='store', default=':robot_face:') | |
parser.add_argument('--message', '-m', type=str, action='store') | |
parser.add_argument( | |
'--name', '-n', type=str, action='store', default='incoming-webhook') | |
parser.add_argument('--team', '-t', type=str, action='store') | |
args = parser.parse_args() | |
return args | |
def main(): | |
args = parse_args() | |
payload = { | |
'channel': args.channel, | |
'username': args.name, | |
'icon_emoji': args.icon, | |
} | |
if not sys.stdin.isatty(): | |
text = '```' + sys.stdin.read() + '```' | |
if args.message is not None: | |
text = args.message + "\n" + text | |
else: | |
text = args.message | |
payload['text'] = text | |
data = json.dumps(payload) | |
url = WEBHOOK_URL[args.team] | |
req = urllib2.Request(url, data, { | |
'Content-Type': 'application/json', | |
'Content-Length': len(data) | |
}) | |
f = urllib2.urlopen(req) | |
response = f.read() | |
f.close() | |
print(response) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment