Skip to content

Commit c71b757

Browse files
author
rohitcoder
committed
Added onlyArchived option for slack
1 parent d06292b commit c71b757

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

connection.yml.sample

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ sources:
139139
slack_example:
140140
channel_types: "public_channel,private_channel"
141141
token: xoxp-XXXXXXXXXXXXXXXXXXXXXXXXX
142+
onlyArchived: False ## By default False, set to True if you want to scan archived channels only
142143
archived_channels: True ## By default False, set to True if you want to scan archived channels also
143144
limit_mins: 15 ## By default 60 mins
144145
read_from: last_message ## By default current Unix timestamp, available options - UNIX Timestamp (e..g: 1737354387), last_message

hawk_scanner/commands/slack.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def connect_slack(args, token):
2222
system.print_error(args, f"Failed to connect to Slack with error: {e.response['error']}")
2323
return None
2424

25-
def check_slack_messages(args, client, patterns, profile_name, channel_types, isExternal, read_from, channel_ids=None, limit_mins=60, archived_channels=False):
25+
def check_slack_messages(args, client, patterns, profile_name, channel_types, isExternal, read_from, channel_ids=None, limit_mins=60, archived_channels=False, onlyArchived=False):
2626
results = []
2727
try:
2828
connection = system.get_connection(args)
@@ -59,6 +59,8 @@ def rate_limit_retry(func, *args, **kwargs):
5959
cursor = None
6060
while True:
6161
try:
62+
if onlyArchived:
63+
archived_channels = True
6264
if archived_channels:
6365
system.print_debug(args, f"Considering archived channels, you may want to set archived_channels to False")
6466
else:
@@ -71,7 +73,11 @@ def rate_limit_retry(func, *args, **kwargs):
7173
cursor=cursor,
7274
exclude_archived=not archived_channels
7375
)
74-
channels.extend(response.get("channels", []))
76+
if onlyArchived:
77+
system.print_info(args, "Getting only archived channels....")
78+
channels.extend([channel for channel in response.get("channels", []) if channel.get("is_archived")])
79+
else:
80+
channels.extend(response.get("channels", []))
7581
# Update the cursor for the next batch
7682
cursor = response.get("response_metadata", {}).get("next_cursor")
7783

@@ -355,8 +361,9 @@ def execute(args):
355361
channel_ids = config.get('channel_ids', [])
356362
limit_mins = config.get('limit_mins', 60)
357363
isExternal = config.get('isExternal', None)
364+
onlyArchived = config.get('onlyArchived', False)
358365
archived_channels = config.get('archived_channels', False)
359-
366+
360367
if token:
361368
system.print_info(args, f"Checking Slack Profile {key}")
362369
else:
@@ -365,7 +372,7 @@ def execute(args):
365372

366373
client = connect_slack(args, token)
367374
if client:
368-
results += check_slack_messages(args, client, patterns, key, channel_types, isExternal, read_from, channel_ids, limit_mins, archived_channels)
375+
results += check_slack_messages(args, client, patterns, key, channel_types, isExternal, read_from, channel_ids, limit_mins, archived_channels, onlyArchived)
369376
else:
370377
system.print_error(args, "No Slack connection details found in connection.yml")
371378
else:

0 commit comments

Comments
 (0)