@@ -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 , 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 ):
2626 results = []
2727 try :
2828 team_info = client .team_info ()
@@ -83,9 +83,27 @@ def rate_limit_retry(func, *args, **kwargs):
8383 system .print_debug (args , f"Skipping archived channel: { channel_id } " )
8484 except SlackApiError as e :
8585 system .print_error (args , f"Failed to fetch channel with id { channel_id } with error: { e .response ['error' ]} " )
86-
86+ system .print_info (args , f"Found { len (channels )} channels" )
87+ filtered_channels = []
88+ for channel in channels :
89+ channel_is_external = channel .get ("is_ext_shared" )
90+
91+ if isExternal is not None :
92+ if isExternal and not channel_is_external :
93+ system .print_debug (args , f"Skipping non-external channel: { channel ['name' ]} " )
94+ continue # Skip this channel
95+ elif not isExternal and channel_is_external :
96+ system .print_debug (args , f"Skipping external channel: { channel ['name' ]} " )
97+ continue # Skip this channel
98+
99+ if isExternal and channel_is_external :
100+ system .print_info (args , f"Found external channel: { channel ['name' ]} " )
101+
102+ filtered_channels .append (channel ) # Add the channel if it wasn't skipped
103+ if filtered_channels .__len__ () > 0 :
104+ channels = filtered_channels # Update the original list
87105 # Optional: Print or log the total number of channels fetched
88- system .print_info (args , f"Total channels fetched : { len (channels )} " )
106+ system .print_info (args , f"Total channels to scan after filteration : { len (channels )} " )
89107 system .print_info (args , f"Found { len (channels )} channels of type { channel_types } " )
90108 system .print_debug (args , f"Checking messages in channels: { ', ' .join ([channel ['name' ] for channel in channels ])} " )
91109
@@ -95,7 +113,7 @@ def rate_limit_retry(func, *args, **kwargs):
95113 latest_time = int (time .time ())
96114
97115 if read_from == 'last_message' :
98- system .print_info (args , "Fetching messages from the last message in the channel" )
116+ system .print_info (args , "Fetching messages from the last message in the channel " + channel_name )
99117 last_msg = get_last_msg (args , client , channel_id )
100118 if last_msg :
101119 latest_time = float (last_msg ['timestamp' ])
@@ -117,7 +135,7 @@ def rate_limit_retry(func, *args, **kwargs):
117135 system .print_info (args , f"Checking messages in channel { channel_name } ({ channel_id } )" )
118136 system .print_info (args , f"Fetching messages from { time .strftime ('%Y-%m-%d %H:%M:%S' , time .gmtime (oldest_time ))} to { time .strftime ('%Y-%m-%d %H:%M:%S' , time .gmtime (latest_time ))} " )
119137 messages = rate_limit_retry (client .conversations_history , channel = channel_id , oldest = oldest_time , latest = latest_time )["messages" ]
120- print ( f"Found { len (messages )} messages in channel { channel_name } ({ channel_id } )" )
138+ system . print_debug ( args , f"Found { len (messages )} messages in channel { channel_name } ({ channel_id } )" )
121139 for message in messages :
122140 user = message .get ("user" , "" )
123141 text = message .get ("text" )
@@ -305,6 +323,7 @@ def execute(args):
305323 channel_types = config .get ('channel_types' , "public_channel,private_channel" )
306324 channel_ids = config .get ('channel_ids' , [])
307325 limit_mins = config .get ('limit_mins' , 60 )
326+ isExternal = config .get ('isExternal' , None )
308327 archived_channels = config .get ('archived_channels' , False )
309328
310329 if token :
@@ -315,7 +334,7 @@ def execute(args):
315334
316335 client = connect_slack (args , token )
317336 if client :
318- results += check_slack_messages (args , client , patterns , key , channel_types , read_from , channel_ids , limit_mins , archived_channels )
337+ results += check_slack_messages (args , client , patterns , key , channel_types , isExternal , read_from , channel_ids , limit_mins , archived_channels )
319338 else :
320339 system .print_error (args , "No Slack connection details found in connection.yml" )
321340 else :
0 commit comments