diff --git a/scripttease/library/overlays/common.py b/scripttease/library/overlays/common.py index 499f547..4483b40 100644 --- a/scripttease/library/overlays/common.py +++ b/scripttease/library/overlays/common.py @@ -9,6 +9,7 @@ __all__ = ( "python_pip", "python_virtualenv", "run", + "slack", ) # Functions @@ -62,10 +63,29 @@ def run(statement, **kwargs): return Command(statement, **kwargs) +def slack(message, url=None, **kwargs): + """Send a message to Slack. + + - message (str): The message to be sent. + - url (str): The webhook URL. This is required. See documentation. + + """ + kwargs.setdefault("comment", "send a message to slack") + + a = list() + + a.append("curl -X POST -H 'Content-type: application/json' --data") + a.append("'" + '{"text": "%s"}' % message + "'") + a.append(url) + + return Command(" ".join(a), **kwargs) + + # Mappings COMMON_MAPPINGS = { 'pip': python_pip, 'run': run, + 'slack': slack, 'virtualenv': python_virtualenv, } diff --git a/tests/test_library_overlays_common.py b/tests/test_library_overlays_common.py index 0d47eed..a22ec85 100644 --- a/tests/test_library_overlays_common.py +++ b/tests/test_library_overlays_common.py @@ -20,3 +20,11 @@ def test_python_virtual_env(): def test_run(): c = run("ls -ls") assert "ls -ls" in c.get_statement() + + +def test_slack(): + c = slack("This is a test.", url="https://example.slack.com/asdf/1234") + s = c.get_statement(suppress_comment=True) + assert "curl -X POST -H 'Content-type: application/json' --data" in s + assert "This is a test." in s + assert "https://example.slack.com/asdf/1234" in s