From a5e0fc331dc113497b02b1f6c7e509fbe6e5b734 Mon Sep 17 00:00:00 2001 From: Toshio Kuratomi Date: Fri, 6 Mar 2015 07:58:44 -0800 Subject: [PATCH] The old slack webhook API still works. It's just deprecated by slack. Analyze the given token and use the old webhook format if the token is old style and use the new format if the token is new style. Make domain optional with new-style tokens. Fixes #157 --- notification/slack.py | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/notification/slack.py b/notification/slack.py index 5577228978b..1ae748247f9 100644 --- a/notification/slack.py +++ b/notification/slack.py @@ -28,12 +28,20 @@ author: Ramon de la Fuente options: domain: description: - - Slack (sub)domain for your environment without protocol. - (i.e. C(future500.slack.com)) - required: true + - Slack (sub)domain for your environment without protocol. (i.e. + C(future500.slack.com)) In 1.8 and beyond, this is deprecated and may + be ignored. See token documentation for information. + required: false token: description: - - Slack integration token + - Slack integration token. This authenticates you to the slack service. + Prior to 1.8, a token looked like C(3Ffe373sfhRE6y42Fg3rvf4GlK). In + 1.8 and above, ansible adapts to the new slack API where tokens look + like C(G922VJP24/D921DW937/3Ffe373sfhRE6y42Fg3rvf4GlK). If tokens + are in the new format then slack will ignore any value of domain. If + the token is in the old format the domain is required. Ansible has no + control of when slack will get rid of the old API. When slack does + that the old format will stop working. required: true msg: description: @@ -105,6 +113,7 @@ EXAMPLES = """ """ +OLD_SLACK_INCOMING_WEBHOOK = 'https://%s/services/hooks/incoming-webhook?token=%s' SLACK_INCOMING_WEBHOOK = 'https://hooks.slack.com/services/%s' def build_payload_for_slack(module, text, channel, username, icon_url, icon_emoji, link_names, parse): @@ -127,7 +136,13 @@ def build_payload_for_slack(module, text, channel, username, icon_url, icon_emoj return payload def do_notify_slack(module, domain, token, payload): - slack_incoming_webhook = SLACK_INCOMING_WEBHOOK % (token) + if token.count('/') >= 2: + # New style token + slack_incoming_webhook = SLACK_INCOMING_WEBHOOK % (token) + else: + if not domain: + module.fail_json(msg="Slack has updated its webhook API. You need to specify a token of the form XXXX/YYYY/ZZZZ in your playbook") + slack_incoming_webhook = OLD_SLACK_INCOMING_WEBHOOK % (domain, token) response, info = fetch_url(module, slack_incoming_webhook, data=payload) if info['status'] != 200: @@ -137,7 +152,7 @@ def do_notify_slack(module, domain, token, payload): def main(): module = AnsibleModule( argument_spec = dict( - domain = dict(type='str', required=True), + domain = dict(type='str', required=False, default=None), token = dict(type='str', required=True), msg = dict(type='str', required=True), channel = dict(type='str', default=None),