Last active
February 25, 2019 08:19
-
-
Save MvRens/9afa12ce14b15d3482f40cf29e6a2df7 to your computer and use it in GitHub Desktop.
Python wrapper script for Slack chat.postMessage API
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/python | |
import sys | |
import argparse | |
import requests | |
parser = argparse.ArgumentParser(description = 'Slack chat.postMessage wrapper') | |
parser.add_argument('token', help = 'The OAuth token for your app with chat:write:bot permission.') | |
parser.add_argument('channel', help = 'The channel name to post to, without the pound sign.') | |
parser.add_argument('-m', '--message', help = 'The message to send. Either this or the file parameter is required.') | |
parser.add_argument('-f', '--file', help = 'The message text file to send. Either this or the message parameter is required.') | |
parser.add_argument('-a', '--attachment', nargs = '*', help = 'An optional attachment text file to send. Alternatively you can pipe the attachment to STDIN.') | |
parser.add_argument('-ac', '--attachment-color', help = 'The color used for the attachment\'s indentation.') | |
args = parser.parse_args() | |
payload = { | |
'channel': args.channel | |
} | |
# Compose text (simply append if both the message and file are supplied) | |
if args.message is not None: | |
text = args.message.replace('\\n', '\n') | |
else: | |
text = '' | |
if args.file is not None: | |
try: | |
with open(args.file, 'r') as messageFile: | |
text += messageFile.read() | |
except: | |
print('Error while trying to read message from ' + args.file) | |
#exit() | |
payload['text'] = text | |
# Parse attachment(s) | |
def add_attachment(text, filename = None): | |
attachment = { | |
'text': text | |
} | |
if args.attachment_color is not None: | |
attachment['color'] = args.attachment_color | |
if filename is not None: | |
attachment['title'] = filename | |
if 'attachments' in payload: | |
payload['attachments'].append(attachment) | |
else: | |
payload['attachments'] = [attachment] | |
# isatty returns false if the input is not a terminal, e.g. piped in data | |
if not sys.stdin.isatty(): | |
add_attachment(sys.stdin.read()) | |
if args.attachment is not None: | |
for attachment in args.attachment: | |
try: | |
with open(attachment, 'r') as attachmentFile: | |
add_attachment(attachmentFile.read(), attachment) | |
except: | |
print('Error while trying to read attachment from ' + attachment) | |
#exit() | |
headers = { | |
'Content-Type': 'application/json; charset=UTF-8', | |
'Authorization': 'Bearer ' + args.token | |
} | |
response = requests.post('https://slack.com/api/chat.postMessage', headers = headers, json = payload) | |
if response.status_code != 200: | |
print(response.text) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment