4848from robusta .core .sinks .sink_base import KeyT
4949from robusta .core .sinks .slack .slack_sink_params import SlackSinkParams
5050from robusta .core .sinks .slack .preview .slack_sink_preview_params import SlackSinkPreviewParams
51-
5251from robusta .core .sinks .transformer import Transformer
5352
5453ACTION_TRIGGER_PLAYBOOK = "trigger_playbook"
@@ -62,7 +61,7 @@ class SlackSender:
6261 verified_api_tokens : Set [str ] = set ()
6362 channel_name_to_id = {}
6463
65- def __init__ (self , slack_token : str , account_id : str , cluster_name : str , signing_key : str , slack_channel : str , is_preview : bool = False ):
64+ def __init__ (self , slack_token : str , account_id : str , cluster_name : str , signing_key : str , slack_channel : str , registry , is_preview : bool = False ):
6665 """
6766 Connect to Slack and verify that the Slack token is valid.
6867 Return True on success, False on failure
@@ -80,6 +79,7 @@ def __init__(self, slack_token: str, account_id: str, cluster_name: str, signing
8079 timeout = SLACK_REQUEST_TIMEOUT ,
8180 retry_handlers = all_builtin_retry_handlers (),
8281 )
82+ self .registry = registry
8383 self .signing_key = signing_key
8484 self .account_id = account_id
8585 self .cluster_name = cluster_name
@@ -394,33 +394,6 @@ def __limit_labels_size(self, labels: dict, max_size: int = 1000) -> dict:
394394
395395 return limited_labels
396396
397- def __create_holmes_callback (self , finding : Finding ) -> CallbackBlock :
398- resource = ResourceInfo (
399- name = finding .subject .name if finding .subject .name else "" ,
400- namespace = finding .subject .namespace ,
401- kind = finding .subject .subject_type .value if finding .subject .subject_type .value else "" ,
402- node = finding .subject .node ,
403- container = finding .subject .container ,
404- )
405-
406- context : Dict [str , Any ] = {
407- "robusta_issue_id" : str (finding .id ),
408- "issue_type" : finding .aggregation_key ,
409- "source" : finding .source .name ,
410- "labels" : self .__limit_labels_size (labels = finding .subject .labels ),
411- }
412-
413- return CallbackBlock (
414- {
415- "Ask HolmesGPT" : CallbackChoice (
416- action = ask_holmes ,
417- action_params = AIInvestigateParams (
418- resource = resource , investigation_type = "issue" , ask = "Why is this alert firing?" , context = context
419- ),
420- )
421- }
422- )
423-
424397 @staticmethod
425398 def extract_mentions (title ) -> (str , str ):
426399 mentions = MENTION_PATTERN .findall (title )
@@ -667,6 +640,16 @@ def send_holmes_analysis(
667640 except Exception :
668641 logging .exception (f"error sending message to slack. { title } " )
669642
643+ def get_holmes_block (self , platform_enabled : bool , slackbot_enabled ) -> Optional [MarkdownBlock ]:
644+ if not platform_enabled and not slackbot_enabled :
645+ return MarkdownBlock ("_Ask AI questions about this alert, by connecting <https://platform.robusta.dev/create-account|Robusta SaaS> and tagging @holmes._" )
646+ elif platform_enabled and not slackbot_enabled :
647+ return MarkdownBlock ("_Ask AI questions about this alert, by adding @holmes to your <https://docs.robusta.dev/master/configuration/holmesgpt/index.html#enable-holmes-in-slack-in-the-platform|Slack>._" )
648+ elif platform_enabled and slackbot_enabled :
649+ return MarkdownBlock ("_Ask AI questions about this alert, by tagging @holmes in a threaded reply_" )
650+ return None
651+
652+
670653 def send_finding_to_slack (
671654 self ,
672655 finding : Finding ,
@@ -691,6 +674,15 @@ def send_finding_to_slack(
691674 thread_ts = thread_ts
692675 )
693676
677+ def __is_holmes_slackbot_enabled (self ) -> bool :
678+ robusta_sinks = self .registry .get_sinks ().get_robusta_sinks () if self .registry else None
679+ if not robusta_sinks :
680+ logging .debug ("No robusta sinks found, holmes not connected to slackbot" )
681+ return False
682+
683+ robusta_sink = robusta_sinks [0 ]
684+ return robusta_sink .is_holmes_slackbot_connected ()
685+
694686 def __send_finding_to_slack (
695687 self ,
696688 finding : Finding ,
@@ -725,9 +717,6 @@ def __send_finding_to_slack(
725717 )
726718 blocks .append (links_block )
727719
728- if HOLMES_ENABLED and HOLMES_ASK_SLACK_BUTTON_ENABLED :
729- blocks .append (self .__create_holmes_callback (finding ))
730-
731720 blocks .append (MarkdownBlock (text = f"*Source:* `{ self .cluster_name } `" ))
732721 if finding .description :
733722 if finding .source == FindingSource .PROMETHEUS :
@@ -753,6 +742,12 @@ def __send_finding_to_slack(
753742
754743 blocks .append (DividerBlock ())
755744
745+ is_holmes_slackbot_enabled = self .__is_holmes_slackbot_enabled ()
746+ holmes_block = self .get_holmes_block (platform_enabled , is_holmes_slackbot_enabled )
747+ if holmes_block :
748+ blocks .append (holmes_block )
749+
750+
756751 if len (attachment_blocks ):
757752 attachment_blocks .append (DividerBlock ())
758753
0 commit comments