diff options
Diffstat (limited to 'warmachine/connections')
| -rw-r--r-- | warmachine/connections/slack.py | 44 |
1 files changed, 28 insertions, 16 deletions
diff --git a/warmachine/connections/slack.py b/warmachine/connections/slack.py index 845b5c8..0ef39dc 100644 --- a/warmachine/connections/slack.py +++ b/warmachine/connections/slack.py | |||
| @@ -78,24 +78,9 @@ class SlackWS(Connection): | |||
| 78 | """ | 78 | """ |
| 79 | Say something in the provided channel or IM by id | 79 | Say something in the provided channel or IM by id |
| 80 | """ | 80 | """ |
| 81 | |||
| 82 | # If the destination is a user, figure out the DM channel id | 81 | # If the destination is a user, figure out the DM channel id |
| 83 | if destination_id.startswith('U'): | 82 | if destination_id.startswith('U'): |
| 84 | url = 'https://slack.com/api/im.open?{}'.format(urlencode({ | 83 | destination_id = self.get_dm_id_by_user(destination_id) |
| 85 | 'token': self.token, | ||
| 86 | 'user': destination_id, | ||
| 87 | })) | ||
| 88 | |||
| 89 | req = urllib.request.Request(url) | ||
| 90 | r = urllib.request.urlopen(req).read().decode('utf-8') | ||
| 91 | |||
| 92 | data = json.loads(r) | ||
| 93 | |||
| 94 | if not data['ok']: | ||
| 95 | raise Exception(data) | ||
| 96 | return | ||
| 97 | |||
| 98 | destination_id = data['channel']['id'] | ||
| 99 | 84 | ||
| 100 | await self._send(json.dumps({ | 85 | await self._send(json.dumps({ |
| 101 | 'id': 1, # TODO: this should be a get_msgid call or something | 86 | 'id': 1, # TODO: this should be a get_msgid call or something |
| @@ -224,6 +209,33 @@ class SlackWS(Connection): | |||
| 224 | )) | 209 | )) |
| 225 | self.user_map[msg['user']]['presence'] = msg['presence'] | 210 | self.user_map[msg['user']]['presence'] = msg['presence'] |
| 226 | 211 | ||
| 212 | def get_dm_id_by_user(self, user_id): | ||
| 213 | """ | ||
| 214 | Return the channel id for a direct message to a specific user. | ||
| 215 | |||
| 216 | Args: | ||
| 217 | user_id (str): slack user id | ||
| 218 | |||
| 219 | Return: | ||
| 220 | str: DM channel id for the provided user. None on error | ||
| 221 | """ | ||
| 222 | url = 'https://slack.com/api/im.open?{}'.format(urlencode({ | ||
| 223 | 'token': self.token, | ||
| 224 | 'user': user_id, | ||
| 225 | })) | ||
| 226 | |||
| 227 | req = urllib.request.Request(url) | ||
| 228 | r = urllib.request.urlopen(req).read().decode('utf-8') | ||
| 229 | |||
| 230 | data = json.loads(r) | ||
| 231 | |||
| 232 | if not data['ok']: | ||
| 233 | raise Exception(data) | ||
| 234 | return | ||
| 235 | |||
| 236 | return data['channel']['id'] | ||
| 237 | |||
| 238 | |||
| 227 | def get_users_by_channel(self, channel): | 239 | def get_users_by_channel(self, channel): |
| 228 | url = 'https://slack.com/api/groups.info?{}'.format(urlencode( | 240 | url = 'https://slack.com/api/groups.info?{}'.format(urlencode( |
| 229 | { | 241 | { |