1- import time
1+ import time , os , requests
22from datetime import datetime
33from slack_sdk import WebClient
44from slack_sdk .errors import SlackApiError
@@ -63,12 +63,31 @@ def check_slack_messages(args, client, patterns, profile_name, channel_types, ch
6363 # Get messages from the channel within the time range
6464 system .print_info (args , f"Checking messages in channel { channel_name } ({ channel_id } )" )
6565 messages = client .conversations_history (channel = channel_id , oldest = oldest_time )["messages" ]
66-
6766 for message in messages :
6867 user = message .get ("user" , "" )
6968 text = message .get ("text" )
7069 message_ts = message .get ("ts" )
71-
70+ files = message .get ("files" , [])
71+ for file in files :
72+ folder_path = f"data/slack/"
73+ file_addr = download_file (args , client , file , folder_path )
74+ if file_addr :
75+ system .print_debug (args , f"Checking file: { file_addr } " )
76+ matches = system .read_match_strings (args , file_addr , 'slack' )
77+ if matches :
78+ for match in matches :
79+ results .append ({
80+ 'channel_id' : channel_id ,
81+ 'channel_name' : channel_name ,
82+ 'user' : user ,
83+ 'pattern_name' : match ['pattern_name' ],
84+ 'matches' : list (set (match ['matches' ])),
85+ 'sample_text' : match ['sample_text' ],
86+ 'profile' : profile_name ,
87+ 'message_link' : workspace_url + f"/archives/{ channel_id } /p{ message_ts .replace ('.' , '' )} " ,
88+ 'data_source' : 'slack'
89+ })
90+
7291 # Check main message for matches
7392 if text :
7493 matches = system .match_strings (args , text )
@@ -86,20 +105,36 @@ def check_slack_messages(args, client, patterns, profile_name, channel_types, ch
86105 'data_source' : 'slack'
87106 })
88107
89- # Check for replies (threads)
90108 if "thread_ts" in message :
91109 thread_ts = message ["thread_ts" ]
92-
93- # Fetch replies for the thread
94110 replies = client .conversations_replies (channel = channel_id , ts = thread_ts , oldest = oldest_time )["messages" ]
95-
96- # Exclude parent message and check replies
97111 for reply in replies :
98112 if reply ["ts" ] != thread_ts : # Skip the parent message
99113 reply_user = reply .get ("user" , "" )
100114 reply_text = reply .get ("text" )
101115 reply_ts = reply .get ("ts" )
102116
117+ reply_files = reply .get ("files" , [])
118+ for file in reply_files :
119+ folder_path = f"data/slack/"
120+ file_addr = download_file (args , client , file , folder_path )
121+ if file_addr :
122+ system .print_debug (args , f"Checking file: { file_addr } " )
123+ matches = system .read_match_strings (args , file_addr , 'slack' )
124+ if matches :
125+ for match in matches :
126+ results .append ({
127+ 'channel_id' : channel_id ,
128+ 'channel_name' : channel_name ,
129+ 'user' : reply_user ,
130+ 'pattern_name' : match ['pattern_name' ],
131+ 'matches' : list (set (match ['matches' ])),
132+ 'sample_text' : match ['sample_text' ],
133+ 'profile' : profile_name ,
134+ 'message_link' : workspace_url + f"/archives/{ channel_id } /p{ reply_ts .replace ('.' , '' )} " ,
135+ 'data_source' : 'slack'
136+ })
137+
103138 if reply_text :
104139 reply_matches = system .match_strings (args , reply_text )
105140 if reply_matches :
@@ -119,9 +154,46 @@ def check_slack_messages(args, client, patterns, profile_name, channel_types, ch
119154 return results
120155
121156 except SlackApiError as e :
122- system .print_error (args , f"Failed to fetch messages from Slack with error: { e . response [ 'error' ] } " )
157+ system .print_error (args , f"Failed to fetch messages from Slack with error: { e } " )
123158 return results
124159
160+
161+
162+ def download_file (args , client , file_info , folder_path ) -> str :
163+ try :
164+ # Ensure the folder exists
165+ os .makedirs (folder_path , exist_ok = True )
166+
167+ # Use the Slack client to get file info
168+ file_url = file_info ['url_private_download' ]
169+ file_name = file_info ['name' ]
170+
171+ # Create the full path to save the file
172+ file_path = os .path .join (folder_path , file_name )
173+
174+ # Send a GET request to download the file
175+ system .print_debug (args , f"Downloading file: { file_url } " )
176+ response = requests .get (file_url , headers = {f'Authorization' : f"Bearer { client .token } " })
177+ if response .status_code == 200 :
178+ with open (file_path , 'wb' ) as f :
179+ f .write (response .content )
180+ return file_path
181+ else :
182+ # Log error if the status code is not 200
183+ system .print_error (args , f"Failed to download file with status code: { response .status_code } " )
184+ return None
185+
186+ except SlackApiError as e :
187+ # Handle Slack API-specific errors
188+ system .print_error (args , f"Failed to download file with error: { e .response ['error' ]} " )
189+ return None
190+
191+ except Exception as e :
192+ # Handle any other exceptions
193+ system .print_error (args , f"An unexpected error occurred: { str (e )} " )
194+ return None
195+
196+
125197def execute (args ):
126198 results = []
127199 system .print_info (args , "Running Checks for Slack Sources" )
0 commit comments