Skip to content
Snippets Groups Projects
Commit bec040fb authored by Sven Mäder's avatar Sven Mäder :speech_balloon:
Browse files

Use dict.get() for /webhook/slack

parent ec6b1dc0
No related branches found
No related tags found
No related merge requests found
...@@ -11,36 +11,40 @@ def slack(hook): ...@@ -11,36 +11,40 @@ def slack(hook):
incoming = request.json incoming = request.json
print('Got incoming /slack hook: ' + str(incoming)) print('Got incoming /slack hook: ' + str(incoming))
if 'attachments' in incoming: attachments = incoming.get('attachments', [])
for attachment in incoming['attachments']: username = str(incoming.get('username', ''))
color = ''
if 'color' in attachment: for attachment in attachments:
color = str(attachment['color']).lower() color = str(attachment.get('color', '')).lower()
html += '<font color="' + color + '">' if color else '' title = str(attachment.get('title', ''))
if 'title' in attachment: title_link = str(attachment.get('title_link', ''))
title = str(attachment['title']) text = str(attachment.get('text', ''))
if 'title_link' in attachment: fields = attachment.get('fields', [])
title_link = str(attachment['title_link'])
plain += title + ' ' + title_link + '\n' html += '<font color="' + color + '">' if color else ''
html += '<b><a href="' + title_link + '">' + title + '</a></b><br/>\n'
else: if title and title_link:
plain += title + '\n' plain += title + ' ' + title_link + '\n'
html += '<b>' + title + '</b>\n' html += '<b><a href="' + title_link + '">' + title + '</a></b><br/>\n'
if 'text' in attachment:
text = str(attachment['text']) if text:
plain += text + '\n' plain += text + '\n'
html += text + '<br/>\n' html += text + '<br/>\n'
if 'fields' in attachment:
for field in attachment['fields']: for field in fields:
if 'title' in field and 'value' in field: title = str(field.get('title', ''))
title = str(field['title']) value = str(field.get('value', ''))
value = str(field['value']) if title and value:
plain += title + ': ' + value + '\n' plain += title + ': ' + value + '\n'
html += '<b>' + title + '</b>: ' + value + '<br/>\n' html += '<b>' + title + '</b>: ' + value + '<br/>\n'
html += '</font>' if color else ''
html += '</font>' if color else ''
if plain and html: if plain and html:
json = {'text':plain,'html':html} if username:
json = {'text':plain,'html':html,'username':username}
else:
json = {'text':plain,'html':html}
print('Sending hookshot: ' + str(json)) print('Sending hookshot: ' + str(json))
r = requests.post(url + hook, json=json) r = requests.post(url + hook, json=json)
else: else:
...@@ -53,7 +57,7 @@ def slack(hook): ...@@ -53,7 +57,7 @@ def slack(hook):
def grafana(hook): def grafana(hook):
plain = '' plain = ''
html = '' html = ''
incoming = dict(request.json) incoming = request.json
print('Got incoming /grafana hook: ' + str(incoming)) print('Got incoming /grafana hook: ' + str(incoming))
title = str(incoming.get('title', '')) title = str(incoming.get('title', ''))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment