"""
Convenience interface to send messages to the ACCRE slack
"""
import getpass
import socket
import sys
import requests
from accre.config import get_config
from accre.util import accre_argparser
CONFIG = get_config()
[docs]def simple_message(text, channel=None, username=None, icon=None):
"""
Send a simple text message to ACCRE slack.
:param str text: message to send to slack
:param str channel: Channel or user to send to (use at-symbol for user)
or default to random.
:param str username: Username to display in channel, defaults to
combination of current posix user and host
:param str icon: Emoji icon to use for message, defaults to accre
symbol. Colons are not required.
"""
url_hook = CONFIG['slack']['url_hook']
if channel is None:
channel = 'random'
if username is None:
username = '{0}-on-{1}'.format(getpass.getuser(), socket.gethostname())
if icon is None:
icon = 'accre'
payload = {
'channel': channel,
'username': username,
'icon_emoji': ':{0}:'.format(icon),
'text': text
}
resp = requests.post(url_hook, json=payload)
resp.raise_for_status()
[docs]def status_message(
pretext,
items=None,
channel=None,
username=None,
icon=None,
color='good',
footer=None
):
"""
Send a formatted status message to ACCRE slack including a pretext
header message, a set of named items with descrptions, and a status
color. Generally suitable for messages from status checks or audits.
:param str pretext: prefix for message to send to slack
:param dict items: mapping of keys which will appear as item headers
in the message, and values for which the string representation
will be the text of each message item.
:param str channel: Channel or user to send to (use at-symbol for user)
or default to random.
:param str username: Username to display in channel, defaults to
combination of current posix user and host
:param str icon: Emoji icon to use for message, defaults to accre
symbol. Colons are not required.
:param str color: One of good (green), warning (yellow), danger (red)
or a hex code, i.e. #439FE0
:param str footer: Optional message footer
"""
url_hook = CONFIG['slack']['url_hook']
if channel is None:
channel = 'random'
if username is None:
username = '{0}-on-{1}'.format(getpass.getuser(), socket.gethostname())
if icon is None:
icon = 'accre'
if items is None:
items = {}
fields = [
{'title': key, 'value': str(val), 'short': False}
for key, val in items.items()
]
payload = {
'channel': channel,
'username': username,
'icon_emoji': ':{0}:'.format(icon),
'attachments': [{
'fallback': pretext,
'pretext': pretext,
'color': color,
'fields': fields
}]
}
if footer is not None:
payload['attachments'][0]['footer'] = footer
resp = requests.post(url_hook, json=payload)
resp.raise_for_status()
[docs]def main():
"""
CLI entry point for Slack posting
"""
description = "Post a simple text message to the ACCRE slack"
parser = accre_argparser('slackpost', description=description)
parser.add_argument('-m', '--message',
action='store',
default=None,
help="Message to post to slack"
)
parser.add_argument('-c', '--channel',
action='store',
default=None,
help="Channel or user (precede with @) to post to"
)
parser.add_argument('-u', '--username',
action='store',
default=None,
help="Username to display as poster of the message"
)
parser.add_argument('-i', '--icon',
action='store',
default=None,
help="Emoji icon to display with the message"
)
parser.add_argument('-C', '--code',
action='store_true',
help="Format the message as code"
)
args = parser.parse_args()
message = args.message
if message is None:
message = sys.stdin.read()
if args.code:
message = '```\n{}\n```'.format(message)
simple_message(
message,
channel=args.channel,
username=args.username,
icon=args.icon
)
if __name__ == '__main__':
main()