mirror of
https://github.com/ansible/awx.git
synced 2026-03-04 10:11:05 -03:30
support slack color notification #1490
Signed-off-by: paihu <paihu_j@yahoo.co.jp>
This commit is contained in:
@@ -16,13 +16,17 @@ WEBSOCKET_TIMEOUT = 30
|
|||||||
class SlackBackend(AWXBaseEmailBackend):
|
class SlackBackend(AWXBaseEmailBackend):
|
||||||
|
|
||||||
init_parameters = {"token": {"label": "Token", "type": "password"},
|
init_parameters = {"token": {"label": "Token", "type": "password"},
|
||||||
|
"hex_color": {"label": "Notification Color", "type": "string"},
|
||||||
"channels": {"label": "Destination Channels", "type": "list"}}
|
"channels": {"label": "Destination Channels", "type": "list"}}
|
||||||
recipient_parameter = "channels"
|
recipient_parameter = "channels"
|
||||||
sender_parameter = None
|
sender_parameter = None
|
||||||
|
|
||||||
def __init__(self, token, fail_silently=False, **kwargs):
|
def __init__(self, token, hex_color, fail_silently=False, **kwargs):
|
||||||
super(SlackBackend, self).__init__(fail_silently=fail_silently)
|
super(SlackBackend, self).__init__(fail_silently=fail_silently)
|
||||||
self.token = token
|
self.token = token
|
||||||
|
self.color = None
|
||||||
|
if hex_color.startswith("#") and (len(hex_color) == 4 or len(hex_color) == 7):
|
||||||
|
self.color = hex_color
|
||||||
self.connection = None
|
self.connection = None
|
||||||
|
|
||||||
def open(self):
|
def open(self):
|
||||||
@@ -51,6 +55,37 @@ class SlackBackend(AWXBaseEmailBackend):
|
|||||||
self.connection = None
|
self.connection = None
|
||||||
|
|
||||||
def send_messages(self, messages):
|
def send_messages(self, messages):
|
||||||
|
if self.color:
|
||||||
|
return self._send_attachments(messages)
|
||||||
|
else:
|
||||||
|
return self._send_rtm_messages(messages)
|
||||||
|
|
||||||
|
def _send_attachments(self, messages):
|
||||||
|
connection = SlackClient(self.token)
|
||||||
|
sent_messages = 0
|
||||||
|
for m in messages:
|
||||||
|
try:
|
||||||
|
for r in m.recipients():
|
||||||
|
if r.startswith('#'):
|
||||||
|
r = r[1:]
|
||||||
|
ret = connection.api_call("chat.postMessage",
|
||||||
|
channel=r,
|
||||||
|
attachments=[{
|
||||||
|
"color": self.color,
|
||||||
|
"text": m.subject
|
||||||
|
}])
|
||||||
|
logger.debug(ret)
|
||||||
|
if ret['ok']:
|
||||||
|
sent_messages += 1
|
||||||
|
else:
|
||||||
|
raise RuntimeError("Slack Notification unable to send {}: {}".format(r, m.subject))
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(smart_text(_("Exception sending messages: {}").format(e)))
|
||||||
|
if not self.fail_silently:
|
||||||
|
raise
|
||||||
|
return sent_messages
|
||||||
|
|
||||||
|
def _send_rtm_messages(self, messages):
|
||||||
if self.connection is None:
|
if self.connection is None:
|
||||||
self.open()
|
self.open()
|
||||||
sent_messages = 0
|
sent_messages = 0
|
||||||
|
|||||||
@@ -435,6 +435,15 @@ export default ['i18n', function(i18n) {
|
|||||||
ngShow: "notification_type.value == 'email'",
|
ngShow: "notification_type.value == 'email'",
|
||||||
labelClass: 'NotificationsForm-radioButtons'
|
labelClass: 'NotificationsForm-radioButtons'
|
||||||
}]
|
}]
|
||||||
|
},
|
||||||
|
hex_color: {
|
||||||
|
label: i18n._('Notification Color'),
|
||||||
|
dataTitle: i18n._('Notification Color'),
|
||||||
|
type: 'text',
|
||||||
|
subForm: 'typeSubForm',
|
||||||
|
ngShow: "notification_type.value == 'slack' ",
|
||||||
|
ngDisabled: '!(notification_template.summary_fields.user_capabilities.edit || canAdd)',
|
||||||
|
awPopOver: i18n._('Specify a notification color. Acceptable colors are hex color code (example: #3af or #789abc) .')
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
@@ -2832,6 +2832,8 @@ msgstr ""
|
|||||||
|
|
||||||
#: client/src/notifications/notificationTemplates.form.js:288
|
#: client/src/notifications/notificationTemplates.form.js:288
|
||||||
#: client/src/notifications/notificationTemplates.form.js:289
|
#: client/src/notifications/notificationTemplates.form.js:289
|
||||||
|
#: client/src/notifications/notificationTemplates.form.js:440
|
||||||
|
#: client/src/notifications/notificationTemplates.form.js:441
|
||||||
msgid "Notification Color"
|
msgid "Notification Color"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
@@ -5420,3 +5422,7 @@ msgstr ""
|
|||||||
#: client/src/shared/paginate/paginate.partial.html:55
|
#: client/src/shared/paginate/paginate.partial.html:55
|
||||||
msgid "{{pageSize}}"
|
msgid "{{pageSize}}"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
|
#: client/src/notifications/notificationTemplates.form.js:445
|
||||||
|
msgid "Specify a notification color. Acceptable colors are hex color code (example: #3af or #789abc) ."
|
||||||
|
msgstr ""
|
||||||
|
|||||||
Reference in New Issue
Block a user