@@ -69,6 +69,7 @@ def run(self):
6969 .lower ()
7070 )
7171 channel_name = channel_name [:80 ]
72+ self .channel_name = channel_name
7273
7374 headers = {
7475 "Authorization" : f"Bearer { self .slack_token } " ,
@@ -192,49 +193,72 @@ def format_tlp(level):
192193 case_id = self .get_param ("data.caseId" )
193194 case_unique_id = self .get_param ("data.id" )
194195
195- # Channel name format: case-CASEID
196- channel_name = (
197- f"{ self .channel_prefix } { case_id } " .replace (" " , "-" )
198- .replace ("." , "" )
199- .replace ("," , "" )
200- .lower ()
201- )
202- channel_name = channel_name [:80 ]
196+ # Collect all channel names from tags
197+ channel_names = []
198+ case_tags = self .get_param ("data.tags" , [])
199+ for tag in case_tags :
200+ if tag .startswith ("slack:" ):
201+ channel_names .append (tag [6 :]) # Remove "slack:" prefix
202+
203+ # Fallback to reconstructing channel name if no tags found
204+ if not channel_names :
205+ channel_name = (
206+ f"{ self .channel_prefix } { case_id } " .replace (" " , "-" )
207+ .replace ("." , "" )
208+ .replace ("," , "" )
209+ .lower ()
210+ )
211+ channel_name = channel_name [:80 ]
212+ channel_names .append (channel_name )
203213
204214 headers = {
205215 "Authorization" : f"Bearer { self .slack_token } " ,
206216 "Content-Type" : "application/json" ,
207217 }
208218
209- # Find the channel
210- channel_id = self .find_existing_channel (channel_name , headers )
211- if not channel_id :
212- self .error (
213- f"Channel '{ channel_name } ' not found. Create the channel first."
214- )
219+ # Sync all channels
220+ synced_channels = []
221+ errors = []
215222
216- # Get channel conversation history
217- conversation_data = self .get_channel_conversations (channel_id , headers )
223+ for channel_name in channel_names :
224+ try :
225+ # Find the channel
226+ channel_id = self .find_existing_channel (channel_name , headers )
227+ if not channel_id :
228+ errors .append (f"Channel '{ channel_name } ' not found" )
229+ continue
230+
231+ # Get channel conversation history
232+ conversation_data = self .get_channel_conversations (channel_id , headers )
233+
234+ # Create or update TheHive task with conversation data
235+ task_id , action = self .create_or_update_thehive_task (
236+ case_unique_id , channel_name , conversation_data
237+ )
218238
219- # Create or update TheHive task with conversation data
220- task_id , action = self .create_or_update_thehive_task (
221- case_unique_id , channel_name , conversation_data
222- )
239+ synced_channels .append ({
240+ "channel_name" : channel_name ,
241+ "channel_id" : channel_id ,
242+ "task_id" : task_id ,
243+ "action" : action
244+ })
245+
246+ except Exception as e :
247+ errors .append (f"Error syncing '{ channel_name } ': { str (e )} " )
248+
249+ # Build report
250+ if not synced_channels and errors :
251+ self .error (f"Failed to sync channels: { ', ' .join (errors )} " )
223252
224- # Include debug info in the main report
225253 report_data = {
226- "channel_name" : channel_name ,
227- "channel_id" : channel_id ,
228- "task_id" : task_id ,
229- "action" : action ,
230- "message" : f"Slack channel `{ channel_name } ` conversation { action } in TheHive task { task_id } ." ,
254+ "synced_channels" : synced_channels ,
255+ "total_synced" : len (synced_channels ),
256+ "message" : f"Synced { len (synced_channels )} channel(s)"
231257 }
232-
233- # Add debug info if we have it
234- if hasattr (self , '_last_debug_info' ):
235- report_data ["debug_info" ] = self ._last_debug_info
236- delattr (self , '_last_debug_info' )
237-
258+
259+ if errors :
260+ report_data ["errors" ] = errors
261+
238262 self .report (report_data )
239263
240264 def get_channel_conversations (self , channel_id , headers ):
@@ -705,6 +729,17 @@ def create_or_update_thehive_task(self, case_id, channel_name, conversations):
705729
706730 except Exception as e :
707731 self .error (f"Failed to create task: { str (e )} " )
732+
733+ def operations (self , raw ):
734+ artifacts = []
735+ # AddTagToArtifact ({ "type": "AddTagToArtifact", "tag": "tag to add" }): add a tag to the artifact related to the object
736+ # AddTagToCase ({ "type": "AddTagToCase", "tag": "tag to add" }): add a tag to the case related to the object
737+ # MarkAlertAsRead: mark the alert related to the object as read
738+ # AddCustomFields ({"name": "key", "value": "value", "tpe": "type"): add a custom field to the case related to the object
739+ if self .service == "createchannel" :
740+ if hasattr (self , 'channel_name' ):
741+ artifacts .append (self .build_operation ("AddTagToCase" , tag = f"slack:{ self .channel_name } " ))
742+ return artifacts
708743
709744
710745if __name__ == "__main__" :
0 commit comments