mirror of
https://github.com/ansible/awx.git
synced 2026-02-26 23:46:05 -03:30
Merge pull request #2126 from walkafwalka/slack_backend_web_api
Standardize Slack backend API method
This commit is contained in:
@@ -1,7 +1,6 @@
|
|||||||
# Copyright (c) 2016 Ansible, Inc.
|
# Copyright (c) 2016 Ansible, Inc.
|
||||||
# All Rights Reserved.
|
# All Rights Reserved.
|
||||||
|
|
||||||
import time
|
|
||||||
import logging
|
import logging
|
||||||
from slackclient import SlackClient
|
from slackclient import SlackClient
|
||||||
|
|
||||||
@@ -26,40 +25,8 @@ class SlackBackend(AWXBaseEmailBackend):
|
|||||||
self.color = None
|
self.color = None
|
||||||
if hex_color.startswith("#") and (len(hex_color) == 4 or len(hex_color) == 7):
|
if hex_color.startswith("#") and (len(hex_color) == 4 or len(hex_color) == 7):
|
||||||
self.color = hex_color
|
self.color = hex_color
|
||||||
self.connection = None
|
|
||||||
|
|
||||||
def open(self):
|
|
||||||
if self.connection is not None:
|
|
||||||
return False
|
|
||||||
self.connection = SlackClient(self.token)
|
|
||||||
if not self.connection.rtm_connect():
|
|
||||||
if not self.fail_silently:
|
|
||||||
raise Exception("Slack Notification Token is invalid")
|
|
||||||
|
|
||||||
start = time.time()
|
|
||||||
time.clock()
|
|
||||||
elapsed = 0
|
|
||||||
while elapsed < WEBSOCKET_TIMEOUT:
|
|
||||||
events = self.connection.rtm_read()
|
|
||||||
if any(event['type'] == 'hello' for event in events):
|
|
||||||
return True
|
|
||||||
elapsed = time.time() - start
|
|
||||||
time.sleep(0.5)
|
|
||||||
|
|
||||||
raise RuntimeError("Slack Notification unable to establish websocket connection after {} seconds".format(WEBSOCKET_TIMEOUT))
|
|
||||||
|
|
||||||
def close(self):
|
|
||||||
if self.connection is None:
|
|
||||||
return
|
|
||||||
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)
|
connection = SlackClient(self.token)
|
||||||
sent_messages = 0
|
sent_messages = 0
|
||||||
for m in messages:
|
for m in messages:
|
||||||
@@ -67,12 +34,17 @@ class SlackBackend(AWXBaseEmailBackend):
|
|||||||
for r in m.recipients():
|
for r in m.recipients():
|
||||||
if r.startswith('#'):
|
if r.startswith('#'):
|
||||||
r = r[1:]
|
r = r[1:]
|
||||||
ret = connection.api_call("chat.postMessage",
|
if self.color:
|
||||||
channel=r,
|
ret = connection.api_call("chat.postMessage",
|
||||||
attachments=[{
|
channel=r,
|
||||||
"color": self.color,
|
attachments=[{
|
||||||
"text": m.subject
|
"color": self.color,
|
||||||
}])
|
"text": m.subject
|
||||||
|
}])
|
||||||
|
else:
|
||||||
|
ret = connection.api_call("chat.postMessage",
|
||||||
|
channel=r,
|
||||||
|
text=m.subject)
|
||||||
logger.debug(ret)
|
logger.debug(ret)
|
||||||
if ret['ok']:
|
if ret['ok']:
|
||||||
sent_messages += 1
|
sent_messages += 1
|
||||||
@@ -83,20 +55,3 @@ class SlackBackend(AWXBaseEmailBackend):
|
|||||||
if not self.fail_silently:
|
if not self.fail_silently:
|
||||||
raise
|
raise
|
||||||
return sent_messages
|
return sent_messages
|
||||||
|
|
||||||
def _send_rtm_messages(self, messages):
|
|
||||||
if self.connection is None:
|
|
||||||
self.open()
|
|
||||||
sent_messages = 0
|
|
||||||
for m in messages:
|
|
||||||
try:
|
|
||||||
for r in m.recipients():
|
|
||||||
if r.startswith('#'):
|
|
||||||
r = r[1:]
|
|
||||||
self.connection.rtm_send_message(r, m.subject)
|
|
||||||
sent_messages += 1
|
|
||||||
except Exception as e:
|
|
||||||
logger.error(smart_text(_("Exception sending messages: {}").format(e)))
|
|
||||||
if not self.fail_silently:
|
|
||||||
raise
|
|
||||||
return sent_messages
|
|
||||||
|
|||||||
Reference in New Issue
Block a user