1
|
|
|
import urllib |
2
|
|
|
|
3
|
|
|
import requests |
4
|
|
|
|
5
|
|
|
from st2actions.runners.pythonrunner import Action |
6
|
|
|
|
7
|
|
|
__all__ = [ |
8
|
|
|
'SendInviteAction' |
9
|
|
|
] |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
class SendInviteAction(Action): |
13
|
|
|
def run(self, email, channels, first_name, token, set_active, attempts): |
14
|
|
|
token = token if token else self.config['admin']['admin_token'] |
15
|
|
|
set_active = set_active if set_active else self.config['admin']['set_active'] |
16
|
|
|
attempts = attempts if attempts else self.config['admin']['attempts'] |
17
|
|
|
auto_join_channels = self.config['admin']['auto_join_channels'] |
18
|
|
|
url = "https://%s.slack.com/api/users.admin.invite" % \ |
19
|
|
|
self.config['admin']['organization'] |
20
|
|
|
|
21
|
|
|
headers = {} |
22
|
|
|
headers['Content-Type'] = 'application/x-www-form-urlencoded' |
23
|
|
|
body = { |
24
|
|
|
'email': email.encode('utf-8'), |
25
|
|
|
'channels': " ".join(auto_join_channels), |
26
|
|
|
'first_name': first_name.encode('utf-8'), |
27
|
|
|
'token': token, |
28
|
|
|
'set_active': set_active, |
29
|
|
|
'_attempts': attempts |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
data = urllib.urlencode(body) |
33
|
|
|
response = requests.get(url=url, |
34
|
|
|
headers=headers, params=data) |
35
|
|
|
results = response.json() |
36
|
|
|
|
37
|
|
|
if results['ok'] is True: |
38
|
|
|
return 'Invite successfully sent to %s. RESPONSE: %s' % \ |
39
|
|
|
(email, results) |
40
|
|
|
else: |
41
|
|
|
failure_reason = ('Failed to send invite to %s: %s \ |
42
|
|
|
(status code: %s)' % (email, response.text, |
43
|
|
|
response.status_code)) |
44
|
|
|
self.logger.exception(failure_reason) |
45
|
|
|
raise Exception(failure_reason) |
46
|
|
|
|
47
|
|
|
return True |
48
|
|
|
|