Skip to content

Commit

Permalink
Fixed errors due to slack rtm.start deprecation
Browse files Browse the repository at this point in the history
  • Loading branch information
lins05 committed Sep 28, 2022
1 parent 2ffb360 commit bfdbef9
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
local_settings.py
local_test_settings.py
slackbot_settings.py
!tests/functional/slackbot_settings.py
slackbot_test_settings.py
/build
/dist
Expand Down
6 changes: 3 additions & 3 deletions scripts/slackbot-test-ctl
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ init_proxychains() {
cat >$ssconfig <<EOF
{
"server":"127.0.0.1",
"server_port":8800,
"server_port":8899,
"local_address": "127.0.0.1",
"local_port":1800,
"local_port":1899,
"password":"password",
"timeout":600,
"method":"aes-256-cfb"
Expand All @@ -26,7 +26,7 @@ remote_dns_subnet 224
tcp_read_time_out 15000
tcp_connect_time_out 8000
[ProxyList]
socks5 127.0.0.1 1800
socks5 127.0.0.1 1899
EOF
}

Expand Down
31 changes: 26 additions & 5 deletions slackbot/slackclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,23 @@

logger = logging.getLogger(__name__)

def webapi_generic_list(webapi, resource_key, response_key):
"""Generic <foo>.list request, where <foo> could be users, chanels,
etc."""
ret = []
next_cursor = None
while True:
args = {}
if next_cursor:
args['cursor'] = next_cursor
response = getattr(webapi, resource_key).list(**args)
ret.extend(response.body.get(response_key))

next_cursor = response.body.get('response_metadata', {}).get('next_cursor')
if not next_cursor:
break
logging.info('Getting next page for %s (%s collected)', resource_key, len(ret))
return ret

class SlackClient(object):
def __init__(self, token, timeout=None, bot_icon=None, bot_emoji=None, connect=True,
Expand Down Expand Up @@ -57,14 +74,18 @@ def reconnect(self):
logger.exception('failed to reconnect: %s', e)
time.sleep(5)

def list_users(self):
return webapi_generic_list(self.webapi, 'users', 'members')

def list_channels(self):
return webapi_generic_list(self.webapi, 'conversations', 'channels')

def parse_slack_login_data(self, login_data):
self.login_data = login_data
self.domain = self.login_data['team']['domain']
self.username = self.login_data['self']['name']
self.parse_user_data(login_data['users'])
self.parse_channel_data(login_data['channels'])
self.parse_channel_data(login_data['groups'])
self.parse_channel_data(login_data['ims'])
self.parse_user_data(self.list_users())
self.parse_channel_data(self.list_channels())

proxy, proxy_port, no_proxy = get_http_proxy(os.environ)

Expand Down Expand Up @@ -153,7 +174,7 @@ def get_channel(self, channel_id):
return Channel(self, self.channels[channel_id])

def open_dm_channel(self, user_id):
return self.webapi.im.open(user_id).body["channel"]["id"]
return self.webapi.conversations.open(users=user_id).body["channel"]["id"]

def find_channel_by_name(self, channel_name):
for channel_id, channel in iteritems(self.channels):
Expand Down
6 changes: 2 additions & 4 deletions tests/functional/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,7 @@ def _has_got_message(self, channel, match, start=None, end=None):
match = six.text_type(r'\<@{}\>: {}').format(self.driver_userid, match)
oldest = start or self._start_ts
latest = end or time.time()
func = self.slacker.channels.history if channel.startswith('C') \
else self.slacker.im.history
response = func(channel, oldest=oldest, latest=latest)
response = self.slacker.conversations.history(channel=channel, oldest=oldest, latest=latest)
for msg in response.body['messages']:
if msg['type'] == 'message' and re.match(match, msg['text'], re.DOTALL):
return True
Expand Down Expand Up @@ -217,7 +215,7 @@ def _rtm_read_forever(self):

def _start_dm_channel(self):
"""Start a slack direct messages channel with the test bot"""
response = self.slacker.im.open(self.testbot_userid)
response = self.slacker.conversations.open(users=self.testbot_userid)
self.dm_chan = response.body['channel']['id']

def _is_testbot_online(self):
Expand Down

0 comments on commit bfdbef9

Please sign in to comment.