mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-26 22:51:23 -07:00
Slack: add support for blocks
This commit is contained in:
parent
f5ed0689c1
commit
6ceb43916d
2 changed files with 91 additions and 6 deletions
|
@ -1,6 +1,7 @@
|
||||||
#!/usr/bin/python
|
#!/usr/bin/python
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
# (c) 2020, Lee Goolsbee <lgoolsbee@atlassian.com>
|
||||||
# (c) 2020, Michal Middleton <mm.404@icloud.com>
|
# (c) 2020, Michal Middleton <mm.404@icloud.com>
|
||||||
# (c) 2017, Steve Pletcher <steve@steve-pletcher.com>
|
# (c) 2017, Steve Pletcher <steve@steve-pletcher.com>
|
||||||
# (c) 2016, René Moser <mail@renemoser.net>
|
# (c) 2016, René Moser <mail@renemoser.net>
|
||||||
|
@ -100,7 +101,11 @@ options:
|
||||||
attachments:
|
attachments:
|
||||||
description:
|
description:
|
||||||
- Define a list of attachments. This list mirrors the Slack JSON API.
|
- Define a list of attachments. This list mirrors the Slack JSON API.
|
||||||
- For more information, see also in the (U(https://api.slack.com/docs/attachments)).
|
- For more information, see (U(https://api.slack.com/docs/attachments)).
|
||||||
|
blocks:
|
||||||
|
description:
|
||||||
|
- Define a list of blocks. This list mirrors the Slack JSON API.
|
||||||
|
- For more information, see (U(https://api.slack.com/block-kit)).
|
||||||
"""
|
"""
|
||||||
|
|
||||||
EXAMPLES = """
|
EXAMPLES = """
|
||||||
|
@ -153,6 +158,27 @@ EXAMPLES = """
|
||||||
value: 'load average: 5,16, 4,64, 2,43'
|
value: 'load average: 5,16, 4,64, 2,43'
|
||||||
short: True
|
short: True
|
||||||
|
|
||||||
|
- name: Use the blocks API
|
||||||
|
community.general.slack:
|
||||||
|
token: thetoken/generatedby/slack
|
||||||
|
blocks:
|
||||||
|
- type: section
|
||||||
|
text:
|
||||||
|
type: mrkdwn
|
||||||
|
text: |-
|
||||||
|
*System load*
|
||||||
|
Display my system load on host A and B
|
||||||
|
- type: context
|
||||||
|
elements:
|
||||||
|
- type: mrkdwn
|
||||||
|
text: |-
|
||||||
|
*System A*
|
||||||
|
load average: 0,74, 0,66, 0,63
|
||||||
|
- type: mrkdwn
|
||||||
|
text: |-
|
||||||
|
*System B*
|
||||||
|
load average: 5,16, 4,64, 2,43
|
||||||
|
|
||||||
- name: Send a message with a link using Slack markup
|
- name: Send a message with a link using Slack markup
|
||||||
community.general.slack:
|
community.general.slack:
|
||||||
token: thetoken/generatedby/slack
|
token: thetoken/generatedby/slack
|
||||||
|
@ -206,8 +232,25 @@ def escape_quotes(text):
|
||||||
return "".join(escape_table.get(c, c) for c in text)
|
return "".join(escape_table.get(c, c) for c in text)
|
||||||
|
|
||||||
|
|
||||||
|
def recursive_escape(obj):
|
||||||
|
'''Recursively escape quotes inside "text" and "alt_text" strings inside block kit objects'''
|
||||||
|
block_keys_to_escape = ['text', 'alt_text']
|
||||||
|
if isinstance(obj, dict):
|
||||||
|
escaped = {}
|
||||||
|
for k, v in obj.items():
|
||||||
|
if isinstance(v, str) and k in block_keys_to_escape:
|
||||||
|
escaped[k] = escape_quotes(v)
|
||||||
|
else:
|
||||||
|
escaped[k] = recursive_escape(v)
|
||||||
|
elif isinstance(obj, list):
|
||||||
|
escaped = [recursive_escape(v) for v in obj]
|
||||||
|
else:
|
||||||
|
return obj
|
||||||
|
return escaped
|
||||||
|
|
||||||
|
|
||||||
def build_payload_for_slack(module, text, channel, thread_id, username, icon_url, icon_emoji, link_names,
|
def build_payload_for_slack(module, text, channel, thread_id, username, icon_url, icon_emoji, link_names,
|
||||||
parse, color, attachments):
|
parse, color, attachments, blocks):
|
||||||
payload = {}
|
payload = {}
|
||||||
if color == "normal" and text is not None:
|
if color == "normal" and text is not None:
|
||||||
payload = dict(text=escape_quotes(text))
|
payload = dict(text=escape_quotes(text))
|
||||||
|
@ -237,7 +280,7 @@ def build_payload_for_slack(module, text, channel, thread_id, username, icon_url
|
||||||
payload['attachments'] = []
|
payload['attachments'] = []
|
||||||
|
|
||||||
if attachments is not None:
|
if attachments is not None:
|
||||||
keys_to_escape = [
|
attachment_keys_to_escape = [
|
||||||
'title',
|
'title',
|
||||||
'text',
|
'text',
|
||||||
'author_name',
|
'author_name',
|
||||||
|
@ -245,7 +288,7 @@ def build_payload_for_slack(module, text, channel, thread_id, username, icon_url
|
||||||
'fallback',
|
'fallback',
|
||||||
]
|
]
|
||||||
for attachment in attachments:
|
for attachment in attachments:
|
||||||
for key in keys_to_escape:
|
for key in attachment_keys_to_escape:
|
||||||
if key in attachment:
|
if key in attachment:
|
||||||
attachment[key] = escape_quotes(attachment[key])
|
attachment[key] = escape_quotes(attachment[key])
|
||||||
|
|
||||||
|
@ -254,6 +297,9 @@ def build_payload_for_slack(module, text, channel, thread_id, username, icon_url
|
||||||
|
|
||||||
payload['attachments'].append(attachment)
|
payload['attachments'].append(attachment)
|
||||||
|
|
||||||
|
if blocks is not None:
|
||||||
|
payload['blocks'] = recursive_escape(blocks)
|
||||||
|
|
||||||
payload = module.jsonify(payload)
|
payload = module.jsonify(payload)
|
||||||
return payload
|
return payload
|
||||||
|
|
||||||
|
@ -310,7 +356,8 @@ def main():
|
||||||
parse=dict(type='str', default=None, choices=['none', 'full']),
|
parse=dict(type='str', default=None, choices=['none', 'full']),
|
||||||
validate_certs=dict(default=True, type='bool'),
|
validate_certs=dict(default=True, type='bool'),
|
||||||
color=dict(type='str', default='normal'),
|
color=dict(type='str', default='normal'),
|
||||||
attachments=dict(type='list', required=False, default=None)
|
attachments=dict(type='list', required=False, default=None),
|
||||||
|
blocks=dict(type='list', required=False, default=None)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -326,6 +373,7 @@ def main():
|
||||||
parse = module.params['parse']
|
parse = module.params['parse']
|
||||||
color = module.params['color']
|
color = module.params['color']
|
||||||
attachments = module.params['attachments']
|
attachments = module.params['attachments']
|
||||||
|
blocks = module.params['blocks']
|
||||||
|
|
||||||
color_choices = ['normal', 'good', 'warning', 'danger']
|
color_choices = ['normal', 'good', 'warning', 'danger']
|
||||||
if color not in color_choices and not is_valid_hex_color(color):
|
if color not in color_choices and not is_valid_hex_color(color):
|
||||||
|
@ -333,7 +381,7 @@ def main():
|
||||||
"or any valid hex value with length 3 or 6." % color_choices)
|
"or any valid hex value with length 3 or 6." % color_choices)
|
||||||
|
|
||||||
payload = build_payload_for_slack(module, text, channel, thread_id, username, icon_url, icon_emoji, link_names,
|
payload = build_payload_for_slack(module, text, channel, thread_id, username, icon_url, icon_emoji, link_names,
|
||||||
parse, color, attachments)
|
parse, color, attachments, blocks)
|
||||||
slack_response = do_notify_slack(module, domain, token, payload)
|
slack_response = do_notify_slack(module, domain, token, payload)
|
||||||
|
|
||||||
if 'ok' in slack_response:
|
if 'ok' in slack_response:
|
||||||
|
|
|
@ -88,6 +88,43 @@ class TestSlackModule(ModuleTestCase):
|
||||||
assert call_data['thread_ts'] == '100.00'
|
assert call_data['thread_ts'] == '100.00'
|
||||||
assert fetch_url_mock.call_args[1]['url'] == "https://hooks.slack.com/services/XXXX/YYYY/ZZZZ"
|
assert fetch_url_mock.call_args[1]['url'] == "https://hooks.slack.com/services/XXXX/YYYY/ZZZZ"
|
||||||
|
|
||||||
|
def test_message_with_blocks(self):
|
||||||
|
"""tests sending a message with blocks"""
|
||||||
|
set_module_args({
|
||||||
|
'token': 'XXXX/YYYY/ZZZZ',
|
||||||
|
'msg': 'test',
|
||||||
|
'blocks': [{
|
||||||
|
'type': 'section',
|
||||||
|
'text': {
|
||||||
|
'type': 'mrkdwn',
|
||||||
|
'text': '*test*'
|
||||||
|
},
|
||||||
|
'accessory': {
|
||||||
|
'type': 'image',
|
||||||
|
'image_url': 'https://www.ansible.com/favicon.ico',
|
||||||
|
'alt_text': 'test'
|
||||||
|
}
|
||||||
|
}, {
|
||||||
|
'type': 'section',
|
||||||
|
'text': {
|
||||||
|
'type': 'plain_text',
|
||||||
|
'text': 'test',
|
||||||
|
'emoji': True
|
||||||
|
}
|
||||||
|
}]
|
||||||
|
})
|
||||||
|
|
||||||
|
with patch.object(slack, "fetch_url") as fetch_url_mock:
|
||||||
|
fetch_url_mock.return_value = (None, {"status": 200})
|
||||||
|
with self.assertRaises(AnsibleExitJson):
|
||||||
|
self.module.main()
|
||||||
|
|
||||||
|
self.assertTrue(fetch_url_mock.call_count, 1)
|
||||||
|
call_data = json.loads(fetch_url_mock.call_args[1]['data'])
|
||||||
|
assert call_data['username'] == "Ansible"
|
||||||
|
assert call_data['blocks'][1]['text']['text'] == "test"
|
||||||
|
assert fetch_url_mock.call_args[1]['url'] == "https://hooks.slack.com/services/XXXX/YYYY/ZZZZ"
|
||||||
|
|
||||||
def test_message_with_invalid_color(self):
|
def test_message_with_invalid_color(self):
|
||||||
"""tests sending invalid color value to module"""
|
"""tests sending invalid color value to module"""
|
||||||
set_module_args({
|
set_module_args({
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue