diff options
Diffstat (limited to 'warmachine/connections')
| -rw-r--r-- | warmachine/connections/base.py | 7 | ||||
| -rw-r--r-- | warmachine/connections/slack.py | 44 |
2 files changed, 50 insertions, 1 deletions
diff --git a/warmachine/connections/base.py b/warmachine/connections/base.py index e68e9b9..4df5996 100644 --- a/warmachine/connections/base.py +++ b/warmachine/connections/base.py | |||
| @@ -26,6 +26,13 @@ class Connection(object): | |||
| 26 | raise NotImplementedError('{} must implement `read` method'.format( | 26 | raise NotImplementedError('{} must implement `read` method'.format( |
| 27 | self.__class__.__name__)) | 27 | self.__class__.__name__)) |
| 28 | 28 | ||
| 29 | def get_users_by_channel(self, channel): | ||
| 30 | """ | ||
| 31 | Return a list of users who are in the provided channel | ||
| 32 | """ | ||
| 33 | raise NotImplementedError('{} must implement `get_users_by_channel` ' | ||
| 34 | 'method'.format(self.__class__.__name__)) | ||
| 35 | |||
| 29 | def id(self): | 36 | def id(self): |
| 30 | """ | 37 | """ |
| 31 | Unique ID for this connection. Since there can be more than one | 38 | Unique ID for this connection. Since there can be more than one |
diff --git a/warmachine/connections/slack.py b/warmachine/connections/slack.py index b1a5390..90903e7 100644 --- a/warmachine/connections/slack.py +++ b/warmachine/connections/slack.py | |||
| @@ -3,6 +3,7 @@ import json | |||
| 3 | import logging | 3 | import logging |
| 4 | from pprint import pformat | 4 | from pprint import pformat |
| 5 | from urllib.parse import urlencode | 5 | from urllib.parse import urlencode |
| 6 | import urllib.request | ||
| 6 | 7 | ||
| 7 | import websockets | 8 | import websockets |
| 8 | 9 | ||
| @@ -95,7 +96,6 @@ class SlackWS(Connection): | |||
| 95 | Returns: | 96 | Returns: |
| 96 | str: websocket url to connect to | 97 | str: websocket url to connect to |
| 97 | """ | 98 | """ |
| 98 | import urllib.request | ||
| 99 | url = 'https://slack.com/api/rtm.start?{}'.format( | 99 | url = 'https://slack.com/api/rtm.start?{}'.format( |
| 100 | urlencode( | 100 | urlencode( |
| 101 | {'token': | 101 | {'token': |
| @@ -121,6 +121,8 @@ class SlackWS(Connection): | |||
| 121 | """ | 121 | """ |
| 122 | if not self._info: | 122 | if not self._info: |
| 123 | return | 123 | return |
| 124 | with open('slack_info.json', 'w') as f: | ||
| 125 | f.write(pformat(self._info)) | ||
| 124 | 126 | ||
| 125 | self.status = CONNECTED | 127 | self.status = CONNECTED |
| 126 | 128 | ||
| @@ -190,6 +192,18 @@ class SlackWS(Connection): | |||
| 190 | )) | 192 | )) |
| 191 | self.user_map[msg['user']]['presence'] = msg['presence'] | 193 | self.user_map[msg['user']]['presence'] = msg['presence'] |
| 192 | 194 | ||
| 195 | def get_users_by_channel(self, channel): | ||
| 196 | url = 'https://slack.com/api/groups.info?{}'.format(urlencode( | ||
| 197 | { | ||
| 198 | 'token': self.token, | ||
| 199 | 'channel': channel, | ||
| 200 | })) | ||
| 201 | self.log.debug(url) | ||
| 202 | req = urllib.request.Request(url) | ||
| 203 | r = urllib.request.urlopen(req).read().decode('utf-8') | ||
| 204 | |||
| 205 | self.log.debug(r) | ||
| 206 | |||
| 193 | async def on_group_join(self, channel): | 207 | async def on_group_join(self, channel): |
| 194 | """ | 208 | """ |
| 195 | The group_joined event is sent to all connections for a user when that | 209 | The group_joined event is sent to all connections for a user when that |
| @@ -279,3 +293,31 @@ class SlackWS(Connection): | |||
| 279 | # 'user': 'U1U05AF5J' | 293 | # 'user': 'U1U05AF5J' |
| 280 | # } | 294 | # } |
| 281 | # } | 295 | # } |
| 296 | |||
| 297 | |||
| 298 | # Invited to a public channel | ||
| 299 | # 2016-07-29 16:23:24,817 [DEBUG] SlackWS: on_channel_joined does not exist for message: {'type': 'channel_joined', 'chan | ||
| 300 | # nel': {'members': ['U0286NL58', 'U1U05AF5J'], 'purpose': {'last_set': 0, 'creator': '', 'value': ''}, 'topic': {'last_s | ||
| 301 | # et': 0, 'creator': '', 'value': ''}, 'is_member': True, 'is_channel': True, 'creator': 'U0286NL58', 'is_archived': Fals | ||
| 302 | # e, 'unread_count_display': 0, 'id': 'C1WJU3ZU0', 'name': 'wm-test2', 'is_general': False, 'created': 1469830985, 'unrea | ||
| 303 | # d_count': 0, 'latest': {'text': '<@U0286NL58|jason> has joined the channel', 'type': 'message', 'user': 'U0286NL58', 's | ||
| 304 | # ubtype': 'channel_join', 'ts': '1469830985.000002'}, 'last_read': '1469830985.000002'}} | ||
| 305 | # 2016-07-29 16:23:24,878 [DEBUG] SlackWS: on_message_channel_join does not exist for message: {'channel': 'C1WJU3ZU0', ' | ||
| 306 | # text': '<@U1U05AF5J|wm-standup-test> has joined the channel', 'type': 'message', 'inviter': 'U0286NL58', 'subtype': 'ch | ||
| 307 | # annel_join', 'user_profile': {'real_name': '', 'name': 'wm-standup-test', 'image_72': 'https://avatars.slack-edge.com/2 | ||
| 308 | # 016-07-21/62015427159_1da65a3cf7a85e85c3cb_72.png', 'first_name': None, 'avatar_hash': '1da65a3cf7a8'}, 'ts': '14698310 | ||
| 309 | # 04.000003', 'user': 'U1U05AF5J', 'team': 'T027XPE12'} | ||
| 310 | |||
| 311 | # Someone else joins a public channel | ||
| 312 | # 2016-07-29 16:26:19,966 [DEBUG] SlackWS: on_message_channel_join does not exist for message: {'type': 'message', 'invit | ||
| 313 | # er': 'U0286NL58', 'ts': '1469831179.000004', 'team': 'T027XPE12', 'user': 'U0286167T', 'channel': 'C1WJU3ZU0', 'user_pr | ||
| 314 | # ofile': {'name': 'synic', 'image_72': 'https://avatars.slack-edge.com/2016-06-24/54136624065_49ec8bc368966c152817_72.jp | ||
| 315 | # g', 'real_name': 'Adam Olsen', 'first_name': 'Adam', 'avatar_hash': '49ec8bc36896'}, 'subtype': 'channel_join', 'text': | ||
| 316 | # '<@U0286167T|synic> has joined the channel'} | ||
| 317 | |||
| 318 | # Invited to a private channel | ||
| 319 | # 2016-07-29 16:27:29,376 [DEBUG] SlackWS: on_message_group_join does not exist for message: {'type': 'message', 'inviter | ||
| 320 | # ': 'U0286NL58', 'ts': '1469831249.000047', 'team': 'T027XPE12', 'user': 'U0286167T', 'channel': 'G1W837CGP', 'user_prof | ||
| 321 | # ile': {'name': 'synic', 'image_72': 'https://avatars.slack-edge.com/2016-06-24/54136624065_49ec8bc368966c152817_72.jpg' | ||
| 322 | # , 'real_name': 'Adam Olsen', 'first_name': 'Adam', 'avatar_hash': '49ec8bc36896'}, 'subtype': 'group_join', 'text': '<@ | ||
| 323 | # U0286167T|synic> has joined the group'} | ||